Modbus RTU and TCP
Scope
Modbus is a communication protocol for industrial devices. The LoggerV uses Modbus to read and control inverters, meters, battery systems and other devices.
This article is for installers. It tells how Modbus operates, how the LoggerV uses Modbus RTU and Modbus TCP, and what the Modbus errors mean. For the RS485 wiring, see RS485 wiring for the LoggerV. For the RS232 and RS485 ports, see LoggerV serial and CAN ports.
Modbus Operation
Modbus uses a master and slaves. The master sends a request. The slave with the correct address sends a response. A slave does not send data without a request.
- Master: the LoggerV.
- Slave: the device, for example an inverter or a meter.
- Address: the Modbus Slave ID on Modbus RTU, or the Unit ID on Modbus TCP.
- Request: a function code, a register address and a quantity of registers or a value.
- Response: the data, or an exception code if the slave cannot do the request.
Registers
The device keeps its data in registers. Each register has an address and holds 16 bits. A value with more than 16 bits uses two or more registers.
- Holding register: a value that the master can read and write, for example a setpoint.
- Input register: a value that the master can only read, for example a measurement.
- Coil: one bit that the master can read and write, for example an on/off command.
- Discrete input: one bit that the master can only read, for example an alarm.
The device manual gives the register map. The LoggerV device driver contains the register map for each supported device.
Some manuals give register numbers that start at 1, or 5-digit numbers such as 40001. On the wire, the address starts at 0. Register 40001 in a manual is thus holding register address 0.
Function Codes
- 0x01: read coils.
- 0x02: read discrete inputs.
- 0x03: read holding registers.
- 0x04: read input registers.
- 0x05: write a single coil.
- 0x06: write a single register.
- 0x0F: write more than one coil.
- 0x10: write more than one register.
Modbus RTU
Modbus RTU sends the data as bytes on a serial line, RS485 or RS232.
- Frame: the Modbus Slave ID, the function code, the data and a CRC (a check value).
- End of a frame: a silent time of 3.5 characters on the line.
- Serial settings: the master and all slaves on one port must use the same baud rate, data bits, parity and stop bits.
- One request at a time: only one device transmits at a time on the line.
An RS485 port can have more than one device. An RS232 port has one device.
Modbus TCP
Modbus TCP sends the data in a TCP connection on an Ethernet network.
- IP address: the network address of the device.
- TCP port: 502 for most devices.
- Unit ID: the address of the slave in the device. Many devices use 1. Some devices ignore the Unit ID.
- Transaction ID: a number that connects each response to its request.
A Modbus TCP gateway changes Modbus TCP to Modbus RTU. The gateway has an IP address. The devices behind the gateway use RS485. The Unit ID in the request is the Modbus Slave ID of the RS485 device.
Modbus on the LoggerV
The LoggerV is the Modbus master. Each device has a device driver on the LoggerV. The driver reads the registers of the device at intervals.
For Modbus RTU, the default port settings, the response timeout, the gap between requests and the Modbus Slave ID rules are in RS485 wiring for the LoggerV.
For Modbus TCP:
- IP Address and TCP Port: set in the Add Device wizard. The default TCP Port is 502.
- Response timeout: 5000 ms. A LoggerV gateway can make this value longer.
- Shared connection: devices with the same IP address and TCP port use one TCP connection. The requests to these devices go one at a time.
After three failed requests in a row, the LoggerV shows the device as disconnected. The LoggerV then tries to connect to the device again.
Exception Codes
A device sends an exception code when it receives the request but cannot do it.
- 01 ILLEGAL FUNCTION: the device does not support the function code. The device model or the firmware version is possibly not correct for the driver.
- 02 ILLEGAL DATA ADDRESS: the register does not exist in the device. The firmware version of the device is possibly older or newer than the driver expects. This is the most frequent exception. If it occurs many times, record the firmware version and speak to support.
- 03 ILLEGAL DATA VALUE: the request has a value or a length that the device does not accept.
- 04 SERVER DEVICE FAILURE: the device has an internal error.
- 05 ACKNOWLEDGE: the device accepted a long command and is still busy with it.
- 06 SERVER DEVICE BUSY: the device is busy. The master must send the request again later.
- 0A GATEWAY PATH UNAVAILABLE: the gateway cannot find a path to the device. The gateway configuration is not correct, or the gateway has too many requests.
- 0B GATEWAY TARGET DEVICE FAILED TO RESPOND: the gateway sent the request on RS485, but the device did not answer. Examine the device, its Modbus Slave ID and the RS485 wiring.
Troubleshooting
A communication error occurs when the LoggerV does not receive a correct response.
- Timeout: the device did not answer before the response timeout. Examine the power, the wiring, the Modbus Slave ID or Unit ID and the serial settings. On an RS485 port, also examine the polarity, see RS485 wiring for the LoggerV.
- Timeouts behind a gateway: if all devices behind the gateway have timeouts, examine the gateway, its network connection and its RS485 wiring. If only one device has timeouts, examine that device, its Modbus Slave ID and its wiring.
- CRC error (RTU only): the response has errors in it. The cause is electrical noise, a baud rate that is not the same, no termination, or two devices with the same Modbus Slave ID. Examine the termination, the cable route and the baud rate.
- Received partial response: the response started but did not complete before the response timeout. The cause is electrical noise or a device that is too slow.
- Wrong function code: the response has a different function code from the request. Two devices possibly use the same address, or a gateway sent an old response.
- Exception response: the device sent an exception code. See Exception Codes.
- TCP connection closed (TCP only): the LoggerV connects again at the next request. Make sure that the device and the LoggerV are on the same network, and that the IP address is correct. Make sure that Modbus TCP is on in the device, on TCP port 502 or the port in the wizard.
- Transaction ID mismatch (TCP only): a gateway sent a late response to an old request. The LoggerV closes the connection and connects again.
- Search for device Fail!: the scan in the Add Device wizard did not find a device in the scan range. Examine the Modbus Slave ID, the port settings and the wiring.