Your machine must run a different routine for whichever part is in front of it: the part carries a DataMatrix, the controller reads the code, and the decoded value picks the subroutine. The reader is not chosen yet, which is an advantage — you can pick the interface the PLC side wants instead of adapting to whatever a scanner happens to output. This how-to builds the whole chain on an all-in-one PLC/HMI with a serial port: reader selection, payload configuration, trigger wiring, the new-read latch, the string compare, and the dispatch rung that calls one subroutine per code value, with timing math that shows where serial stops being enough.
The decision, up front
Four blocks do this job, and every platform that ships serial protocol and string instructions has them:
- A fixed code reader, hard-triggered, sending ASCII over RS-232.
- A protocol scan block — many platforms name it exactly Protocol Scan — that buffers incoming characters and copies them into a string when it sees the terminator.
- A new-read pulse: a one-shot that fires for one scan when a frame lands, so each code is handled exactly once.
- A dispatch: string compares against a table of known codes that load a selector integer, and one jump-to-subroutine rung per value.
Commit to the numbers: RS-232 ASCII carries this comfortably up to about 20 reads per second, payloads of a couple hundred characters, and 15 m of cable, the classic RS-232 limit. Past any one of those, read the results over Ethernet instead — the latch and dispatch logic below stays identical, only the transport changes.
The timing behind that threshold:
One character at 8 data bits, no parity, one stop bit is 10 bits on the wire, and the two scans are the worst case for the protocol block and the dispatch to each catch the frame.
What you need
- A fixed code reader with an ASCII serial output. The Interface filter in the code reader finder lists 28 Serial models, so the choice is real, not a compromise.
- The reader's RS-232 cable or its published pinout, and one free serial port on the controller.
- A part-present sensor or a spare PLC output to trigger the reader.
- The list of DataMatrix values the machine will see, and the subroutine each one must start.
Compact readers with built-in optics and illumination cover the short, fixed standoffs a machine needs. The LS-CRD-0047 is a 1.2 MP compact reader that reads from 60-120 mm over USB 2.0, for a host that takes USB devices.
- Resolution
- Up to 3 MP
- Shutter
- Global
- Interface
- USB
The LS-CRD-0052 is the same 1.2 MP class with a 12 mm focal length and an Ethernet interface, for a cabinet already wired for network drops.
- Resolution
- Up to 3 MP
- Shutter
- Global
- Interface
- Ethernet
At the high-rate end, the MV-ID6200PM-00C-NNG is a 20 MP logistics code reader with a Serial interface.
- Interface
- Serial
- Resolution
- 16-25 MP
- Sensor format
- 1"
- Shutter
- Global
Configure the reader payload before wiring
Set both ends to the same serial line: 115200 baud, 8 data bits, no parity, 1 stop bit. Then frame the payload:
- Program the reader to send an STX prefix and an ETX suffix on its serial output; that setting lives in the reader's own configuration tool or its programming barcodes.
- Match the protocol scan block's terminator to that suffix, character for character.
- Keep the payload disciplined: fixed-length values, or a known separator when one code carries several fields. Splitting barcode strings into lot and part fields then takes two extract instructions instead of guesswork.
STX (0x02) marks where the code starts, so leading noise bytes never shift your compare; ETX (0x03) tells the protocol block the frame is complete. What actually lands in the receive buffer — padding, nulls, partial frames at power-up — is a failure set of its own, covered in ASCII barcode data framing in the PLC receive buffer.
Wire the trigger line
Fixed readers expose a trigger input; handheld guns keep their own. You want the fixed kind here. Wire the part-present sensor to the reader's trigger input and, if the sequence needs it, to a PLC input in parallel so the controller knows a read was requested.
Hard trigger beats free-run for dispatch: the frame that arrives then belongs to the part about to be processed, so the new-read pulse means something physical. The trade-offs are laid out in fixed barcode reader trigger modes, and driving the input from controller logic is covered in triggering a code reader from a GPIO pin.
Program the PLC in six steps
Declare the working data first, with types and starting values:
NewRead BOOL := 0 one-scan pulse: a frame arrived
CodeString STRING := '' latched copy of the last frame
Selector INT := 0 0 = unknown, 1..n = routine to run
- Configure the PLC serial port to 115200 baud, 8N1, identical to the reader.
- Arm the protocol scan block on ETX. On the terminator it writes the received characters to a receive string and sets its frame-ready bit. How that ASCII settles into controller memory — bytes, words, clears — is mapped in storing ASCII barcode strings in PLC memory.
- On the rising edge of frame-ready, copy the receive string into CodeString and set NewRead for exactly one scan. Copying only on the edge is the whole trick: copy barcode data to a string only on a new-read handshake shows the handshake, and why a barcode string disappears after one PLC scan shows what skipping it costs.
- Compare CodeString against the table of known codes with the string compares from the platform's string function blocks. The first match writes its routine number into Selector; no match leaves it at 0.
- Add one dispatch rung per routine: NewRead AND (Selector = n) calls subroutine n. Nothing else may condition the call.
- Inside each subroutine, treat the calling pulse as the routine's first scan: reset per-cycle state at entry and leave no latches behind. First-run trigger and reset methods for PLC subroutines collects the standard patterns.
If the code embeds its routine number in a fixed field, skip the compare chain: an ASCII String Extract instruction (AEX on platforms that carry it) slices the field by start position and length, and a string-to-integer conversion loads Selector directly. The ladder patterns for the compare path, including demanding several equal reads before acting, are in PLC ladder logic to compare barcode strings and count three reads.
Worked example: one 20-character code every 375 ms
Inputs, each with its source:
- Line rate: 160 reads per minute, the case rate in a PLC ASCII serial port at 160 reads per minute.
- Payload: 20 ASCII characters in the DataMatrix.
- Framing: STX + payload + ETX + CR + LF.
- Line: 115200 baud, 8N1.
- Controller scan time: 10 ms, a mid-size program.
- Decode: budget from the reader's rating — the LS-CRD-0028 is rated 60 codes/s.
Arithmetic:
- Period per part: 60000 ms / 160 = 375 ms.
- Frame length: 20 payload bytes + 4 framing bytes = 23 bytes, so 23 × 10 bits = 230 bits.
- Wire time: 230 / 115200 = 2.0 ms.
- Decode ceiling: 1000 / 60 = 16.7 ms per read.
- Visibility in the controller: 2 × 10 ms = 20 ms.
- Worst case trigger-to-subroutine: 16.7 + 2.0 + 20 = 38.7 ms, call it 39 ms.
- Margin: 375 / 39 = 9.6.
Decision: at 160 parts per minute the serial chain uses about a tenth of the window between parts, and the cable is not the constraint — decode and the two scans are. Keep RS-232 while a part arrives no faster than every 50 ms (20 reads per second) and the payload stays under a few hundred characters; past either number, move to Ethernet.
How to prove it works
- Present each known code once and watch Selector and the subroutine's entry counter: one call per read, zero calls on the scans between reads.
- Present an unknown code: Selector stays 0 and the fault routine runs.
- Pull the trigger with no code in view: after a fixed window, 500 ms is a sane start, a timeout should flag the missed read and re-arm — the sequence in aborting a serial read when the scanner returns nothing.
- Cycle power on the reader mid-stream: the first frame after power-up must still land cleanly behind its STX.
When serial is the wrong answer
Move to Ethernet when any of these holds:
- Cable over 15 m.
- Rate above about 20 reads per second.
- Payloads past a few hundred characters, where wire time starts to rival decode.
- The reader you want only speaks network.
Over Ethernet the same latch-and-dispatch logic reads results instead of characters: reading fixed code reader results into a PLC over EtherNet/IP maps the tags, and wiring the PLC as the client to a reader acting as TCP server covers the socket path. The MV-ID3013PM-12M-WBN is a 1.3 MP compact reader with a 12 mm focal length on Fast Ethernet for exactly that wiring.
- Interface
- Fast Ethernet
- Resolution
- Up to 3 MP
- Shutter
- Global
- Lens mount
- M12
When each code should also trigger a vision check, a smart camera decodes and inspects in one device and reports a result the PLC still latches and dispatches the same way; if you build the read yourself, size the sensor for the smallest printed module with the camera resolution calculator. And if glare or a curved surface is killing the read, fix it in hardware before touching the program: code reader accessories carry polarizing covers and light boards that remove the reflection the decoder is fighting.
Checklist before you commission
- Baud, data bits, parity and stop bits identical in the reader and the PLC port.
- Protocol scan block terminator equals the reader's configured suffix, character for character.
- NewRead is a true one-shot: set on the frame-ready edge, cleared the next scan.
- Every subroutine call conditioned on NewRead, one rung per Selector value.
- Selector 0 wired to a fault subroutine that stops the feed and logs the unknown string.
- A read timeout after trigger, with re-arm.
- Reader 0 V tied to controller signal ground at one point.
- Trigger sensor debounced so one part fires exactly one read.
Then start a station build in the system configurator, or send us the code sizes, distances and cycle rate through the contact page and we will shortlist the readers.
Questions
How many different subroutines can one DataMatrix reader dispatch to?
One per entry in your compare table, and the table can be as long as you like. Each known code maps to one selector value and one call rung. If the list grows past a few dozen, stop comparing strings: encode the routine number inside the code itself, extract that field with an ASCII String Extract instruction, and convert it straight into the selector integer.
Free-run or hard trigger when the code picks the subroutine?
Hard trigger. A free-running reader decodes continuously and the PLC cannot tell which part produced the last string, so a dispatch pulse has no meaning. Trigger the reader from the part-present sensor or a PLC output, and the frame that arrives belongs to the part you are about to process. Free-run is only defensible when one part sits still in front of the reader for the whole cycle.
What serial settings should the reader and the PLC use?
115200 baud, 8 data bits, no parity, 1 stop bit, with STX before the payload and ETX after it. Set the same values in the reader configuration and the PLC port; a mismatch in any one of them produces framing errors or garbage, not a slower link. Drop to 9600 baud only on a long, noisy cable, where the same 23-byte frame needs 24 ms instead of 2.0 ms.
What should the PLC do with a code that is not in the table?
Leave the selector at 0 and give 0 a fault subroutine: stop the feed, flag the operator, and store the unknown string with a timestamp so you can tell a printing defect from a missing table entry. Never let an unknown code fall through with no rung true, because that is how a machine runs the previous part's routine on the next part.
Can the reader itself run the decision instead of the PLC?
Yes. A smart camera decodes on-device and can apply matching logic before it reports, so the PLC receives a short result rather than raw code strings; the new-read latch and a dispatch on that result still belong in the PLC. Use this split when each code also triggers a vision check, because one device then handles decode and inspection in a single exposure.



