0

Per the manual of the device I want to communicate with, the message should be structured like this:

address, functioncode, 0, StartRegister, 0, NumRegs, crc-L, crc-H

meaning that if the address is 1, function code is 04, start register is 0, and the number of registers is 1, then the message should look like this:

1, 4, 0, 0, 0, 1, 0, 0

The last two on the right are reserved for the crc values which I already know how to calculate and append.

What is the correct method from minimalmodbus that I can use to send this message successfully?

This is the code I have so far. It doesn't work.

import minimalmodbus
import serial

instrument = minimalmodbus.Instrument('COM1', 1)
instrument.serial.port
instrument.serial.baudrate = 19200
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.mode = minimalmodbus.MODE_RTU
instrument.serial.timeout = 1

data = [1, 4, 0, 0, 0, 1, 49, 202]
instrument.write(data)
respose = instrument.readline()
print (respose)
instrument.close()
  • Minimal modbus will do a lot of the work for you; the library will work out what needs to be sent (including the CRC). I believe `read_register(0, 0, 4)` will issue the request you want. See [the docs](https://minimalmodbus.readthedocs.io/en/stable/usage.html#typical-usage) for further info. – Brits Aug 22 '22 at 20:31
  • If you turn on [debug mode](https://minimalmodbus.readthedocs.io/en/stable/debugmode.html?highlight=crc#debug-mode) `instrument = minimalmodbus.Instrument('COM1', 1, debug=True)` you'll see the bytes written to the serial port printed on the console, the last two will be the CRC. – import random Aug 23 '22 at 08:01
  • how do I make sure read_register(0, 0, 4) is sending the crc values? – Isaac Zaiek Aug 23 '22 at 15:43
  • Well that is what the function does... You could turn on debug mode (see comment above) and it will log the data sent/received. – Brits Aug 23 '22 at 20:03

0 Answers0