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.