# How can I split buffer where received the specific byte

**URL:** <https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084>\
**Category:** General\
**Created:** [21 November 2018 00:48 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084 "2018-11-21T00:48:27Z")\
**Posts on this page:** 20\
**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:** [21 November 2018 00:48 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/1 "2018-11-21T00:48:27Z")

</div>

Hi Guys,  
i working on my interfacing program and I need to split the buffer received, from a serial communication, for each one message received. Each serial message have a start/stop byte structure:

0x01 - start  
0x03 - stop

**msg received example:**  
07:50:56.694 \<- 01 80 00 00 05 95 00 00 00 10 00 03  
07:50:56.874 \<- 01 80 10 00 05 A4 00 01 00 30 00 03

My actually program can´t split it as you can see below:  
As you can see the 1st message have part of bytes in the 2st buffer (2st buffer component).  
 ![buffer%20strcuture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3a782e3cda1d0a6b8745da91004a74a337c5aea5.jpeg)

has one in it that can help me to seperate the buffers or has a specif function?

Best Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 November 2018 07:41 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/2 "2018-11-21T07:41:08Z")

</div>

Didn’t Colin answer this when you asked a similar question 8days ago?

(see his first post)

> [@How can I get array components values](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/2):
>
> Note that this is not an Array it is a Buffer, which is not the same thing. However, if you just want individual bytes as numbers then you can access as if it is an array of 8 bit integers. So you can use, for example var x = msg.payload[2] to get element 2. There are a number of questions though. Is that what you are trying to do or do you want the buffer split into a sequence of messages each containing one value or what? If you want one number per message then first convert it to an ex…

---

<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:** [21 November 2018 09:00 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/3 "2018-11-21T09:00:48Z")

</div>

Hi Mr Unkmoose. Thanks for your response.

Answering your questions, I would like to create a new sequence of bytes, in array, that have begin and end according the start and stop (0x01 and 0x03). For example:  
See below the buffer, without any treatment, received from node-red is:  
1st buffer - [**_1_** ,128,2,16,2,16,2,21,149,2,16,2,16,2,16]  
2 st buffer - [**16,2,16,_3_** ,_ **1** _,128,16,2,16,2,21,164,2,16,2,17,2,16,48,2,16, **_3_**]

**A new buffer or array shall be:**  
1st buffer/array - [1,128,2,16,2,16,2,21,149,2,16,2,16,2,16, **16,2,16,3**]  
2 st buffer/array - [1,128,16,2,16,2,21,164,2,16,2,17,2,16,48,2,16,3]  
see that each one has its size or length.  
I need to work with a function or we have a node that can be do that?

BR,  
Alex

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 November 2018 09:18 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/4 "2018-11-21T09:18:47Z")

</div>

You'll have to do it in a function.  
Take a look at [https://www.w3schools.com/nodejs/met\_buffer\_indexof.asp](https://www.w3schools.com/nodejs/met_buffer_indexof.asp)  
or  
[https://www.tutorialspoint.com/nodejs/nodejs\_buffers.htm](https://www.tutorialspoint.com/nodejs/nodejs_buffers.htm)  
which should get you started

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 November 2018 09:29 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/5 "2018-11-21T09:29:57Z")

</div>

Can 1's and 3's appear at other positions in the buffer or is it guaranteed that every 1 is the start and every 3 is the end?

---

<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:** [21 November 2018 12:39 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/6 "2018-11-21T12:39:52Z")

</div>

Hi colin,  
every msg star with 1 and stop with 3. But we have a ddifferent lengths accoridng each msg answered.

BR  
Alex

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 November 2018 13:45 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/7 "2018-11-21T13:45:27Z")

</div>

> [@aargollo](#):
>
> every msg star with 1 and stop with 3. But we have a ddifferent lengths accoridng each msg answered.

That didn't answer the question. Can there be a 1 or a 3 inside the message, or will a 1 _always_ mean start and a 3 _always_ means end?

---

<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:** [21 November 2018 13:55 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/8 "2018-11-21T13:55:12Z")

</div>

Sorry Colin, We dont have 1 or 3 inside the message!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 November 2018 14:04 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/9 "2018-11-21T14:04:56Z")

</div>

OK, are you getting this from a serial node? In which case you can configure the serial node to pass on its data each time it sees a particular character. It seems that will accept a hex code so if you set the character to 0x03 then it should give you the messages already split.

---

<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:** [21 November 2018 22:16 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/10 "2018-11-21T22:16:02Z")

</div>

Hi Colin, thanks a lot!  
The split code (with 0x03 stop byte), in the serial input, almost worked. As you can see in the PIC attached, for each message received the debug node show twice.  
for example:  
You can see in PIC in the first 4 messages (18 and 19 bytes), we have 2 to 2 duplicates, and in the last only 1 message with 37 bytes (original without 0x03).

