IMDHMIControls LibraryShop main store

Explainer

Modbus RTU CRC16 in ladder on an Omron CPM2A

Industrial networks Published 9 min read By IMDHMI controls team

Short answer

A CPM2A can generate the Modbus RTU CRC16 in ladder because the algorithm reduces to XOR and right shifts: initialize the accumulator to FFFF hex, XOR each frame byte, shift right eight times per byte, XOR with A001 hex when a shifted-out bit is 1, then append the result low byte first. The FCS instruction is an ASCII XOR checksum for Host Link and cannot substitute.

On this page
  1. Can an Omron CPM2A calculate the Modbus RTU CRC16 in ladder?
  2. Which bytes of the frame go into the CRC16?
  3. What does the Modbus RTU algorithm specify, exactly?
  4. Why can't the CPM2A's FCS instruction do the job instead?
  5. Should you build the bitwise loop or a lookup table?
  6. How do you prove the ladder CRC is right before relying on it?
  7. Where do the finished CRC bytes go in the frame?
  8. Which CRC build should you commit to on a CPM2A?
  9. Questions asked

If a Modbus RTU device ignores requests from an Omron CPM2A, suspect the frame check first: the CPM2A has no instruction that computes the RTU CRC16 for you, so the calculation belongs in a ladder subroutine you build once and call before every send. The algorithm looks forbidding in hexadecimal but reduces to operations the CPM2A instruction set already has — an exclusive-OR of each frame byte into a 16-bit accumulator, eight right shifts per byte, and one conditional exclusive-OR with a fixed constant. Preset the accumulator, run every covered byte of the frame through that loop, then append the result after the data, low byte first. The FCS instruction is not a shortcut — it is Host Link's check, not Modbus's. This article gives the byte coverage, the exact algorithm with arithmetic, the FCS trap, the bitwise-versus-table decision, and a test frame that proves your ladder.

Can an Omron CPM2A calculate the Modbus RTU CRC16 in ladder? #

Yes — the RTU frame check is nothing but exclusive-ORs, eight right shifts per byte, and one conditional exclusive-OR with a fixed 16-bit constant, and every one of those operations exists in the CPM2A instruction set, so a subroutine of a few dozen rungs generates it.

What you need, named by what each element does: a wordwise exclusive-OR instruction, a right-shift instruction that moves the accumulator's lowest bit into the carry flag where a rung can branch on it, word comparison, counters, and indexed addressing into the data memory (DM) area. Structure the work as a subroutine with two loops: an outer byte loop driven by a pointer and a byte counter, and an inner bit loop driven by a bit counter with the conditional XOR step inside it. Unrolling the bit loop eight times removes the inner loop and shortens execution at the cost of extra rungs; either structure works.

Call the subroutine once per frame, after the address, function and data bytes are all in the transmit buffer and before the rung that fires the port. The frame assembly side — port settings, buffers and send timing on this family of PLCs — is its own task, covered in writing a Modbus RTU master on an Omron CPM2 in ladder; this article stays on the checksum itself.

Which bytes of the frame go into the CRC16? #

The calculation covers every transmitted byte from the slave address through the final data byte, in transmission order, and excludes the two CRC bytes themselves, so a six-byte read request means exactly six passes through the routine and nothing more.

In an RTU frame those bytes are the slave address, the function code and the data field. The example used throughout this article is a read of two holding registers from slave 1, so the covered bytes are the six 01 03 00 00 00 02.

Frame field Example bytes CRC treatment
Slave address 01 First byte processed
Function code 03 Second
Register address 00 00 Processed in transmitted order
Register count 00 02 Last bytes processed
CRC C4 0B Produced by the routine, never part of its input

Most defects at this stage are boundary defects rather than math defects: the byte count includes a stale CRC pair from the previous frame, the routine walks DM words instead of bytes and reads the wrong half of each word, or the byte order inside each word does not match what the port actually sends. Freeze one request during commissioning, write down its start word, its byte count and which byte of each word goes out first, and make the subroutine's input count equal that written list byte for byte.

What does the Modbus RTU algorithm specify, exactly? #

The Modbus over Serial Line specification fixes four choices: preset the accumulator to FFFF hex, exclusive-OR each byte into it, shift right eight times per byte, and exclusive-OR with A001 hex — the reflected form of the 0x8005 polynomial — whenever a shifted-out bit is 1.

