(very newbie) "assembling" a message to be sent

Hi all,
Fist of all I want to apologize for not being able to express my self very well in english (as well for my misspelligns).
I am just starting out with Node-Red and my programming background is only making easy programs for arduino (something simmilar to C++), and I am learning C# since fes weeks ago.

I started doing easy flows with node-red that work (flows that does not require code writting) without need of help. But I have a problem that I am not able to solve at this moment.

I want to send a command to the driver of a stepper via the serial port (USB). Each command consists of 9 bytes:

  • byte 1: module adress
  • byte 2: Command number
  • byte 3: Type number
  • byte 4: Motor or bank number
  • bytes 5-8: Value (MSB first)
  • byte 9: checksum (sum of previous 8 bytes)

When I started programming arduino (or when starting out wiht C#), one of the first thinks that I became familiar with was the variable statement, so that the number of bits of each variable is fixed. But in node-red I haven't found a way to define the bit-lenght of messages.

In this case, what I think that I have to do is:

  • Create the first part of the message (bytes 1-4) as a fixed one.
  • Read the input (from a dashboard node) to set the "value", and express it as a 32 bits integer.
  • split the 32 bit number in 4 bytes.
  • sum the 4 bytes of the first part of the message and the 4 bytes of the Value.
  • Put the 9 bytes (4 of the first part of the command, 4 bytes of the value and the checksum) toghether and send the message...

any idea to do that?


Hi. You English is pretty good :+1:

Yeah, all possible, start reading up on nodejs buffer.

Buffer has swathes of functions for reading and writing bytes, words, floats, 32bit, signed / unsigned, BE, LE, and so on.

For things like bit shifts, masking etc look at the JavaScript Bitwise Operators

For creating a message to send your data payload, refer to node-red documentation working with messages

Try putting it all together in an easy example (make sure you make use of debug nodes and node.warn()). When you get stuck, come back and ask a question.

Thank you so much for your hints,
I think that now I got it!
At least in the debug window I am seing what I expect.

Good stuff.

It would be good to post your solution (screen shots, description, flow) for future readers to benefit from?

I tried a very simple flow. I connected the stepper via USB and it works, so I have a starting point.


I have problems with the checksum. I tried a for loop but it caused the flow not to respond, so i summed the 0-7 bytes at once. You can see for loop, which is commented, so that you can see where is the error.

var buf1 = Buffer.alloc(9);

buf1[0] = 1; // module address
buf1[1] = 4; // command(4: move to position...)
buf1[2] = 0; // command type (0: absolute position; 1: relative position)
buf1[3] = 0; // motor num

buf1.writeInt32BE(msg.payload,4); // bytes from 5 to 8 correspond to command value (position in steps)

//var checksum = buf1[0];
//var i;
//for(i=1; 1<8; i++)//
//   checksum = checksum + buf1[i];
// buf1[8] = checksum;

buf1[8]= buf1[0] + buf1[1] + buf1[2] + buf1[3] + buf1[4] + buf1[5] + buf1[6] + buf1[7] + buf1[8];

var msg1 = {payload: buf1};
return msg1;

You might want to look at what you are doing in this statement. Look very carefully!

ups!! no comment... :man_facepalming::flushed:
Thank you again!

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