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

**URL:** <https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541>\
**Category:** Industrial\
**Created:** [26 April 2020 11:12 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541 "2020-04-26T11:12:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [26 April 2020 11:12 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/1 "2020-04-26T11:12:42Z")

</div>

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?

thanks

---

<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:** [26 April 2020 14:07 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/2 "2020-04-26T14:07:43Z")

</div>

Hi. You English is pretty good 👍

> [@e-Duardo](#):
>
> any idea to do that

Yeah, all possible, start reading up on [nodejs buffer](https://nodejs.org/docs/latest-v12.x/api/buffer.html).

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](https://www.geeksforgeeks.org/javascript-bitwise-operators/amp/)

For creating a message to send your data payload, refer to node-red documentation [working with messages](https://nodered.org/docs/user-guide/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.

---

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [27 April 2020 08:12 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/3 "2020-04-27T08:12:30Z")

</div>

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.

---

<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:** [27 April 2020 08:15 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/4 "2020-04-27T08:15:53Z")

</div>

Good stuff.

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

---

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [27 April 2020 10:13 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/5 "2020-04-27T10:13:56Z")

</div>

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

[image|690x383](https://discourse.nodered.org/uploads/short-url/9evnJUPSGs5cuzJDRZRc1i570gl.png)

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.

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

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 April 2020 10:33 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/6 "2020-04-27T10:33:42Z")

</div>

> [@e-Duardo](#):
>
> for(i=1; 1\<8; i++)//

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

---

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [27 April 2020 10:45 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/7 "2020-04-27T10:45:55Z")

</div>

ups!! no comment... 🤦‍♂️😳  
Thank you again!

---

<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:** [26 June 2020 10:55 UTC](https://discourse.nodered.org/t/very-newbie-assembling-a-message-to-be-sent/25541/8 "2020-06-26T10:55:08Z")

</div>

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