As rungs: load 0xFFFF into the accumulator; for each frame byte, XOR the byte into the accumulator; then, eight times, test the lowest bit — if it is 1, shift the accumulator right and XOR it with 0xA001, and if it is 0, shift right only. The constant 0xA001 is the bit-reversed image of the polynomial 0x8005, and that reversal is what lets each byte enter the accumulator in a single XOR with no pre-shuffling. Both constants hold on every RTU device, and the knowledge-base page on the Modbus RTU CRC-16 preset and polynomial shows the bit-level derivation if you want to see why the reflected form works.

Worked pass for the first example byte, 0x01:

  • XOR step: 0xFFFF XOR 0x0001 = 0xFFFE
  • Shift 1: lowest bit 0, shift only → 0x7FFF
  • Shift 2: bit 1 → shift to 0x3FFF, XOR 0xA001 → 0x9FFE
  • Shift 3: bit 0 → 0x4FFF
  • Shift 4: bit 1 → 0x27FF, XOR 0xA001 → 0x87FE
  • Shift 5: bit 0 → 0x43FF
  • Shift 6: bit 1 → 0x21FF, XOR 0xA001 → 0x81FE
  • Shift 7: bit 0 → 0x40FF
  • Shift 8: bit 1 → 0x207F, XOR 0xA001 → 0x807E

After the first byte the accumulator holds 0x807E; the proof section below shows the value after every remaining byte of the example frame. Copy these eight shift decisions into the subroutine's rung comments — when a computed value diverges later, the defect is almost always one missing or extra conditional XOR inside this loop.

Why can't the CPM2A's FCS instruction do the job instead? #

It cannot, because the FCS instruction is Omron's Host Link check — the ASCII codes of the frame's characters XORed together and rendered as two ASCII hex digits — while RTU needs a 16-bit CRC over binary bytes, so an FCS-swapped frame fails every Modbus slave's check.

Host Link is the ASCII command set the CPM2A's serial port speaks toward programming software and host computers, and Omron's programming manual for the CPM1, CPM1A, CPM2A, CPM2C and SRM1 documents the FCS instruction as that protocol's frame check. The differences that matter:

  • Input domain: Host Link ASCII characters versus RTU binary bytes.
  • Math: a plain XOR of characters collapses toward small values; the CRC16 runs a 16-bit shifted polynomial over every bit of every byte.
  • Output: two ASCII hex digits versus two raw binary bytes appended after the data.

In networking generally, FCS names the check field in a frame and CRC names the algorithm that fills it; on the CPM2A, FCS names one specific Host Link instruction. A request carrying FCS output in its tail is not a slightly wrong Modbus frame — no RTU slave will act on it.

Should you build the bitwise loop or a lookup table? #

Build the bitwise loop: it needs about five working words, while a table version must park 256 precalculated words of DM on the controller before it processes the first byte, and on a CPM2A that is a poor trade for one checksum.

The bitwise working set is the accumulator, the current byte, a byte counter, a bit counter and a buffer pointer — five DM work words plus the buffer itself. The table version stores, for each of the 256 possible byte values, the pattern that byte's eight shift-and-XOR steps produce; each frame byte then costs an index calculation and a table read instead of eight shift decisions, but the 256 words must sit in DM for the life of the program.

Decision: bitwise on a CPM2A. The six-byte example above costs 48 shift decisions and a 128-byte message costs 1,024, which still fits inside one scan between frame-complete and send at normal poll rates. If traffic grows until the loop shows up in scan time, slice the byte loop across several scans before you reach for the table. If you later port the routine to a larger C-series controller with DM to spare, the table shortens the per-byte work; either way, a step-by-step guide to implementing the 0xA001 polynomial is worth having open while you place the rungs.

How do you prove the ladder CRC is right before relying on it? #

Run one fixed test frame through the subroutine and compare against a known result: for the read request 01 03 00 00 00 02, the accumulator must finish at 0BC4 hex, and the pair appended after the data must read C4 then 0B.

Store the six bytes in a fixed block of DM words exactly as the port will transmit them, run the routine, and watch the accumulator after each byte. The correct chain is:

Byte XORed in Accumulator after that byte's eight shifts
01 807E
03 2140
00 F020
00 D8F1
00 8419
02 0BC4

Any other value means a boundary or shift-step defect: trace the pointer and the bit counter rung by rung to the first divergent byte, then compare that byte's eight decisions with the list in the algorithm section. Confirm the finished pair independently with a PC-side CRC-16 Modbus calculator on the same six bytes, or against the example CRC printed in the slave device's manual. One safety rule until the test passes: keep the routine on read requests only, because a boundary bug feeds the routine bytes you did not intend, and a write command carrying a valid CRC over the wrong data will execute at the slave.

Where do the finished CRC bytes go in the frame? #