![buffer_w%200x03%20complete](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d6c7ec02a6094c22e5f25092b8b1e877df0880a8.jpeg)

Do you have idea how to fix it?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 November 2018 07:24 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/11 "2018-11-22T07:24:16Z")

</div>

Is the debug node direct on the output of the Com node?  
Show us what is in some of the short buffers.  
Post a screenshot showing the setup of the com port config node (the one where you put in the Split On character).

---

<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:** [22 November 2018 08:49 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/12 "2018-11-22T08:49:01Z")

</div>

Hi Colin, good morning!  
Yes, of course, the debug node is direct on the serial input node. As you can see, in the PIC below, the setup configuration, nodes in the flow and debug screen:

 ![setup%20serial%20IN%20and%20node%20flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/168714c6c8a9e97c03198f29d57f46fb866f0350.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 November 2018 09:23 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/13 "2018-11-22T09:23:46Z")

</div>

Sorry, still not sure what the problem is. You haven't shown the end of the short buffer so we can't see whether it ends in 03. Is the problem that the buffer you show is correct but you are receiving it twice? If that is the problem and the debug is on the output of the Com port node then either there is a massive bug in the Com node (which is unlikely) or that data is being received twice. I think it is much more likely that the device on the other end of the wire is sending it twice.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 November 2018 09:28 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/14 "2018-11-22T09:28:55Z")

</div>

If it really is ending the exact thing twice then an RBE node will remove the duplicate 🙂

---

<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:** [22 November 2018 09:34 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/15 "2018-11-22T09:34:29Z")

</div>

Hi Colin  
The problem is the debug show it twice. The split buffer w/ stop bytes running well. The end of buffer is correctly!  
I made some experience, I changed the split input to "after a silent of" (without split code 0x03) and the debug show only one message, considering the same scenario. As you can see below:

 ![setup%20serial%20IN%20and%20node%20flow%20without%20code](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2ea22ed836471c7f6bbb1e58917d78822b1078d4.jpeg)

Do you can see that the message buffer is single!  
Do you have any idea what is happening?

---

<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:** [22 November 2018 09:36 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/16 "2018-11-22T09:36:17Z")

</div>

Dceejay, plase tel me more about node RBE. I didnt know it!

Best Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 November 2018 13:46 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/17 "2018-11-22T13:46:59Z")

</div>

> [@aargollo](#):
>
> tel me more about node RBE.

It is one of the built in nodes. It can check for a message the same as the previous one and ignore it if so.

On the serial node, if you regularly get repeated records when you use split on character but _never_ when you use the timeout then perhaps there is a problem with the serial node. If necessary do some more testing to see if you can convince yourself. If you think there is a problem with the node then I am not the one to help, but I imagine someone will want to know which version it is. You can see that from the Manage Palette window.

What is the mechanism that the sender uses to know when to send?

---

<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:** [22 November 2018 15:32 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/18 "2018-11-22T15:32:17Z")

</div>

Colin, using a serial interface, transmitting one bit every clock pulse. Just in 2 wires connected trough converter device. Could you see below more about it:  
pi@raspberrypi:~ $ lsusb  
Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

My Serial configuration at node-red side is:

 ![serial%20config](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/82bf3ca67412f4807ba38b909b5bea6d1fb22fab.jpeg)

It´s very stranger, the duplication behavior happening just when I split the buffer raw data (using a stop byte 0x03). I did another experience to split (0x92) using the same message, but 0x92/146 is on 9th position, the original buffer have 19 elements, and I didn´t see duplication behavior, but the elements 10- 19 disappeared! As you can see below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e729b7800b625bdd9bfe44a5c108d7c820f09362.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/19e3f3e956f96702f422f98e265970b3adc09644.png)  
This behavior above is normal?

I´ll make experience palliative with node RBE to try to cut duplication message!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 November 2018 16:06 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/19 "2018-11-22T16:06:03Z")

</div>

> [@aargollo](#):
>
> using a serial interface, transmitting one bit every clock puls

Sorry, I meant how does the sender know when to transmit another buffer to the node red machine. I am just looking for some mechanism that might cause it to re-transmit the same buffer. Is there any feedback back to the sender?

When you told it to split on the 9th byte I wonder whether the bytes after it got lost because the com node was processing the buffer. I would have thought the reception would be interrupt driven, but I don't know how it works so don't know.

---

<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:** [22 November 2018 23:31 UTC](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084/20 "2018-11-22T23:31:54Z")

</div>

Colin, don´t worrying!  
The exchange device does not have any mechanism to guarantee the procedures send and response message. Does not have feedback!

What makes me wary is that with timeout defined in split input works with or duplication.

Thanks a lot.  
Best Regards,  
Alex

[Next page](https://discourse.nodered.org/t/how-can-i-split-buffer-where-received-the-specific-byte/5084.md?page=2)
