I have a problem with communication via Modbus RTU. First of all sorry for my English ;):
- I’m using RS485 Pi SN65HVD72 to connect with MultiCONT PRD-28-2. I made everything like is in this instruction: https://www.abelectronics.co.uk/kb/article/1035/raspberry-pi-3-serial-port-usage
config.txt changes:
- toverlay=pi3-miniuart-bt
- enable_uart=1
After this steps the config of my /dev looks like this:
- lrwxrwxrwx 1 root root 7 gru 30 15:03 serial0 -> ttyAMA0
- lrwxrwxrwx 1 root root 5 gru 30 15:03 serial1 -> ttyS0
- In MultiCONT PRD-28-2 I change RS485 options to:
- Protocol: Modbus
- Parity: ODD
- Stop bit: 1 bit
- Python code:
from pymodbus.client.sync import ModbusSerialClient as ModbusClient import logging FORMAT = ('%(asctime)-15s %(threadName)-15s' ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s') logging.basicConfig(format=FORMAT) log = logging.getLogger() log.setLevel(logging.DEBUG) #multicont = ModbusClient(method='rtu',port='/dev/serial0',stopbits=1,bytsize=8,parity='N',baudrate=9600,timeout=1,strict=False) multicont = ModbusClient(method='rtu',port='/dev/serial0',stopbits=1,bytsize=8,parity='O',baudrate=9600,timeout=1,strict=False) connection = multicont.connect() print(connection) value = multicont.read_coils(1,15,unit=1) log.debug(value)
- Error message:
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
- Logs:
2020-12-30 16:24:51,992 MainThread DEBUG transaction :118
Current transaction state – IDLE
2020-12-30 16:24:51,992 MainThread DEBUG transaction :123 Running transaction 1
2020-12-30 16:24:51,992 MainThread DEBUG transaction
:230 SEND: 0x1 0x1 0x0 0x1 0x0 0xf 0x2d 0xce
2020-12-30 16:24:51,993 MainThread DEBUG sync :75 New Transaction state ‘SENDING’
2020-12-30 16:24:51,993 MainThread
DEBUG transaction :239 Changing transaction state from ‘SENDING’ to ‘WAITING FOR REPLY’
2020-12-30 16:24:52,995 MainThread DEBUG transaction :253 Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received))
2020-12-30 16:24:52,995 MainThread DEBUG rtu_framer :241 Frame – [b”] not ready
2020-12-30 16:24:52,996 MainThread DEBUG
transaction :409 Getting transaction 1
2020-12-30 16:24:52,996 MainThread DEBUG transaction :204
Changing transaction state from ‘PROCESSING REPLY’ to ‘TRANSACTION_COMPLETE’
2020-12-30 16:24:52,996 MainThread
DEBUG communication :20 Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
Advertisement
Answer
PyModbus is not receiving a response.
If you have access to an oscilloscope, this is the moment to use it. There are soooo many things that can go wrong with Modbus-RTU.
Make sure you know what you are doing with the electrical wiring, familiarize yourself with RS485 biasing and termination.
Possible causes:
- request sent to wrong serial device/interface
- device uses different baudrate or serial settings
- device can hear you allright but has a different slave ID configured, so it doesn’t respond
- wrong wiring, grounding issues
- RS485 electrical problems, most notably the master must be biasing the bus, usually via a resistor that you have to solder/enable
- Bus termination resistors are required for correct voltage levels (sometimes they are already built-in, more often you need to enable them, with some luck may work without)
- Master still driving the RS485 bus while the slave is responding. (If you do not have a dedicated RS485 driver, the bus may end up being asserted/deasserted by unreliable software timers. Some devices allow you to configure a response delay as a workaround.)
- Master not driving the bus at all. (Working in RS232 mode instead of RS485 mode.)
- Also, Linux might have ttyAMA0 configured as a serial console for boot/kernel messages. (This is unlikely to cause your current problem, just something else to watch out for.)
Final piece of advice: if you have the option to use Modbus-TCP instead of Modbus-RTU, do that instead. This replaces all the RS485 electrical headaches which IP-configuration headaches, for which you can use over-the-counter medication.