How-to
Serial Modbus communication between a Linux SBC and a PLC
Short answer
A Linux SBC exchanges serial Modbus with a PLC when the PLC's documented mode, role and line settings match at both ends: spec default 19200 baud 8-E-1, or no parity with 2 stop bits; RS-485 terminated with 120 Ω at both ends; frames separated by 3.5 character-times of silence. Prove the link with one holding-register read before any write, and set the timeout from the measured round trip.
On this page
Serial Modbus between a Linux SBC and a PLC works when three documented things match: the PLC's Modbus mode and role, the serial line settings, and the register addressing. Processor speed plays no part; any Linux board keeps up with a link running at 9600 or 19200 baud. Copy the PLC's configured values verbatim, wire the bus properly, and prove the link with a single holding-register read before you send any write. This article lists the settings to collect, the wiring and frame-timing rules with the arithmetic, the bring-up steps in order, and the timeout decision worked out for a 9600-baud poll.
Decide role and mode before any code #
The PLC's serial port decides everything. Its manual must document Modbus RTU or ASCII service on that port, at a configured station address, in a stated role. For data acquisition the natural split is the SBC as client issuing requests and the PLC as server answering them. If the controller's serial firmware only speaks the programming protocol or can only act as client, no Modbus library will fix it: put a protocol gateway between them, translating serial Modbus on the SBC side into the protocol the PLC actually accepts. A gateway works, but it adds a second data map to configure and one more hop of latency.
Mode matters at the byte level. RTU and ASCII frame the same data differently — RTU sends raw 8-bit characters protected by a CRC, ASCII sends 7-bit characters protected by an LRC — and a device set to one mode silently ignores the other even at an identical baud rate. RTU is the usual choice; pick ASCII only when the PLC manual states it.
Collect these documented values before touching the SBC:
| Setting | Where it is documented | What to set |
|---|---|---|
| Modbus mode (RTU or ASCII) | PLC serial-port manual | RTU unless the manual says ASCII |
| Role | PLC serial-port manual | PLC as server, SBC as client |
| Station address | PLC port configuration | Exactly the configured address |
| Baud rate | PLC port configuration | Exactly the configured value |
| Parity and stop bits | PLC port configuration | Exactly the configured values |
| Register map and address convention | PLC Modbus data map | Read-only first |
Match the serial line settings #
The Modbus over Serial Line specification sets a default of 19200 baud, 8 data bits, even parity and 1 stop bit, and requires 2 stop bits when parity is none — a rule the spec has carried since its first release. With either compliant setting the character on the wire is 11 bits: one start, eight data, and two bits of parity plus stop between them.
Installed PLCs are frequently configured 9600 8-N-1 instead, a field convention that departs from the spec's no-parity rule and works only because both ends were set identically.
The decision: copy the PLC's configured baud, parity and stop bits verbatim into the SBC's port. When you configure both ends yourself, use 19200 8-E-1 (eight data bits, even parity, one stop bit), or no parity with 2 stop bits. A single-bit mismatch in any of the three returns silence, not an error message.
Wire the electrical layer #
The connector shape tells you nothing; the PLC manual names the physical layer. RS-232 is point-to-point, one SBC to one PLC, over roughly 15 m. RS-485 is differential, half-duplex and multidrop: one client polls many servers over one twisted pair, with the RS-485 standard's guideline reaching 1200 m at reduced speed. Most PLC Modbus ports are RS-485. A bare processor UART is 3.3 V logic and speaks neither standard — wiring it directly to a plant port destroys the pin, so every connection goes through a proper transceiver or converter.
Follow the manual's pin table for A/B and signal reference. On termination: leave the bench cable unterminated, but on an installed run put 120 Ω across A and B at both physical ends of the segment. Half-duplex direction: a USB adapter or an industrial port drives its transmit-enable in hardware; a board-support UART with a separate direction pin must release it as soon as the request's last byte leaves the wire, or the first bytes of the reply are truncated.
A dev board with a USB adapter proves the protocol on the bench; when you specify the computer for the cabinet, choose a fanless industrial mini PC with the RS-485 port documented in its manual instead of a dongle on a desktop board.
Respect the frame timing #
The Modbus over Serial Line specification separates frames with a silent interval of at least 3.5 character times and declares a frame broken if 1.5 character times elapse between its bytes. At 9600 baud with 11-bit characters, one character lasts 1.146 ms, so the frame gap is 3.5 × 1.146 = 4.01 ms and the intra-frame limit is 1.72 ms. At 19200 baud the gap is 2.0 ms. Above 19200 baud the spec stops scaling and fixes the gap at 750 µs and the intra-frame limit at 1.75 ms.
Two Linux realities eat this budget. A userspace write can be split by the scheduler, inserting a gap that breaks the intra-frame limit — write each frame with a single write call and confirm the driver buffers it whole. And a direction pin released late shortens the required silence. When bytes arrive frayed or not at all, a Linux Modbus RTU frame receiver built around the 3.5 character timing shows how to measure the actual gaps on the wire.
Keep the turn order strict: one transaction outstanding at any time, and the next request only after the reply or the timeout, plus the frame gap.
Bring the link up #
- Collect the documented settings from the PLC project: mode, role, station address, baud, parity, stop bits, and the register map with its address convention.
- Wire and terminate the segment, and confirm nothing else holds the SBC's serial device — a serial console or a logger on the same port silently consumes bytes.
- Install a maintained Modbus library; libmodbus is a C library for GNU/Linux that provides client and server implementations plus the serial configuration layer. Open the serial context with the device path (a USB adapter typically appears as /dev/ttyUSB0), baud, parity, data bits, stop bits, RTU mode and station address. A successful open proves access to the port, not communication with the PLC.
- Read exactly one holding register whose value you can watch in the PLC project — a status word or counter you already see change. Send a read-holding-registers request (function 03), log the raw reply bytes, the elapsed time and any exception, and keep writes disabled.
- Decode and compare: check signedness (a 16-bit register read as unsigned shows 65535 where the PLC means -1) and byte or word order on any 32-bit pair.
- Extend the reads register by register until every value matches the PLC; only then enable a write, and re-read after each write to confirm what the PLC stored.
- Fix the timeout and poll period from the measured round trip, as the worked example below shows.
Resolve the address convention #
Wire addresses in Modbus run from 0 to 65535. PLC-style documentation numbers holding registers from 40001, a display convention rather than a wire address: 40001 corresponds to address 0. If your library is 0-based and the map lists 40001, request address 0. The proof costs one request: read the candidate address of a register whose value you know, and if the wrong neighbour comes back, shift the convention by one.
Worked example: one register at 9600 baud #
Take a single read-holding-registers poll at 9600 baud, 8-E-1. Every character carries 11 bits, so a character lasts 11 / 9600 = 1.146 ms and the required frame gap is 3.5 × 1.146 = 4.01 ms. The request (station address, function, register address, quantity, CRC) is 8 bytes = 88 bits, occupying the wire for 88 / 9600 = 9.17 ms. The reply (station address, function, byte count, two data bytes, CRC) is 7 bytes = 77 bits = 8.02 ms. Wire time per transaction: 17.2 ms.
Add the PLC's answer time. As a stated example input, assume the PLC commits its reply within two scans of a 10 ms cycle, or 20 ms. Worst-case transaction: 17.2 + 20 = 37.2 ms. Set the master timeout at 100 ms, about 2.7 times the worst case — it absorbs a scan overrun without sluggish error detection. Poll once per 200 ms: bus duty is 37.2 / 200 = 18.6 percent, and the silence between transactions is about 163 ms, far above the required 4.01 ms.
A field-style 8-N-1 character is 10 bits instead of 11, which trims about 9 percent from every figure above and leaves the same decisions standing. Decision for this link: 9600 8-E-1 or the PLC's configured values at both ends, 120 Ω at both ends of the run, timeout 100 ms, poll period 200 ms, one outstanding request. If you measure the PLC's answer time at 50 ms instead of the assumed 20, the transaction becomes 67.2 ms and the same 100 ms timeout still holds with less margin.
Reading the failures #
| Symptom | What it means | Check first |
|---|---|---|
| Port will not open, permission denied | Linux layer | Device path, dial-out group membership, another process holding the port |
| Request out, silence back | Line settings or wiring | Baud, parity, stop bits, station address, A/B swapped, RTU vs ASCII |
| Reply bytes but CRC errors | Signal quality | 120 Ω termination, direction-pin release timing, shield and cable length |
| Valid exception reply | The link is working | The request itself: function, register area, quantity |
| Clean reply, wrong value | Interpretation | Address convention, signedness, word order on 32-bit pairs |
A valid exception reply is progress: framing, settings and timing all match, and the request content is what is wrong. For the silence case, troubleshooting Modbus RTU CRC, frame timing and baud mismatch walks these symptoms in order, and diagnosing an edge gateway that is online while the PLC stays offline covers the layer-by-layer method when more of the chain is involved.
From first read to production #
Once every mapped register has come back correct for a full shift, treat the SBC as the acquisition node it is: timestamp values at the SBC, keep the poll period stable, and publish upward — the same edge box can carry PLC data onward, as in bridging OPC UA PLC data to MQTT with an edge PC. Then leave the serial settings alone: a port reconfiguration made for convenience elsewhere silences this link exactly the way a first-day mismatch does.
Questions asked
How many devices can share the one RS-485 Modbus segment?
One client initiates at a time on a serial Modbus segment. The Modbus serial specification allows station addresses 1 to 247, and the RS-485 standard supports 32 unit loads per segment with classic transceivers, more with modern low-load parts. The practical ceiling is poll duty, not the address range: at 9600 baud a one-register transaction with a 20 ms PLC response occupies the bus about 37 ms, so five slaves polled once per second keep the segment under 19 percent busy.
Can one request read more than one register?
Yes. A read-holding-registers request may ask for up to 125 registers in one reply. The request stays 8 bytes, but the reply grows to 5 bytes plus 2 per register: 125 registers means 255 bytes, which at 9600 baud with 11-bit characters occupies the wire for 255 × 11 / 9600 = 292 ms. Larger reads change the timeout and poll-period decisions, so recompute them the same way as the single-register example.
What timeout and frame gap apply at 19200 baud?
A character at 19200 baud lasts 0.573 ms with 11-bit characters, so the frame gap is 3.5 × 0.573 = 2.0 ms. The one-register request takes 4.58 ms on the wire and the reply 4.01 ms, so with the same 20 ms assumed PLC response the worst case is 28.6 ms. The 100 ms timeout from the 9600-baud example still fits; once you have measured the real answer time you can tighten it toward 50 ms.
Does libmodbus run on every SBC architecture?
libmodbus is a C library for GNU/Linux, so it builds from source with the board's own toolchain on ARM, x86 or any architecture Linux supports. It needs a working serial device node from the board support package and read-write permission for your user, nothing more; processor performance is irrelevant at serial baud rates. If the board exposes only a raw UART, add a transceiver or adapter for the electrical layer.
Can I connect the SBC's UART pins directly to the PLC's serial port?
Only if both ends share the same physical layer, which they almost never do. A bare SoC UART outputs 3.3 V logic; an RS-485 port expects a differential transceiver, and an RS-232 port swings to roughly ±12 V, enough to damage logic pins on contact. Put a transceiver or converter between the UART and the plant port, and follow the PLC manual's pin table for A/B and signal reference before wiring.