How to add SOH at beginning and ETX at end

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:
image

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

hi @aargollo, not certain why you tagged me in your post (I haven't done much with Serial ports in Node-red).

That said, pressure on, I will try to assist

Firstly, as I'm sure you know SOH == 0x1 and ETX == 0x3 (reference)

Also, as I'm sure you know, you can push a value to the front of an array using unshift() and push a value to the end of an array using push()

Handy reference...

Based on this knowledge, I'd suspect you can do the following before sending the data to the serial port....

encodedData.unshift(0x1);//pre-pend with SOH
encodedData.push(0x3);//append with ETX
node.send({payload: Buffer.from(encodedData)}); //send to next node (i.e. the serial port);
/*alternatively use...
msg.payload = Buffer.from(encodedData);
return msg; 
*/

Does that help any?

Disclaimer...
its 2am and i'm fairly drunk

Regards, Steve.

Hi Steve, its working very well!!!!
thannnkssss... a lot!!!!
even drunk you are the best! :slight_smile:

Best regards Alex!