# Sending modbus bit

**URL:** <https://discourse.nodered.org/t/sending-modbus-bit/92807>\
**Category:** General\
**Created:** [30 October 2024 16:55 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807 "2024-10-30T16:55:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [30 October 2024 16:55 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/1 "2024-10-30T16:55:04Z")

</div>

Hi,

I have modbus control for power that works because bit 0, but if I want to control fan speed I have to use bit 12-15. Can you help me on how to do it?

thanks

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

```auto
[
    {
        "id": "bced3dc5da31bb7a",
        "type": "debug",
        "z": "0de2fcb7c52ddc84",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1210,
        "y": 320,
        "wires": []
    },
    {
        "id": "d52474f17c2c94cd",
        "type": "modbus-write",
        "z": "0de2fcb7c52ddc84",
        "name": "power 2000",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "2000",
        "quantity": "1",
        "server": "31b3b9976cf2bddc",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 1030,
        "y": 340,
        "wires": [
            [
                "bced3dc5da31bb7a"
            ],
            []
        ]
    },
    {
        "id": "1105add276c6b266",
        "type": "inject",
        "z": "0de2fcb7c52ddc84",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 850,
        "y": 360,
        "wires": [
            [
                "d52474f17c2c94cd"
            ]
        ]
    },
    {
        "id": "a59a858968ea6b25",
        "type": "inject",
        "z": "0de2fcb7c52ddc84",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 850,
        "y": 320,
        "wires": [
            [
                "d52474f17c2c94cd"
            ]
        ]
    },
    {
        "id": "31b3b9976cf2bddc",
        "type": "modbus-client",
        "name": "MODBUS",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "192.168.28.202",
        "tcpPort": "10502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": false,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true,
        "showErrors": false,
        "showWarnings": true,
        "showLogs": true
    }
]

```

---

<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:** [30 October 2024 18:43 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/2 "2024-10-30T18:43:56Z")

</div>

You will need to read the value, mask the bit on/off and send the full WORD value back

To mask the bits on/off, use a function node. e.g: [Node-red-contrib-remote-io, digital output for joining bool to word - #2 by Steve-Mcl](https://discourse.nodered.org/t/node-red-contrib-remote-io-digital-output-for-joining-bool-to-word/11798/2)

Alternatively, use the `buffer-maker` and `buffer-parser` nodes to conver your data to/from BOOLS in the `node-red-contrib-buffer-parser` package.

---

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [5 December 2024 06:45 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/3 "2024-12-05T06:45:32Z")

</div>

this document modbus

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

I try it to read modbus state power, fan speed and bit  
result this

write modbus register address  
2030 : power&fan speed  
2031 : mode  
2032 : setpoint  
read modbus register address:  
2060 : power&fan speed  
2061 : mode  
2062 : setpoint

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

```auto
[{"id":"880c9fb2aff63126","type":"modbus-read","z":"3a6301a4.44064e","name":"","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"showWarnings":true,"unitid":"2","dataType":"InputRegister","adr":"2060","quantity":"6","rate":"1","rateUnit":"h","delayOnStart":false,"startDelayTime":"","server":"4baeb19a89ea1516","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":130,"y":580,"wires":[["32cb358fbd4ef284","7b6de22caa16aba4"],[]]},{"id":"32cb358fbd4ef284","type":"buffer-maker","z":"3a6301a4.44064e","name":"","specification":"spec","specificationType":"ui","items":[{"name":"item1","type":"uint16be","length":1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":350,"y":600,"wires":[["07eb1757d3970a03"]]},{"id":"7b6de22caa16aba4","type":"function","z":"3a6301a4.44064e","name":"read modbus","func":"msg.payload = {\n power: msg.payload[0] & 1,\n fan_speed: (msg.payload[0] >> 12) & 7\n\n};\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":540,"wires":[["48235995625ce4b0"]]},{"id":"07eb1757d3970a03","type":"buffer-parser","z":"3a6301a4.44064e","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"decimal","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"16bitbe","name":"binary","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":510,"y":600,"wires":[["48235995625ce4b0"]]},{"id":"48235995625ce4b0","type":"debug","z":"3a6301a4.44064e","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":850,"y":600,"wires":[]},{"id":"37c416aa94d3e1a1","type":"modbus-write","z":"3a6301a4.44064e","name":"Power 2030","showStatusActivities":false,"showErrors":false,"showWarnings":true,"unitid":"2","dataType":"HoldingRegister","adr":"2030","quantity":"1","server":"4baeb19a89ea1516","emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":290,"y":700,"wires":[["48235995625ce4b0"],[]]},{"id":"92d7d9899886a56e","type":"inject","z":"3a6301a4.44064e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":720,"wires":[["37c416aa94d3e1a1"]]},{"id":"1a54cadffe125476","type":"inject","z":"3a6301a4.44064e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":680,"wires":[["37c416aa94d3e1a1"]]},{"id":"4baeb19a89ea1516","type":"modbus-client","name":"MODBUS NEW","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"192.168.28.202","tcpPort":"10502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":2,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true,"showErrors":false,"showWarnings":true,"showLogs":true}]

```

Until now I have not been able to control the fan speed.

---

<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:** [5 December 2024 07:23 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/4 "2024-12-05T07:23:38Z")

</div>

Why do you have buffer maker before buffer parser? That will overwrite incoming payload.

For reading, you should read -\> parse (to friendly object with nice names props).

For writing, you must use current state and set/update any bits you wish to change. So ...

Read (get good/latest values) \> parse (to get user friendly format) \> change node (to update bit/nibble) \> maker (to pack the bits/flags/nibbles into bytes) \> parser (to make 16bit unsigned integer array) \> write node.

---

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [25 February 2025 09:18 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/5 "2025-02-25T09:18:58Z")

</div>

Hi Steve

I just tried again because i have other things to do.

I tried to make it like this, but the status in the unit doesn't change

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

```auto
[{"id":"09ec2141e6c1b5ab","type":"inject","z":"787d729432b0e968","name":"H","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":100,"wires":[["5ad8ee2169394bdc"]]},{"id":"5ad8ee2169394bdc","type":"function","z":"787d729432b0e968","name":"Read-2000","func":"msg.payload = {\n //value: [currentValue], \n fc: 4, \n unitid: 2, \n address: 2000, \n quantity: 1 \n};\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":100,"wires":[["bd49b968d1f85815"]]},{"id":"bd49b968d1f85815","type":"modbus-flex-getter","z":"787d729432b0e968","name":"","showStatusActivities":false,"showErrors":false,"showWarnings":true,"logIOActivities":false,"server":"9df2f773a56799fd","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":610,"y":100,"wires":[["07eb1757d3970a03"],[]]},{"id":"07eb1757d3970a03","type":"buffer-parser","z":"787d729432b0e968","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"decimal","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"16bitbe","name":"binary","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":810,"y":100,"wires":[["3ac1f1d36a035f60"]]},{"id":"3ac1f1d36a035f60","type":"function","z":"787d729432b0e968","name":"High","func":"var data = msg.payload;\nvar currentValue = data.decimal;\nvar newValue = (currentValue | (1 << 12)) & ~(1 << 13) | (1 << 14);\n\nmsg.payload = newValue;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":160,"wires":[["32cb358fbd4ef284"]]},{"id":"32cb358fbd4ef284","type":"buffer-maker","z":"787d729432b0e968","name":"","specification":"spec","specificationType":"ui","items":[{"name":"decimal","type":"uint16be","length":1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":510,"y":160,"wires":[["67eac0f9aac29a52"]]},{"id":"67eac0f9aac29a52","type":"buffer-parser","z":"787d729432b0e968","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"decimal","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"","msgPropertyType":"","resultType":"keyvalue","resultTypeType":"","multipleResult":false,"fanOutMultipleResult":false,"setTopic":false,"outputs":1,"x":670,"y":160,"wires":[["2246b8865ae6891e"]]},{"id":"2246b8865ae6891e","type":"modbus-write","z":"787d729432b0e968","name":"","showStatusActivities":false,"showErrors":false,"showWarnings":true,"unitid":"2","dataType":"HoldingRegister","adr":"2000","quantity":"1","server":"9df2f773a56799fd","emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":860,"y":160,"wires":[["f00d1701955d1671"],[]]},{"id":"f00d1701955d1671","type":"debug","z":"787d729432b0e968","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1050,"y":160,"wires":[]},{"id":"9df2f773a56799fd","type":"modbus-client","name":"MODBUS-TCP","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"192.168.28.203","tcpPort":"10502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":"2","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true,"showErrors":false,"showWarnings":true,"showLogs":true}]

```

If I control it manually via remote  
I read the status:  
low:  
decimal : 4352  
[{"bits":[0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0],"bit0":0,"bit1":0,"bit2":0,"bit3":0,"bit4":0,"bit5":0,"bit6":0,"bit7":0,"bit8":1,"bit9":0,"bit10":0,"bit11":0,"bit12":1,"bit13":0,"bit14":0,"bit15":0}]

medium :  
decimal: 12544  
[{"bits":[0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0],"bit0":0,"bit1":0,"bit2":0,"bit3":0,"bit4":0,"bit5":0,"bit6":0,"bit7":0,"bit8":1,"bit9":0,"bit10":0,"bit11":0,"bit12":1,"bit13":1,"bit14":0,"bit15":0}]

high :  
decimal: 20736  
[{"bits":[0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0],"bit0":0,"bit1":0,"bit2":0,"bit3":0,"bit4":0,"bit5":0,"bit6":0,"bit7":0,"bit8":1,"bit9":0,"bit10":0,"bit11":0,"bit12":1,"bit13":0,"bit14":1,"bit15":0}]

Image Low

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

Image Medium

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

Image High

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/753014a031233fb7b2dfb958466704adc9b61db9.png)

Thank's

---

<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 May 2025 09:19 UTC](https://discourse.nodered.org/t/sending-modbus-bit/92807/6 "2025-05-26T09:19:10Z")

</div>

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