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

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 shows the "command format" as:

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.

#!/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:

[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:

x8, b8, b8, b8, b16, b16, b16, b16, l32

Which I then send a json msg.payload:

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

And the flow...

[{"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.

Why not simply use the modbus nodes? The CRC calculations are all taken care of under the hood.

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.

[{"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.

1 Like

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