Skip to main content

Modbus RTU and Serial

Two connector types work on serial lines (RS-232 and RS-485), either through a port of the WiConnector computer or through a serial device server on the network:

  • Modbus RTU: Modbus slaves such as meters, drives and temperature controllers.
  • Serial: devices with their own text or binary protocol, such as scales, barcode readers and analysers.

Serial connection (Modbus RTU and Serial)​

Connection settings of a Modbus RTU connector on a serial port

A Modbus RTU or Serial connector is one serial line: one port with its settings, and the devices on it. For two COM ports, create two connectors. The Connection section sets how to reach the line:

FieldMeaningDefault
Connect throughSerial port of this computer, or Serial device server (Moxa, Lantronix, USR and similar, set to raw TCP server mode)Serial port
Serial portCOM3 on Windows; /dev/ttyUSB0, /dev/ttyS0 or /dev/serial/by-id/… on Linux. Ports found on the WiConnector computer are listed under the field; click one to use it
Baud rate, Parity, Data bits, Stop bitsMust match the device exactly. Modbus RTU always uses 8 data bits; the usual setting is 9600 or 19200, Even or None, 1 stop bit9600 8N1
Flow controlNone for almost every device; RTS/CTS or XON/XOFF only if the device's manual asks for itNone
DTR on, RTS onHold these lines high. Some devices, and some RS-232 to RS-485 converters, need DTR or RTS to power or enable themoff
Device server host, TCP portAddress of the serial device server and the TCP port of its serial port (often 4001)
Reply timeout msHow long to wait for an answer1000
Delay between requests msExtra quiet time between requests. Try 5 to 20 ms when a slow device or an RS-485 converter misses requests0
Retry delay msWait before reopening a port that failed, or asking a device that stopped answering5000

On Linux, prefer the /dev/serial/by-id/… name of a USB adapter: it stays the same when the adapter is plugged into another USB socket, whereas /dev/ttyUSB0 and /dev/ttyUSB1 can swap after a restart. The service user needs to be in the dialout group. In Docker, pass the device to the container (see Getting started).

When the port cannot be opened (unplugged adapter, wrong name, in use by another program), every tag of the connector turns Bad with reason NotConnected, the log says why, and WiConnector tries again every retry delay.

Modbus RTU slaves​

One row per slave on the line. Several slaves share one line; WiConnector asks them one at a time.

A Modbus RTU connector with three slaves behind a serial device server

FieldMeaningDefault
NameHow tags refer to this slave, e.g. meter1
Slave IDThe slave address set on the device, 1 to 247. Every slave on one line needs its own1
Word orderAs for Modbus TCP; a single tag can override itHigh first
Max regs/readLargest block read in one request (1 to 125). Many serial devices accept less than 125; lower it if a slave answers illegal data address64
Timeout msReply timeout for this slave only; 0 uses the connection's0

Tag addresses are the same as for Modbus TCP (HR:100, 40001, …), see Tags.

A slave that does not answer turns its tags Bad with reason NoResponse. After two missed polls WiConnector leaves it alone for the retry delay, so one dead slave does not slow down the others on the line. A slave that refuses a request (wrong address, for example) turns only those tags Bad with reason DeviceException. A bad CRC in a reply (often wrong baud rate or parity, or noise on the cable) counts as no answer. A tag with Encoding BCD whose register is not BCD (a hex digit above 9) is Bad with reason NotBcd; see Tags for the Encoding option.

Serial devices​

A Serial connector for a scale, with its message settings

For devices that speak their own protocol: a scale that answers W with its weight, a barcode reader that sends each code it reads, an analyser with a READ? command. Tags say what to send and how to find their value in the reply (see Tags). The Messages section says how replies are cut into messages (frames):

FieldMeaningDefault
A reply endsWith a terminator (most text devices), After a fixed length, or When the line goes quiet (works for any device, binary ones too)With a terminator
TerminatorEnd of each reply: \r\n (CR LF), \r, \n, \x03 (ETX) …\r\n
Frame length bytesFor fixed-length replies
Silence msFor "when the line goes quiet": a gap this long ends a reply50
Start markerOptional start of each reply, e.g. \x02 (STX); bytes before it are droppednone
Request terminatorAdded to every request and write. \r\n for most text devices; empty for binary protocols\r\n
Max frame lengthA reply longer than this is thrown away as noise1024
Listen timeout msTags that listen (see below) turn Bad with reason NoData when nothing arrives for this long. 0 keeps the last value, right for a barcode reader that is quiet most of the time0

Control characters are written \r \n \t \0 \e or \xHH for any byte.

Devices: usually one per connector (one RS-232 cable, one device). Add more only for several instruments on one RS-485 line that are told apart by an address in the request: the device's ID replaces {id} in tag requests, so all instruments can share the same tag addresses or template.

Tags with the same request on the same device and poll group share one request per poll: a weight, its unit and its stable flag from one W cost one question to the scale. Tags that listen do not send anything; they update whenever the device sends a matching message on its own.

When a device does not answer a request within the reply timeout, its tags turn Bad with reason NoResponse. When a reply has no value for a tag, the reason says why: NotANumber, NoMatch, NoSuchField, ShortFrame, UnexpectedReply or OutOfRange. System Configuration → Logs, at Trace level for the connector, shows every request and reply with control characters visible.

Serial ports in Docker​

A container sees no serial ports unless they are passed in. In docker-compose.yml, add the adapter to the server service:

devices:
- /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A1B2C3-if00-port0:/dev/ttyUSB0

and choose /dev/ttyUSB0 on the Connectors page. Use the /dev/serial/by-id/… name on the host (run ls /dev/serial/by-id to see it): it stays the same when the adapter moves to another USB socket. On Docker Desktop for Windows or Mac, USB serial ports cannot be passed to containers; run WiConnector directly on the computer, or use a serial device server.

When WiConnector runs directly on Linux, the user it runs as must be allowed to open serial ports: sudo usermod -aG dialout <user>, then log in again.