# How to add SOH at beginning and ETX at end

**URL:** <https://discourse.nodered.org/t/how-to-add-soh-at-beginning-and-etx-at-end/8794>\
**Category:** General\
**Created:** [10 March 2019 01:47 UTC](https://discourse.nodered.org/t/how-to-add-soh-at-beginning-and-etx-at-end/8794 "2019-03-10T01:47:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [10 March 2019 01:47 UTC](https://discourse.nodered.org/t/how-to-add-soh-at-beginning-and-etx-at-end/8794/1 "2019-03-10T01:47:05Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/dcb84875b24063a297c3046ae4f93ff043a5a0f8.png)

**output from my encoding function** That`*I need to include SOH and ETX bytes.*`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fdcdc96c263eed861d904b39ade16b53b3a839f6.png)

**my encoding function**

```auto
//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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 March 2019 02:13 UTC](https://discourse.nodered.org/t/how-to-add-soh-at-beginning-and-etx-at-end/8794/2 "2019-03-10T02:13:13Z")

</div>

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](http://www.asciitable.com/))

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...  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/00476f38c80a111afd19f0fec7c9ff9b90b39a39.jpeg)

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [10 March 2019 10:25 UTC](https://discourse.nodered.org/t/how-to-add-soh-at-beginning-and-etx-at-end/8794/3 "2019-03-10T10:25:02Z")

</div>

> [@Steve-Mcl](#):
>
> its 2am and i'm fairly drunk

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

Best regards Alex!
