Skip to content

Commit

Permalink
Default DLE/STX sequence for TCP/UDP
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahCallaway committed Dec 7, 2021
1 parent 22b2bdc commit aa09789
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ umd.sendTallyTCP('192.168.X.X', 9000, tally)

### git

- <https://github.com/NoahCallaway/tsl-umd-v5>
- <https://github.com/NoahCallaway/tsl-umd-v5>

<br>

---

### DLE/STX Sequence

By default, the DLE/STX sequence is **enabled on TCP** packets and **disabled on UDP** packets as specified [here](https://tslproducts.com/media/1959/tsl-umd-protocol.pdf).

If necessary, use the `sequence` argument to override the defaults.

```javascript
//Send UDP tally with DLE/STX forced ON
umd.sendTallyUDP('192.168.X.X', 8900, tally, true)

//Send TCP tally with DLE/STX forced OFF
umd.sendTallyTCP('192.168.X.X', 9000, tally, false)
```
45 changes: 30 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TSL5 extends EventEmitter {
this.emit('message', tally)
}

constructPacket(tally) {
constructPacket(tally, sequence) {
let bufUMD = Buffer.alloc(12)

if (!tally.index) {
Expand Down Expand Up @@ -130,26 +130,36 @@ class TSL5 extends EventEmitter {
let msgLength = Buffer.byteLength(bufUMD) - 2
bufUMD.writeUInt16LE(msgLength, this._PBC)

//Add DLE/STX and stuffing
let packetBuf = Buffer.from([this._DLE, this._STX])
//Add DLE/STX and stuffing if needed
if (sequence) {
let packetBuf = Buffer.from([this._DLE, this._STX])

for(let i = 0; i < bufUMD.length; i++) {
if (bufUMD[i] == this._DLE) {
packetBuf = Buffer.concat([packetBuf, Buffer.from([this._DLE, this._DLE])])
} else {
packetBuf = Buffer.concat([packetBuf, Buffer.from([bufUMD[i]])])
for(let i = 0; i < bufUMD.length; i++) {
if (bufUMD[i] == this._DLE) {
packetBuf = Buffer.concat([packetBuf, Buffer.from([this._DLE, this._DLE])])
} else {
packetBuf = Buffer.concat([packetBuf, Buffer.from([bufUMD[i]])])
}
}
return packetBuf

} else {
return bufUMD
}
return packetBuf
}

sendTallyUDP(ip, port, tally) {
sendTallyUDP(ip, port, tally, sequence) {
try {
if (!ip | !port | !tally){
throw 'Missing Parameter from call sendTallyUDP()'
}
let msg = this.constructPacket(tally)

if (sequence == null) {
debug('No DLE/STX sequence by default for UDP.')
sequence = false
}

let msg = this.constructPacket(tally, sequence)

let client = dgram.createSocket('udp4')

client.send(msg, port, ip, function(error) {
Expand All @@ -166,13 +176,18 @@ class TSL5 extends EventEmitter {
}
}

sendTallyTCP(ip, port, tally) {
sendTallyTCP(ip, port, tally, sequence) {
try {
if (!ip | !port | !tally){
throw 'Missing Parameter from call sendTallyTCP()'
}
let msg = this.constructPacket(tally)

if (sequence == null) {
debug('Adding DLE/STX sequence by default for TCP.')
sequence = true
}

let msg = this.constructPacket(tally, sequence)

let client = new net.Socket()
client.connect(port, ip);

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsl-umd-v5",
"version": "1.0.4",
"version": "1.0.5",
"description": "TSL UMDv5 Protocol for Node.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit aa09789

Please sign in to comment.