I have a question about buffers

flows (8).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 .

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).

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?

  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

  3. Explain what the bytes mean e.g.

    • bytes 0+1 are a UINT16 (voltage)
    • bytes 2+3 are a UINT16 (current)
    • byte 4 is 8 boolean flags
    • byte 5 is an 8bit int (status code)
    • etc
    • etc

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)

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

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