MODBUS - Write boolean to Word

Aha! .. only after your last posts did it 'register' with me that you were sending decimals to holding registers to control the DO.
I made some modifications to the flow to reflect that .. i dont have time now to expand but there are some comments between the function code.

Try the following :

[{"id":"7ed14e5.f2503b","type":"modbus-flex-write","z":"ddb94f08.dac8a8","name":"","showStatusActivities":false,"showErrors":false,"server":"28c906aa.79aae2","emptyMsgOnFail":false,"keepMsgProperties":false,"x":830,"y":420,"wires":[["30459f25.0f9838"],[]]},{"id":"a590b4c1.0f80e","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"},{"p":"coil","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":310,"y":320,"wires":[["ebde8026.c14da8"]]},{"id":"30459f25.0f9838","type":"debug","z":"ddb94f08.dac8a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":400,"wires":[]},{"id":"ebde8026.c14da8","type":"function","z":"ddb94f08.dac8a8","name":"FC6","func":"// get current status array\nlet deviceStatus = flow.get(\"deviceStatus\");  \n\n// what coil to update ..  must be sent in msg.coil as a number \nlet coil = msg.coil;\n\n// make string to array\ndeviceStatus = deviceStatus.split('');\n\n// update x coil status in array based on payload 0 or 1\ndeviceStatus[coil] = msg.payload;\n// make it to a string AGAIN !!!\ndeviceStatus = deviceStatus.join('');\n\n// update status for Context so node-red will know the status\nflow.set(\"deviceStatus\", deviceStatus);     \n\n\n// make the string to a decimal\nlet dec = parseInt(deviceStatus, 2)\n\n\n// prepare msg for Modbus flex write\nmsg.payload =  {\n    value: dec, \n    'fc': 6, \n    'unitid': 1, \n    'address': 0,\n    'quantity': 1 \n};  \n\n // send msg to Modbus flex write\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":420,"wires":[["7ed14e5.f2503b"]]},{"id":"ef4575de.037b28","type":"comment","z":"ddb94f08.dac8a8","name":"Update Context and Prepare Write","info":"","x":760,"y":320,"wires":[]},{"id":"3237f347.ed54b4","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"trigger","payloadType":"str","x":190,"y":140,"wires":[["4b202f6f.f4938"]]},{"id":"eb0f70eb.9b4c4","type":"debug","z":"ddb94f08.dac8a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1070,"y":140,"wires":[]},{"id":"4b202f6f.f4938","type":"function","z":"ddb94f08.dac8a8","name":"FC03","func":"msg.payload = { \n  //  value: msg.payload, \n    'fc': 3, \n    'unitid': 1, \n    'address': 0,\n    'quantity': 1 \n} \nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":140,"wires":[["1d12e1bf.a40bbe"]]},{"id":"1d12e1bf.a40bbe","type":"modbus-flex-getter","z":"ddb94f08.dac8a8","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"28c906aa.79aae2","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":620,"y":140,"wires":[["e471c7d9.aab7c"],[]]},{"id":"e471c7d9.aab7c","type":"function","z":"ddb94f08.dac8a8","name":"","func":"\n// convert to binary string and add leading zeros\nlet deviceStatus = msg.payload[0].toString(2).padStart(16, '0');\n\n// set Node-red context in memory so node-red can access the status\nflow.set('deviceStatus', deviceStatus)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":140,"wires":[["eb0f70eb.9b4c4"]]},{"id":"5818ac8a.bccd24","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"},{"p":"coil","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":310,"y":380,"wires":[["ebde8026.c14da8"]]},{"id":"5eac710f.091238","type":"comment","z":"ddb94f08.dac8a8","name":"Initial Read","info":"","x":340,"y":80,"wires":[]},{"id":"25f887fd.782f48","type":"comment","z":"ddb94f08.dac8a8","name":"Set Context","info":"","x":850,"y":80,"wires":[]},{"id":"a55a5106.4402b","type":"comment","z":"ddb94f08.dac8a8","name":"0","info":"","x":150,"y":360,"wires":[]},{"id":"d2c7c90d.c7ad5","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"},{"p":"coil","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":310,"y":460,"wires":[["ebde8026.c14da8"]]},{"id":"b8d89b7e.10ab6","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"},{"p":"coil","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":310,"y":520,"wires":[["ebde8026.c14da8"]]},{"id":"70365c01.0a213c","type":"comment","z":"ddb94f08.dac8a8","name":"1","info":"","x":150,"y":500,"wires":[]},{"id":"64793635.7d5c4","type":"inject","z":"ddb94f08.dac8a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":720,"wires":[["5ab93fcf.bff26"]]},{"id":"5ab93fcf.bff26","type":"function","z":"ddb94f08.dac8a8","name":"","func":"//get current status from memory\nlet deviceStatus = flow.get(\"deviceStatus\")  \n\nlet dec = parseInt(deviceStatus, 2);\n\nmsg.deviceStatus = deviceStatus;\nmsg.dec = dec;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":720,"wires":[["de14584b.b0e298"]]},{"id":"de14584b.b0e298","type":"debug","z":"ddb94f08.dac8a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":720,"wires":[]},{"id":"c011668.3824818","type":"comment","z":"ddb94f08.dac8a8","name":"test read - binary to decimal","info":"","x":480,"y":660,"wires":[]},{"id":"28c906aa.79aae2","type":"modbus-client","z":"","name":"TCP1","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.66","tcpPort":"502","tcpType":"TPC-RTU-BUFFERED","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"200","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

ps. the order of the binary may be wrong, in that case it may need to be reversed.

If you are new to Node-red there is a very good video playlist that goes throught the manipulation of messages here

2 Likes