How-to
Writing a Modbus RTU master on an Omron CPM2 in ladder
Short answer
Yes, but you write it yourself: the CPM2's ports offer no Modbus mode, so the master runs in no-protocol mode — TXD(48) sends the frame, RXD(47) collects the reply, ladder computes the CRC-16, and a 2 ms timer at 19200 baud enforces the 3.5-character silent interval. Worth doing on a running machine for monitoring and start/stop; on a rebuild, buy a CPU that speaks Modbus.
On this page
The CPM2 can be a Modbus RTU master — you write it yourself. Nothing in a CPM2A or CPM2C speaks Modbus: the serial ports run Host Link, NT Link or no-protocol mode, so the master is hand-framed in ladder. TXD(48) puts the request on the wire, RXD(47) collects the reply, a ladder routine computes the CRC-16, and a timer enforces the silent interval that separates RTU frames: 3.5 character times, which is 2.0 ms at 19200 baud. For monitoring soft starters and drives on a short bus, with occasional start/stop, that is a bounded job, and every piece of it is below. If the machine is being rebuilt rather than kept alive, skip to the last section: there the honest answer is to buy a CPU with Modbus built in.
What the CPM2 gives you #
A CPM2A carries two serial channels: an RS-232C port on a 9-pin D-sub, and a peripheral port that takes adapters — the CPM1-CIF01 for RS-232C devices, or the RS-422/485 adapter (CPM1-CIF11) when you need a two-wire multidrop trunk. The CPM2C reaches the same two roles through its option boards. One drive within a cable length: RS-232C point to point is enough. Several starters and drives on one pair: RS-485 through the CIF11, with the trunk built to the Modbus serial-line rules — shielded twisted pair, 120 Ω termination at both physical ends, up to 32 unit loads and 1200 m total.
The ports ship with Host Link and NT Link; Modbus RTU is not a firmware mode on this CPU. The documented path for talking to a standard serial device is no-protocol mode, where TXD(48) and RXD(47) move raw bytes — which is exactly what Modbus needs.
Set the port up for raw frames #
In the PLC settings for the port in CX-Programmer, select no-protocol mode, 19200 baud, 8 data bits, even parity, 1 stop bit, and disable the start and end codes: RTU frames have no framing characters, only silence. Give every slave on the trunk the same line settings and a unique address from 1 to 247; address 0 is broadcast and gets no reply. If the RS-485 adapter is wired for manual direction control, its transmit enable is asserted only while TXD(48) is shifting the frame and released before the turnaround — an adapter held in transmit blocks every reply on the bus.
Build the frame and the CRC #
| Field | Size | Notes |
|---|---|---|
| Slave address | 1 byte | 1–247; 0 broadcasts, no reply |
| Function code | 1 byte | 03 read holding registers, 06 write single register, 05 write coil, 16 write several |
| Data | 0–252 bytes | Depends on the function |
| CRC-16 | 2 bytes | Low byte first, then high byte |
Read ten holding registers from slave 1, with register 0000 as the first of them: the request is the six bytes 01 03 00 00 00 0A, the CRC over them is 0xCDC5, and the frame on the wire ends CD C5. Pack the frame into consecutive DM words two bytes per word, first byte in the low half, and give TXD(48) the byte count — eight for this request. Byte order inside the word is the classic first fault on a hand-built master; one look with a serial monitor on the bench settles it for your port.
The CRC routine, using example allocations (any free DM words do):
CRC accumulator : D0300, word, starts 0xFFFF
Byte index : D0301, word, starts 0
Frame length : D0302, word, 6 for the example request
Per byte, until D0301 = D0302:
1. XOR the frame byte into the low byte of D0300.
2. Eight times: shift D0300 right one bit;
if the bit shifted out was 1, XOR 0xA001 into D0300.
Append: low byte of D0300 first, then the high byte.
Bit-by-bit costs eight passes per frame byte — 48 passes for the example request — which is noise in a monitoring scan. Prove the routine on the test vector above before any drive sees it: six bytes in, 0xCDC5 out, CD C5 on the wire.
Run the master as a state machine #
One request in flight, one slave at a time, everything driven by a step word and timers. A shape that works:
- IDLE — the application writes slave, function, register and length into the request words and raises a start bit.
- BUILD — move the frame bytes into the transmit words, run the CRC routine, append it low byte first.
- SILENCE — verify the line has been quiet for the full interval since the last activity; this gate, not the scan, paces the bus.
- SEND — assert transmit enable if the adapter needs it, execute TXD(48) with the byte count, wait for its done flag.
- TURN — release transmit enable, restart the interval timer, empty the receive buffer, arm RXD(47).
- WAIT — compare the byte count RXD(47) reports against the expected reply: 5 + 2N bytes for a read of N registers, an 8-byte echo for a write. A timeout timer of 50–100 ms ends the wait.
- VERIFY — slave address matches, function code matches and does not carry the +0x80 exception flag, and the CRC recomputes over everything except the last two bytes.
- PARSE — move the register bytes into working DM and scale them: the drive hands you counts, and turning 0.01 Hz counts into hertz is the same linear math as scaling a 4-20 mA analog input to engineering units in CPM2 ladder.
- RETURN — clear the step word. On timeout, bad CRC or an exception: retry once, then flag that slave offline and move on; the scan never stalls on a dead port.
Broadcasts (address 0) skip steps 6 to 8: send, hold the silence, move on.
Hold the 3.5-character silent interval #
RTU frames are delimited by silence: the Modbus serial-line specification sets the frame boundary at 3.5 character times, and any gap longer than 1.5 character times inside a frame invalidates it at the receiver. That is why the whole frame goes to TXD(48) as one buffer — the port shifts it out back to back, while a ladder that dribbles bytes one scan at a time would be discarded.
The arithmetic, for the 11-bit characters of 8-E-1 (start, 8 data, parity, stop):
- 19200 baud: 11 ÷ 19200 = 0.573 ms per character; 3.5 × 0.573 = 2.0 ms.
- 9600 baud: 11 ÷ 9600 = 1.15 ms per character; 3.5 × 1.15 = 4.0 ms.
- Above 19200 the specification stops computing and fixes the interval at 1.75 ms.
Commit to 5 ms timers at 19200 (10 ms at 9600): double the requirement, and the cost — a few ms per exchange — is invisible on a monitoring bus. The margin is not decoration. A ladder timer starts when its rung is scanned, up to one full scan after the last byte actually crossed the wire, so the true gap is the timer setting minus up to one scan; 5 ms against 2.0 ms with a single-digit-ms scan never produces a short interval.
What one poll costs #
Worked example, the read from above — slave 1, function 03, ten registers, 19200 baud:
- Request: 8 bytes × 11 bits = 88 bits; 88 ÷ 19200 = 4.6 ms on the wire.
- Reply: 3 + 2×10 + 2 = 25 bytes; 25 × 11 = 275 bits; 275 ÷ 19200 = 14.3 ms.
- Two silent intervals at the committed 5 ms: 10 ms.
- Slave turnaround: budget 10 ms; drives answer in a few ms to a few tens.
Wire time ≈ 4.6 + 14.3 + 10 + 10 ≈ 39 ms. Add ladder latency: the state machine advances one step per scan, so assume a 10 ms scan and six steps ≈ 60 ms per exchange — then measure your scan and rescale. Five slaves polled round-robin refresh each one about every 300 ms.
That decides the fit. Monitoring, metering, operator-issued starts and a speed change a few times a second: comfortably inside budget. Anything the PLC must regulate every scan, or coordinated motion: wrong tool, and no timer tuning fixes it.
Prove it on the bench #
- Put a Modbus slave simulator on a PC with an RS-485 adapter and run the master against it: CRC vector, byte order, reply parsing, and the timeout path with the slave unplugged.
- Force each error once — timeout, corrupted reply, exception response — and watch the retry-then-move-on logic work before a starter is affected.
- Then connect one real drive with the machine unable to move, and confirm register values against the drive's own display.
And the one non-negotiable rule: emergency stop and protective interlocks stay hardwired in the starter's control circuit. A Modbus run command is a control command, not a safety function — a dead port, a lost frame or a half-written register must never remove a safeguard.
When to buy the protocol instead #
The CPM2 family is long discontinued, which caps how much engineering belongs on this port. The hand-built master earns its place while the machine runs and the job is monitoring plus start/stop on a handful of slaves. Three migrations remove the protocol work outright:
- A CJ-series CPU with a serial communications unit: framing, CRC and timing live in the module, and the ladder hands it requests. The module also serves the other direction — reading Modbus RTU writes into a CJ1W-SCU31-V1 slave area shows the data landing without a line of protocol code.
- A current compact CPU such as a CP2E supports the RTU master role with vendor function blocks. If you port TXD(48) code to one, the TXD sequential data transmission fix on the CP2E covers the sequencing difference you will meet.
- Any current small controller with native Modbus instructions makes the point by contrast: driving an ATV12 drive from a TM200 PLC over Modbus RTU is a block call, not a state machine.
Choosing among those families is its own question — selecting between Omron CP1, CJ, NX and NJ controllers lays out the trade — but the dividing line is simple: keep the CPM2 while the machine runs, replace the CPU when the machine is rebuilt.
Questions asked
Can the same code make the CPM2 a Modbus RTU slave instead of a master?
Yes — the framing, the CRC routine and the silent interval are identical — but a slave has a harder deadline. A master may time out if the reply does not start promptly, and the ladder scan sits between the last byte received and the first byte sent, so keep the scan short and pre-build the reply for every request you expect. For one slave on an installed machine it works; against a master with a tight timeout, use hardware built for the role.
The drive answered the first poll and never answered again. What broke?
Three causes cover most cases: the receive buffer was not emptied after the previous reply, so the next response is read behind stale bytes and its CRC fails; the next request went out before the silent interval expired; or transmit enable on the RS-485 adapter stayed asserted and blocked the reply. Clear the buffer in the turnaround step of every exchange and time the line from the last byte seen, not from the scan.
Should the bus run at 9600 or 19200 baud?
Run 19200 unless a device on the trunk is fixed at 9600. At 19200, with 11-bit characters, the silent interval works out to 2.0 ms and the ten-register read in the article needs about 19 ms of pure transmission; at 9600 both double, to 4.0 ms and about 38 ms. Five drives still refresh well under a second per pass at either rate, so the slowest slave sets the baud, not performance.
How do I verify the CRC routine before connecting a drive?
Compute the routine over the six bytes 01 03 00 00 00 0A. The accumulator must end at 0xCDC5, and the frame goes on the wire as 01 03 00 00 00 0A CD C5. A PC with a serial monitor on the port is the second check: the bytes on the wire must match the bytes in your DM buffer exactly. If either test fails, fix the routine before a starter is connected.
Is a Modbus start command acceptable for starting the soft starter?
As a control command, yes. As a safety function, no. Emergency stop and protective interlocks belong hardwired in the starter's control circuit, independent of the PLC and the serial link, so a lost frame or a dead port cannot remove them. The Modbus run command should only ever do what a hardwired start button would do, in series with those protections.