# Converting an array of bytes into a int16 to write in a modbus server(holding register)

**URL:** <https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370>\
**Category:** General\
**Created:** [8 May 2022 14:47 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370 "2022-05-08T14:47:03Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:12 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/21 "2022-05-08T16:12:00Z")

</div>

Here's a part of a modbus table I'm working with :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d924163d37ab5997e0f11cc5314c7867b4fc02c.png)

Every this 16 boolean needs to be modify separetly but the problem is that the modbus server only takes UINT16.

So for example if you want the radiator to start (second BOOL):  
1- You put it to 1  
2 - Then the 16 booleans are gathered to do a binary number 0000000000000010  
3 - Finally this binary number is converted to a UINT number. For this example 2  
4 - The other automate plugged into the modbus server read this value and convert it back to a boolean

And I need to do the same first 3 step on Nodered

---

<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:** [8 May 2022 16:15 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/22 "2022-05-08T16:15:53Z")

</div>

I thought this might be where you are heading BUT when you write to bit 1, you will reset bits 0 & 2, 3 → 15

Is it a problem if you write the value `0000 0000 0000 0010` (AKA `0x0002` AKA `2`) to ModBus register 60 ?

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:18 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/23 "2022-05-08T16:18:55Z")

</div>

This is not a problem because they will change the parameters once at a time.

I wasn't sure if the server would like It but I can try right now.

I just need to put the 16 numbers together because he won't accept an array (that's why I tried to convert it directly to a UINT.)

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:39 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/24 "2022-05-08T16:39:32Z")

</div>

It doesn't work with the `0000 0000 0000 0010`

I will have to do the convert It

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:44 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/25 "2022-05-08T16:44:02Z")

</div>

So is the buffer parser still an option ? Because I don't know how to use it properly

---

<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:** [8 May 2022 16:44 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/26 "2022-05-08T16:44:07Z")

</div>

> [@Theodros](#):
>
> This is not a problem because they will change the parameters once at a time

What do you mean exaclty...

1. User will change all necessary values THEN press the button to compute a 16bit value & write that to ModBus  
OR
2. User will change 1 value THEN press the button to compute a 16bit value & write that to ModBus

The reason I ask..

If it is option `2` then changing `60.1` to `on` then `60.2` to `on` will write `0x0002` then write `0x0004`

That means

| Button Press | set value | computed 16 bit value | HEX |
| --- | --- | --- | --- |
| 1 | 1 | 0000 0000 0000 0000 0010 | 0x0002 |
| 2 | 1 | 0000 0000 0000 0000 0100 | 0x0004 |

In simple terms, converting 16 bits with only 1 set will reset the others.

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:44 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/27 "2022-05-08T16:44:59Z")

</div>

Yes it is option 2

---

<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:** [8 May 2022 16:46 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/28 "2022-05-08T16:46:13Z")

</div>

> [@Theodros](#):
>
> option 2

So is it a problem that when the user changes `60.3` it will write `0000 0000 0000 1000` (`0x0008) meaning it will RESET 60.2 and 60.1 (and all the others) back to zero?

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:46 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/29 "2022-05-08T16:46:36Z")

</div>

No it's not a problem

---

<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:** [8 May 2022 16:46 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/30 "2022-05-08T16:46:52Z")

</div>

ok, 5 mins thinking!

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 16:47 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/31 "2022-05-08T16:47:18Z")

</div>

You're the best I swear

---

<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:** [8 May 2022 17:06 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/32 "2022-05-08T17:06:38Z")

</div>

So, based on my understanding of what you want (I still think there is one missing piece but hey ho)..

![chrome_juhv0IuMNq](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4b086561926f6ca2004875d1e142529f658d0af.gif)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f83467c58e43cada4297e6839f13bb59caa47632.png)

The demo flow...

```auto
[{"id":"d25e7704f45533a2","type":"function","z":"fb49eb9df4ba7fc2","name":"Adressage","func":"\nconst num = msg.payload.S1; //Chiffre allant de 0 à 79\nconst val = msg.payload.B1; // booléen\n\nconst parsedInput = { WORD_OFFSET: Math.round(num / 16), BIT: num % 16, BIT_VAL: !!val }\nconst value = updateBit(0, parsedInput.BIT, parsedInput.BIT_VAL);\n\nmsg.payload = {\n inputData: msg.payload,\n parsedInput: parsedInput,\n register: 60 + parsedInput.WORD_OFFSET,\n value: value,\n};\nreturn msg\n\n\n\nfunction getBit(number, bitPosition) {\n return (number & (1 << bitPosition)) === 0 ? 0 : 1;\n}\nfunction setBit(number, bitPosition) {\n return number | (1 << bitPosition);\n}\nfunction clearBit(number, bitPosition) {\n const mask = ~(1 << bitPosition);\n return number & mask;\n}\nfunction updateBit(number, bitPosition, bitValue) {\n const bitValueNormalized = bitValue ? 1 : 0;\n const clearMask = ~(1 << bitPosition);\n return (number & clearMask) | (bitValueNormalized << bitPosition);\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":554,"y":256,"wires":[["928692da614a222d"]]},{"id":"691ae780d1dc0dec","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":2,\"B1\":0}","payloadType":"json","x":350,"y":208,"wires":[["d25e7704f45533a2"]]},{"id":"4910d876dc0a21cb","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":4,\"B1\":1}","payloadType":"json","x":352,"y":256,"wires":[["d25e7704f45533a2"]]},{"id":"a5183058fbef4f74","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":79,\"B1\":1}","payloadType":"json","x":352,"y":304,"wires":[["d25e7704f45533a2"]]},{"id":"928692da614a222d","type":"debug","z":"fb49eb9df4ba7fc2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":730,"y":256,"wires":[]},{"id":"2967c630b3030608","type":"comment","z":"fb49eb9df4ba7fc2","name":"Test data","info":"","x":332,"y":160,"wires":[]},{"id":"4b856a9f60ddccc2","type":"comment","z":"fb49eb9df4ba7fc2","name":"Details for modbus","info":"","x":742,"y":208,"wires":[]}]

```

Hope it helps.

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 17:12 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/33 "2022-05-08T17:12:18Z")

</div>

I implement It and I give you a feedback but I'm almost sure It's the way

---

<div class="post-metadata">

**Author:** ![Theodros](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theodros/32/61068_2.png) [@Theodros](https://discourse.nodered.org/u/Theodros)\
**Post date:** [8 May 2022 17:17 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/34 "2022-05-08T17:17:06Z")

</div>

That's the solution 👍 : , I'm going to study it to understand what you did exactly  
Thank you for eveything, I see you're helping on a lot of forums and It's very cool to have people like you around 😁

---

<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:** [22 May 2022 17:17 UTC](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370/35 "2022-05-22T17:17:19Z")

</div>

Closed on 22 May 2022 17:17 UTC

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

[Previous page](https://discourse.nodered.org/t/converting-an-array-of-bytes-into-a-int16-to-write-in-a-modbus-server-holding-register/62370.md?page=1)
