# I have a question about buffers

**URL:** <https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130>\
**Category:** General\
**Created:** [7 July 2021 11:49 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130 "2021-07-07T11:49:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)\
**Post date:** [7 July 2021 11:49 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/1 "2021-07-07T11:49:11Z")

</div>

[flows (8).json](https://discourse.nodered.org/uploads/short-url/jtKrylruYwkxJ2CVPlGWX1bfENp.json) (41.2 KB)

I have a question about buffers.  
Flow has been uploaded.  
The sequence is as follows.

1. A random buffer was injected into the injection.

[1,3,0,101,0,5]

1. Create the first buf1 rule in the function.

var buf1=Buffer.alloc(msg.payload[5]\*2+3);  
buf1[0]=msg.payload[0];  
buf1[1]=msg.payload[1];  
buf1[2]=msg.payload[5]\*2;

1. Create a second buf1 in the function.

var buf2=Buffer.alloc(320);  
buf2[100]=1;  
buf2[103]=2;  
buf2[113]=12;  
buf2[115]=14;  
buf2[117]=16;  
buf2[118]=17;

1. In the rule of buf1, the data of buffer 2 is inserted from the point payload[3].

buf2.copy(buf1,3,100,139);

1. At this length, the length refers to buf1[2] of buf1.

2. The generated payload looks like this:

[1,3,10,1,0,0,2,0,0,0,0,0,0]

> 01 03 0A 01 00 00 02 00 00 00 00 00 00  
> But this buffer is not what I want.

The buffer I want is:  
[1,3,10,0,1,0,0,0,0,0,2,0,0]

> 01 03 0A 00 01 00 00 00 00 00 02 00 00

That is, it seems to split 01 from payload[3] into 00 01 form.  
May I know how to do this?  
For reference, this is a Modbus RTU serial method.  
It must be expressed as Uint16 .

---

<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:** [7 July 2021 11:54 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/2 "2021-07-07T11:54:03Z")

</div>

> [@Athurian](#):
>
> For reference, this is a Modbus RTU serial method.  
> It must be expressed as Uint16 .

[Buffer Parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) was made for this type of task for converting bytes into nicely named values (int, INT32, UINT32, float, string, big endian, little endian, etc).

---

<div class="post-metadata">

**Author:** ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)\
**Post date:** [7 July 2021 11:55 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/3 "2021-07-07T11:55:46Z")

</div>

I tried, but it doesn't come out.  
buffer [1,3,0,101,0,5]

[1,3,10,0,1,0,0,0,0,0,2,0,0]

Can the data come out?

---

<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:** [7 July 2021 12:08 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/4 "2021-07-07T12:08:24Z")

</div>

1. Show me (in a screenshot) the value coming out of the modbus node

2. put a debug on the output of modbus node (set debug node to show complete message) then use the **copy value** button that appears under your mouse pointer when you hover over the message  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8de91393732cdca1d05cfd73dba839c31a624354.png)

3. Explain what the bytes mean e.g.

* * *

PS, if you dont reply directly to my message I wont get a notification (use the reply button on my message not the reply button at the bottom of the page)

---

<div class="post-metadata">

**Author:** ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)\
**Post date:** [8 July 2021 05:01 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/5 "2021-07-08T05:01:05Z")

</div>

I solved the problem. I made a code fix in the function node. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 September 2021 05:01 UTC](https://discourse.nodered.org/t/i-have-a-question-about-buffers/48130/6 "2021-09-06T05:01:47Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
