Code readingHow-to

Barcode scanner send only codes with a specific prefix

Configure a barcode scanner to send only codes with a specific prefix, transform the matches, and silently discard every other scan, with a test protocol.

LumaSpec Vision9 min read Share on LinkedIn

On this page 10 sections
  1. 01Two rules, and the discard rule goes last
  2. 02Why one rule works once, then leaks
  3. 03Build the rule set
  4. 04Worked example: SK12345678 in, 1SK12345678 + CR out
  5. 05One terminator, one owner
  6. 06If the reader has no rule feature
  7. 07Pick the reader by the host it feeds
  8. 08Go-live test
  9. 09When the scanner is the wrong place for the gate
  10. 10Questions

You want the scanner to act as a gate, not a pipe: codes that start with SK go to the host as 1SK12345678 followed by a carriage return, and every other code vanishes. The first attempt most engineers make — one formatting rule with a begins-with-SK criterion — fails in a specific way: the first SK code arrives correctly, then every code scanned afterwards reaches the PC, rejects included. A criterion never discarded anything; only an action can. If you feed an HMI, PLC or MES from handheld or fixed code readers, this guide gives you the two-rule setup, the reason a single rule leaks, the exact rule table for the SK case, and the go-live test that catches the leak before your MES does.

Two rules, and the discard rule goes last

Formatting rules on an industrial reader have two halves. Criteria decide which codes a rule applies to; actions decide what happens to the data, including whether it is transmitted at all. That split is the whole trick.

The job takes exactly two rules:

  • Rule 1 — keep and transform. Criteria: data begins with SK, set as a data-match pattern of the literal SK plus the any-remainder wildcard. Actions, in order: insert 1 at the front, append a carriage return, send the data.
  • Rule 2 — discard the rest. Criteria: any symbology, any data. Action: send nothing.

Rule lists evaluate from the top, and the first rule whose criteria match is applied; processing stops there. The catch-all must therefore be the last rule — placed above the keep rules, it swallows every code, SK ones included.

Guides name this feature differently — advanced data formatting, data editing, formatting rules — but it is always a numbered rule list built once in the maker's configuration utility, or written to the reader as documented configuration strings.

Why one rule works once, then leaks

Rule evaluation is stateless. Every scan starts again at rule 1; nothing carries over from the previous scan.

With only the SK rule loaded, a matching code triggers the actions and transmits transformed. A non-matching code triggers no actions — and no actions means the reader falls through to its default behavior: transmit the data unchanged. Your first scan happened to be an SK code, so the filter looked correct. The first reject went straight through, and so did everything after it. Nothing wore out and nothing needed re-arming; the rule set never contained a discard. The fix is the catch-all rule, not a fresh configuration.

Build the rule set

  1. Start a new configuration file for the reader in the maker's configuration utility, a free download from its support site. If your fleet is configured with strings from the programming guide over USB or serial instead, the same rules appear there as numbered entries.
  2. Open the formatting or data-rules page.
  3. Create rule 1. Leave the symbology criterion at any, or narrow it to the symbology your labels use. Set the data criterion to a begins-with match on SK.
  4. Give rule 1 three actions, in order: insert prefix 1, append suffix CR (ASCII 13, hex 0D), then send the data. The send action commits the transmission; a matched rule without one transmits nothing.
  5. Create rule 2. Set criteria to any symbology and any data, with the single action send nothing (some guides call it discard).
  6. Confirm the order on screen: rule 1 above rule 2. Re-check after every edit, because utilities renumber rules when you duplicate or move them.
  7. Write the configuration to the reader and save the profile file with the station documentation, so a replacement scanner loads identical rules in a minute.

A global prefix or suffix is not a filter — it adds characters to every scan. If all you need is an Enter key after every scan, that is a suffix job, covered in adding an Enter key with programming barcodes; a preamble prepended to every scan, as in the preamble and serial capture guide, cannot suppress anything either.

Worked example: SK12345678 in, 1SK12345678 + CR out

Inputs, and where each comes from: the station forwards codes starting with SK as 1SK12345678 terminated by a carriage return, because the host field commits on Enter; the sample code SK12345678 is 10 characters; symbology stays at any because the line mixes 1D and 2D labels. The wanted transmission is the 11-character output string plus one CR byte.

sent length = 1 inserted character + 10 code characters + 1 CR = 12 bytes

Trace of three scans:

Scan Rule matched Actions run Host receives
SK12345678 Rule 1 insert 1, append CR, send 1SK12345678 + CR, 12 bytes
ABC1234567 Rule 2 send nothing nothing, 0 bytes
SK12345678 again Rule 1 insert 1, append CR, send 1SK12345678 + CR, 12 bytes

The third row is the regression case: stateless evaluation sends the same code down the same path every time, rejects in between included. Decision: an SK code must produce exactly 12 bytes and every other code none. Bytes after a reject mean the catch-all is missing or mis-ordered.

One terminator, one owner

Decide once who appends the carriage return. If rule 1 appends CR, remove any global Enter or CR suffix from the same configuration — scanner guides warn against setting the same terminator through both mechanisms, and the host would see the string closed twice. If the line already commits scans with a global suffix everywhere else, keep the suffix and drop CR from the rule; rule 1 then only inserts the 1 and sends. The CR LF suffix guide covers the suffix side in detail.

If the reader has no rule feature

