Hi Guys, I working on my device and I have doubts how to including SOH and ETX bytes at beginning and end on the new buffer flow. My code building the cmd to send via serial port w/ specific rules.
To sending through the serial port, my encoding function, needs to replace all those bytes that have less than "0x0a". I got success to encoding all the bytes, but I do not know how to including SOH (0x01) and ETX (0x03) bytes at the beginning and end respectively.
The predecessor node building the command without serial rules, as I described above. However this node have inside SOH and ETX bytes. But to processing each byte I had to take it, in the next node.
As you can see below the predecessor buffer and the output from encoding function.
predecessor buffer flow:
output from my encoding function That*I need to include SOH and ETX bytes.*
my encoding function
//Loop through and decode any encoded values...
var SOH = 0x01;
var ETX = 0x03;
var encodedNext = true;
var encodedData = [];
console.log (msg.payload);
for (var i = 1; i < msg.payload.length -1; i++) {
console.log(`the buffer length is: ` + msg.payload.length);
//get this byte
let b = msg.payload[i];
//if last was <= 0x0a, increase 0x02 0x1x and store it
if (encodedNext && (b <= 0x0a)) { // to change serial byte sent form 0x?b to 0x0b
console.log(`the value is in the range - b <= 0x0a`);
encodedData.push(0x02,b^0x10);
console.log ('the buffer position was:' +i);
console.log(b);
encodedNext = true;//reset flag
continue;//next loop
}
if (b <= 0x0a) {
encodedNext = false;//set flag & continue (dont store this one)
continue;//next loop
}
//if we reach here - simply store it
encodedData.push(b);
console.log ('the buffer member outoff range position was:' +i);
console.log(b);
}
console.log(encodedData);
node.send({payload: Buffer.from(encodedData)});
Anybody have idea how to fix it?
Mr @Steve-Mcl, please do you can help me?
Best Regards,
Alex