# Talking to a modbus 8CH relay - how to use node-red-contrib-binary & crc?

**URL:** <https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279>\
**Category:** General\
**Created:** [17 January 2021 18:07 UTC](https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279 "2021-01-17T18:07:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![IoTPlay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/iotplay/32/40_2.png) [@IoTPlay](https://discourse.nodered.org/u/IoTPlay)\
**Post date:** [17 January 2021 18:07 UTC](https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279/1 "2021-01-17T18:07:45Z")

</div>

Hello all, have a great 2021!

I am trying to incorporate modbus into my Node-RED projects. I have no prior knowledge of modbus, etc.

I bought an 8 ch relay, with the USB dongle from WaveShare, which I finally got working on RPi. It's wiki - [protocol manual](https://www.waveshare.com/wiki/Protocol_Manual_of_Modbus_RTU_Relay) shows the "command format" as:

```auto
Byte 1：Address
Byte 2：Function code
Byte3 4：Address of register (big-endian)
Byte 5 6：data of register (big-endian)
Byte7 8：CRC cheksum (little-endian)

```

I got the WaveShare's testing programme - in python, working - adapted it to switch the relays. For instance, the below python switches channel 8 relay to ON.

```auto
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import serial
import pycrc
import time

s = serial.Serial("/dev/ttyUSB0",9600)    
cmd = [0, 0, 0, 0, 0, 0, 0, 0]

cmd[0] = 0x01 #Device address
cmd[1] = 0x05 #command   

#CH8 On
cmd[2] = 0
cmd[3] = 7
cmd[4] = 0xFF
cmd[5] = 0
crc = pycrc.ModbusCRC(cmd[0:6])
cmd[6] = crc & 0xFF
cmd[7] = crc >> 8
print(cmd)
s.write(cmd)

```

The last command above, writes the following to the screen:

```auto
[1, 5, 0, 7, 255, 0, 61, 251]

```

Now I want to use the **node-red-contrib-serial-modbus** nodes instead of this python test sript, the **modbus-Serial Out** node, which I see should be used with the **node-red-binary** node, with code in the node to convert the inbound msg.payload to binary.

Below is what I have tried. I added in the binary nodes's pattern the following:

```auto
x8, b8, b8, b8, b16, b16, b16, b16, l32

```

Which I then send a json msg.payload:

```auto
[1,5,0,7,255,0,61,251]

```

And the flow...

```auto
[{"id":"6019d48c.a6cf5c","type":"binary","z":"278c3ffc.fa40d8","name":"","property":"payload","pattern":"x8, b8, b8, b8, b16, b16, b16, b16, l32","x":370,"y":240,"wires":[["37464190.6c351e"]]},{"id":"e96ac96d.c0ba78","type":"inject","z":"278c3ffc.fa40d8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,5,0,7,255,0,61,251]","payloadType":"json","x":170,"y":240,"wires":[["6019d48c.a6cf5c"]]},{"id":"37464190.6c351e","type":"modbusSerial out","z":"278c3ffc.fa40d8","port":"190f18f6.4715ff","slave":"0x01","start":"0","dtype":"coil","topic":"","name":"","x":610,"y":240,"wires":[]},{"id":"190f18f6.4715ff","type":"modbusSerialConfig","port":"/dev/ttyUSB0","baud":"9600","data":"8","parity":"none","stop":"1","name":""}]

```

Q1: How do I convert the above working python to something that will work with **node-red-contrib-serial-modbus**?  
Q2: I am also not sure how to **calculate crc** - it seams python has a crc function.

---

<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:** [17 January 2021 18:54 UTC](https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279/2 "2021-01-17T18:54:24Z")

</div>

Why not simply use the [modbus](https://flows.nodered.org/node/node-red-contrib-modbus) nodes? The CRC calculations are all taken care of under the hood.

---

<div class="post-metadata">

**Author:** ![IoTPlay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/iotplay/32/40_2.png) [@IoTPlay](https://discourse.nodered.org/u/IoTPlay)\
**Post date:** [18 January 2021 19:39 UTC](https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279/3 "2021-01-18T19:39:20Z")

</div>

Tried those modbus nodes first - could not get NR to talk to the USB, thus tried modbus-serial. But after your response, tried again - after having to write-up the above - and got it working. It's pretty neat !

the below flow writes an on, or off, to CH8, for those looking for answers.

```auto
[{"id":"8f456ed7.99bec","type":"modbus-write","z":"278c3ffc.fa40d8","name":"","showStatusActivities":true,"showErrors":true,"unitid":"","dataType":"Coil","adr":"7","quantity":"1","server":"bd8a159b.576a08","emptyMsgOnFail":false,"keepMsgProperties":false,"x":420,"y":120,"wires":[[],["e44af53e.daf17"]]},{"id":"e44af53e.daf17","type":"debug","z":"278c3ffc.fa40d8","name":"Response buffer","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":140,"wires":[]},{"id":"34b4617d.2eb7d6","type":"inject","z":"278c3ffc.fa40d8","name":"1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":80,"wires":[["8f456ed7.99bec"]]},{"id":"dad28564.a563f","type":"inject","z":"278c3ffc.fa40d8","name":"0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":140,"wires":[["8f456ed7.99bec"]]},{"id":"bd8a159b.576a08","type":"modbus-client","name":"","clienttype":"serial","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

```

Remember to install pyserial on your Pi.

---

<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:** [1 February 2021 19:39 UTC](https://discourse.nodered.org/t/talking-to-a-modbus-8ch-relay-how-to-use-node-red-contrib-binary-crc/39279/4 "2021-02-01T19:39:25Z")

</div>

Closed on 1 February 2021 19:39 UTC

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