# Modbus unsigned int 16

**URL:** <https://discourse.nodered.org/t/modbus-unsigned-int-16/53319>\
**Category:** Dashboard\
**Created:** [7 November 2021 04:02 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319 "2021-11-07T04:02:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [7 November 2021 04:02 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/1 "2021-11-07T04:02:21Z")

</div>

Hi Guys this is my first post, i have read all the post on modbus RTU and i have made a 95% there with the [flows (8).json](https://discourse.nodered.org/uploads/short-url/arbtGoZVU0j9W5GBZIoFD3OVFZf.json) (398 Bytes)  
flow just by reading and testing

The issue i have is the 16 bit true or false or 1 / 0, on my mode bus address 1015 there is 14 bits being used for the Error flag, what i would like to do is when i get bit 4 with true i want to change this to output "Error A12 (Sensor 2 Broken) so i can display this on the Dashboard,

What have i done Wrong?  
Regards Dave

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [7 November 2021 10:28 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/2 "2021-11-07T10:28:03Z")

</div>

Here are 4 simple functions for bit manipulation:

> [@Node-red-contrib-mcp23017chip](https://discourse.nodered.org/t/node-red-contrib-mcp23017chip/37999/71):
>
> I tend to use a set of functions that help me avoid typos... //Get bit function getBit(number, bitPosition) { return (number & (1 \<\< bitPosition)) === 0 ? 0 : 1; } //Set Bit function setBit(number, bitPosition) { return number | (1 \<\< bitPosition); } //Clear Bit function clearBit(number, bitPosition) { const mask = ~(1 \<\< bitPosition); return number & mask; } //Update Bit function updateBit(number, bitPosition, bitValue) { const bitValueNormalized = bitValue ? 1 : 0; const clearM…

It is safer to use those.

It would be easier for us, if you would export the **whole** Flow, so we can check out.  
You have only exported 1 record with some values.

---

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [7 November 2021 10:54 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/3 "2021-11-07T10:54:08Z")

</div>

Thank you for the reply, all i want to do is have a lookup table for the fault code, at the moment i get 1 or 0

Please find attached the full flow

[flows (9).json](https://discourse.nodered.org/uploads/short-url/aFlMVM16fqHGLnP1mlwzPORx2c6.json) (18.3 KB)

Regards Dave

---

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [10 November 2021 08:28 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/4 "2021-11-10T08:28:44Z")

</div>

Hi PizzaProgram could you give me a example on your temple Regards Dave

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [10 November 2021 08:30 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/5 "2021-11-10T08:30:06Z")

</div>

> [@Migs74](#):
>
> your temple Regards Dave

Sorry, I don't understand.

---

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [10 November 2021 08:50 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/6 "2021-11-10T08:50:08Z")

</div>

Sorry  
sorry i mean this code, which part do i use to trigger a word for modbus,

I tend to use a set of functions that help me avoid typos... //Get bit function getBit(number, bitPosition) { return (number & (1 \<\< bitPosition)) === 0 ? 0 : 1; } //Set Bit function setBit(number, bitPosition) { return number | (1 \<\< bitPosition); } //Clear Bit function clearBit(number, bitPosition) { const mask = ~(1 \<\< bitPosition); return number & mask; } //Update Bit function updateBit(number, bitPosition, bitValue) { const bitValueNormalized = bitValue ? 1 : 0; const clearM…

regards Dave

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [10 November 2021 08:55 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/7 "2021-11-10T08:55:07Z")

</div>

Those functions are not related to Modbus or Triggering.  
Just help you to get/set 1-1 bit inside a number very fast.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 November 2021 13:30 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/8 "2021-11-10T13:30:58Z")

</div>

> [@Migs74](#):
>
> Thank you for the reply, all i want to do is have a lookup table for the fault code, at the moment i get 1 or 0

What does that mean? What incoming values do you have in the payload and what do you want the payload to be set to by the function?

---

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [19 November 2021 03:34 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/9 "2021-11-19T03:34:30Z")

</div>

![Screen Shot 2021-11-19 at 1.20.54 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14e82d4d72c59d82d9793e06297b7235dbed79cc.png)

Hi Colin

I have a modbus address which is a Unsigned Int 16 and i have used the 16 bit block in my flow which is called node-red-contrib-bit, what i would like to do is send a message so the ui dashboard that if the "true" message it will output the message "Alarm Fault" and if the message outputs false then it will output the message "Alarm Okay"

I hope this make more sense

Regards Dave

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2021 10:06 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/10 "2021-11-19T10:06:50Z")

</div>

To convert the true/false state to the alarm strings you can use a Change node configured like this

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

```auto
[{"id":"dbed5f846f13f754","type":"change","z":"bdd7be38.d3b55","name":"Alarm string","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload ? \"Alarm Fault\" : \"Alarm OK\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":1480,"wires":[["2ed65241d345c5e5"]]},{"id":"301c60703350d575","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":160,"y":1440,"wires":[["dbed5f846f13f754"]]},{"id":"226f750e4a7cdf6c","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":160,"y":1520,"wires":[["dbed5f846f13f754"]]},{"id":"2ed65241d345c5e5","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":1480,"wires":[]}]

```

Then to put it on the dashboard you can either send it to a Text node, or if you want a popup notification then use the Notification node.

---

<div class="post-metadata">

**Author:** ![Migs74](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Migs74](https://discourse.nodered.org/u/Migs74)\
**Post date:** [19 November 2021 10:41 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/11 "2021-11-19T10:41:01Z")

</div>

Thank you Colin, that has been a god send thank you. it work's really well

---

<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:** [19 December 2021 10:41 UTC](https://discourse.nodered.org/t/modbus-unsigned-int-16/53319/12 "2021-12-19T10:41:25Z")

</div>

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