Append them immediately after the last data byte, low byte first, so the slave reads the CRC's low byte in the position right after your data and then the high byte, followed by the closing silent interval that ends the RTU frame.

The example request therefore leaves the port as the eight bytes 01 03 00 00 00 02 C4 0B. The specification separates RTU frames with a silent interval of at least 3.5 character times — fixed at 1.75 ms for rates above 19200 bps — and that closing silence tells the receiver the frame ended. What the transmission must never contain is a gap inside the frame: if the port idles between the data bytes and the CRC pair, or anywhere else mid-frame, RTU receivers treat the burst as broken and discard it, the failure mode described in why a Modbus RTU frame gap before the CRC silences the slave. The transmit rung should therefore write the whole buffer, CRC pair included, as one continuous burst, and the CRC subroutine must have finished before that rung fires.

Which CRC build should you commit to on a CPM2A? #

Commit to one bitwise-shift subroutine: about five working words, called once after the frame bytes are complete and before the send rung fires, reused unchanged for checking received frames by comparing its result against the two received CRC bytes and rejecting the frame on any mismatch.

Per case:

  • Transmitting a request: bitwise loop from the address byte through the last data byte, result appended low byte first, send rung held until the subroutine completes.
  • Receiving a response: the same subroutine, unchanged, over the received bytes, its result compared against the stored pair.
  • Growing traffic: slice the byte loop across scans; keep the 256-word table off a CPM2A.
  • Any temptation to reuse FCS: drop it — different protocol, different math.

Initialize every working word on each call: a retained accumulator or counter between frames produces intermittent, length-dependent failures that look exactly like line noise. Keep the six-byte test frame and its 0BC4 result in the subroutine's rung comments, so the next person who opens the program can re-prove the routine in one scan.

Questions asked

Does the CPM2A have a built-in instruction that computes the RTU CRC for you?

No. Its serial port speaks Omron's Host Link natively, and the only check helper in the instruction set is the FCS instruction for Host Link frames. There is no RTU CRC generator on a CPM2A, so the calculation has to be coded in ladder as a subroutine or adapted from a tested example, which is why the bitwise method in this article exists.

What happens on the wire when the CRC bytes are wrong?

Nothing observable. A Modbus RTU receiver that fails the CRC check discards the frame without sending an exception response, so the only symptom at the master is its response timeout expiring. Debug from a serial capture that shows the exact bytes on the wire, and compare them byte for byte with the buffer the ladder assembled before changing any rung of the routine.

Can the same ladder routine serve Modbus ASCII mode instead?

No. ASCII mode replaces the binary CRC16 with an LRC, the two's complement of the 8-bit sum of the frame's characters, transmitted as two ASCII hex digits, with the frame itself delimited by a colon and a CR LF pair. The bitwise CRC subroutine is specific to RTU; an ASCII master needs a small summing routine instead.

Can the same subroutine also verify the CRC of frames the CPM2A receives?

Yes, unchanged. The RTU check is symmetric: run the same routine from the received slave address through the last received data byte, then compare its result with the two bytes stored after them in the receive buffer, in the same low-byte-first order you used to append them on transmit. On a mismatch the correct action is to discard the frame, not to answer it.

Keep reading

  • How-to

    SLC 5/03 CH0 Modbus RTU with the ASCII driver

    An SLC 5/03 channel 0 has no Modbus driver, only DF1 and ASCII, so configure the ASCII driver, build each Modbus frame as a string, and send it with AWT. Use the slave's Modbus ASCII mode with its one-addition LRC checksum where supported, precompute CRC-16 frames where it is not, and move to a protocol module beyond one slave and a few registers.

    Industrial networks

  • Troubleshooting

    Why a Modbus RTU frame gap before the CRC silences the slave

    Measure the idle time between the last request byte and the first CRC byte. Above 3.5 character times — 4.01 ms at 9600 baud with 11-bit characters — the slave ends the frame before the CRC, discards it without an exception reply, and treats the two CRC bytes as noise, so the master times out. The fix is a master driver that writes address, function, data and CRC in one continuous burst, not line tuning or a slower poll.

    Industrial networks

  • Troubleshooting

    CB1241 reads zero on Modbus RTU while the transmitter works

    A CB1241 that reads zero while a USB-to-RS-485 converter reads the same transmitter is failing on the PLC side, and TIA Portal hides the cause: MB_MASTER completes with an error and the next scan's retry clears STATUS. Latch STATUS into a memory word when ERROR goes true, then fix the parity code, the unit ID, the 40001-based register number and the RS-485 bus, and keep the converter off the bus.

    Industrial networks