Entry-level handhelds often expose only a global prefix and suffix, with no rule table. Then the gate moves to the first host that sees the data. In a PLC, compare the first two characters of the received string with SK and branch — the same leading-character compare pattern as a numeric range match on leading digits. In an HMI script or PC service, a start-anchored pattern such as ^SK.* does it, as in regex parsing with capture groups. Keep exactly one filter in the chain — scanner or host, never both — or you will chase prefixes applied and stripped twice. When the cleanup has to live in an HMI entry field, this prefix-strip walkthrough in an HMI data-entry dialog shows the pattern end to end.

Pick the reader by the host it feeds

With the gate logic settled, the remaining decision is the interface, and it follows the host:

Handheld code readersHikrobot MV-IDH2000P/13NR/05RN/U Industial Wired Handheld Code ReaderMV-IDH2000P/13NR/05RN/U
Interface
USB
Connection
Wired
$179.00View part
Handheld code readersHikrobot MV-IDH3000/13NR/05RN/LE Industrial Wired Handheld Code Reader (IP LAN)MV-IDH3000/13NR/05RN/LE
Interface
Fast Ethernet
Connection
Wired
$379.00View part
Handheld code readersHikrobot MV-IDH7000P/10NR/07RN/LP ID7000P PoE Fast Ethernet NR handheld code readerMV-IDH7000P/10NR/07RN/LP
Interface
Fast Ethernet
Connection
Wired
$479.00View part

Above the line, a fixed reader holds the same job permanently:

Fixed code readersHikrobot MV-ID3013PM-12M-WBN 12mm focal length 1.3 MP Compact Industrial Code ReaderMV-ID3013PM-12M-WBN
Interface
Fast Ethernet
Resolution
Up to 3 MP
Shutter
Global
Lens mount
M12
$1,979.00View part

For bench-scale short-range reading, the compact reader below prints a 60-120 mm reading distance and connects over USB 2.0:

Fixed code readersCompact fixed industrial barcode reader, 1.2 MP, USB 2.0, 60-120 mm reading distance (LS-CRD-0047)LS-CRD-0047
Resolution
Up to 3 MP
Shutter
Global
Interface
USB
$559.00View part

Protective covers, light boards and I/O boxes for these readers sit in the code reader accessories category.

Go-live test

Run this sequence before the station goes live, watching the raw receive buffer rather than the formatted HMI field:

  1. Scan SK12345678. Expect 1SK12345678 followed by CR — 12 bytes if you can count them on the port.
  2. Scan a code without SK. Expect silence: no bytes, no terminator. The receive-buffer framing guide shows quiet versus a stray null.
  3. Scan the SK code again. Expect the same 12 bytes — this step reproduces the works-once-then-leaks symptom if the catch-all is missing.
  4. Scan every symbology the station accepts, matched and unmatched, plus one with SK mid-string such as 123SK456; a start-anchored criterion discards it.
  5. Scan a short SK partial such as SK9 and decide whether it should pass. Begins-with matches pass it; add a length criterion to rule 1 if partials must be rejected.

If a serial link delivers garbage instead of clean text, fix framing first — the serial-link garbage guide covers the usual causes.

When the scanner is the wrong place for the gate

Three cases argue for moving or extending the filter:

  • The operator must see why a code was rejected. A silent discard gives the host nothing to display. Surface rejects where a message can be shown, or build a proper check station — the pass/fail match verification build makes the good/no-good decision explicit.
  • The accepted prefix changes with the recipe. A rule table is static. Filter in the PLC against the active recipe instead; the recipe lookup with UDT arrays pattern keeps the accepted list where the recipe lives.
  • The code must equal an expected value, not merely start with a prefix. Prefix gating is a coarse filter; equality checking belongs to the verification layer above it.

Standardizing readers for this job starts with the host interface: USB for bench HMIs, Ethernet for PLC tags. Both categories link from the code reader finder, and the code-reading topic page collects the integration guides. For a review of your rule table before roll-out, send the code format and host type through the contact page.

Questions

Do formatting-rule criteria discard codes that don't match?

No. Criteria only decide whether a rule's actions run; a code that matches no rule is transmitted unchanged. Discarding always comes from an action — send nothing — on a rule the unwanted codes do match. That is why the catch-all rule is required, and why a silent receive buffer on a reject scan is the proof it fired.

Can I accept two different prefixes with different formatting?

Yes. Add one keep rule per prefix, each with its own insert and send actions, all above the single catch-all discard rule. Evaluation stops at the first match, so the keep rules can share one discard rule; you only maintain the keep rules as the prefix list grows.

What does the host see when a code is discarded?

Nothing at all: no characters, no terminator, no new line. The receive buffer stays quiet, which is why buffer-level testing matters more than watching a formatted field. If the process needs an event for rejects, that signal has to come from a host-side filter, not from a suppressed scan.

Is a carriage return the same as the Enter suffix?

CR is ASCII 13 (hex 0D) and most terminals treat it as Enter, but hosts vary: some want CR only, some want CR plus LF (hex 0A). Send exactly the byte sequence your host parser tests for, and set it through one mechanism only — the rule or the global suffix, never both.

Do formatting rules slow the scanner's read rate?

The checks run in the reader between decode and transmit as short string comparisons on a small rule list, so the read rate the data sheet prints still governs. Read rate is set by optics, exposure and code quality, not by two extra rule rows.