How-to
Store Two Barcode Scans in an M221 and Compare Them
Short answer
A USB barcode scanner types into a focused Vijeo Designer input field, and a periodic script copies the field into an external String with sMSG.write(Barcode01.getStringValue()). Give the two scans separate non-overlapping %MW destinations — a 64-character string spans 32 words, %MW100 to %MW131 — and compare them in the M221 with EQUAL_STR, with no integer conversion.
On this page
- How does a USB barcode scanner get text into a Vijeo Designer HMI?
- Why does the scan show on the panel but never reach the M221?
- How much M221 memory does a 64-character barcode string take?
- How do you stop the second scan from overwriting the first?
- Which script copies each scan into its PLC string?
- How do you compare the two strings in the M221?
- How do you prove the whole chain works before production?
- Questions asked
A USB barcode scanner enters a Vijeo Designer application exactly like a keyboard, and the text lands in whichever input field has focus. Getting one scan to an M221, keeping a second one, and comparing the two is three moves: map an external String to controller memory, copy the field into it with the write() method from a periodic script, and compare the two stored strings with EQUAL_STR in the controller. The working configuration for this chain uses a 64-character string at %MW100 — %MW100 through %MW131 — copied with sMSG.write(Barcode01.getStringValue());.
How does a USB barcode scanner get text into a Vijeo Designer HMI? #
It arrives as keystrokes: configure the scanner on a PC, plug it into the panel's USB port, and it types exactly like a keyboard — but the operator must tap the target input field first, then scan.
Whatever the scanner is programmed to send on the PC — termination characters, prefixes, character filtering — it sends identically on the panel, so all scanner-side setup is finished on the PC before the device moves to the machine. One worked example of that kind of setup is stripping non-numeric characters from DS2200 barcode scans.
The panel side has one limit that shapes everything else: no event fires when a scan lands. Nothing runs at the instant of the scan, so the application learns about it by polling. That is why the copy logic below lives in a periodic script rather than in a scan-triggered action.
Why does the scan show on the panel but never reach the M221? #
The displayed text lives only in the panel's input-field variable, and a plain assignment to the external variable fails — the working call is the method form, sMSG.write(Barcode01.getStringValue());.
The field shows the code because the keystrokes land there; nothing writes them onward until a script does. Declare an external variable of type String named sMSG with device address %MW100. The declaration itself is the same task as adding a Modbus RTU driver and external variables in Vijeo Designer, done against the M221 driver instead.
The assignment form sMSG = Barcode01.getStringValue(); is rejected on an external variable, while the write() method call is the form that compiles and transfers. Set the string's size to 64 characters, or the longest code you must store, whichever is greater.
How much M221 memory does a 64-character barcode string take? #
Thirty-two words: two characters pack into each %MW word, so 64 characters ÷ 2 = 32 words, spanning %MW100 through %MW131 — and 131 − 100 + 1 = 32 words confirms the arithmetic.
Work the layout for two destinations:
sMSG_1starts at %MW100, the address from the working setup; at 64 characters it ends at 100 + 32 − 1 = %MW131.sMSG_2starts at the next free word, %MW132, and with the same length ends at 132 + 32 − 1 = %MW163.
Only the %MW100 start comes from the working configuration; %MW132 is this example's arithmetic, and any other M221 data parked in those two ranges has to move. Overlap is the fault this layout prevents: with a shared range the second scan's write lands on top of the first, and the comparison loses its reference value. String length is a setting, not a fixed property — a 32-character code needs 16 words, the size of the test string used later.
How do you stop the second scan from overwriting the first? #
Give each scan its own destination: the operator presses SCAN 1 before the first code and SCAN 2 before the second, and the periodic script copies the shared field into sMSG_1 or sMSG_2, then clears the field and resets the button.
One input field stays shared — the scanner always types into the same field — and the copy destination is chosen by a latch. The SCAN 1 and SCAN 2 buttons write the Integer variables YA_SCAN_1 and YA_SCAN_2 (initial value 0) to a value above 0; the script reads each as a boolean and, after copying, writes 0 back and empties the field with Barcode01.write(""). Because both destinations are controller memory, the first value survives the second scan — the same principle as keeping HMI-entered presets in PLC memory, applied to strings instead of timer values.
Which script copies each scan into its PLC string? #
A periodic script that runs for the whole target, holding the two if-blocks below: it reads the field, writes it to whichever external String is latched, then empties the field and drops the latch.
- Declare
Barcode01as the String variable bound to the input field's value, andYA_SCAN_1andYA_SCAN_2as Integer variables with initial value 0, each driven above 0 by its button. - Declare
sMSG_1andsMSG_2as external Strings of 64 characters at non-overlapping addresses — %MW100 and %MW132 in the worked layout above. - Create a periodic script on the target and set its period short enough that at least one tick falls between two consecutive scans, so a fast double-scan cannot land both codes under one latch.
- Enter the code:
// Button latches
boolean scan1 = YA_SCAN_1.getIntValue() > 0;
boolean scan2 = YA_SCAN_2.getIntValue() > 0;
// First barcode
if (Barcode01.getStringValue().compareTo("") != 0 && scan1) {
sMSG_1.write(Barcode01.getStringValue());
Barcode01.write("");
YA_SCAN_1.write(0);
}
// Second barcode
if (Barcode01.getStringValue().compareTo("") != 0 && scan2) {
sMSG_2.write(Barcode01.getStringValue());
Barcode01.write("");
YA_SCAN_2.write(0);
}
The two boolean locals start fresh each tick and take their value from getIntValue(); compareTo("") != 0 is the "a scan has landed" test that replaces the missing scan event; every transfer uses the same write() method shown earlier. After download, this script is the only writer of sMSG_1 and sMSG_2.
How do you compare the two strings in the M221? #
With EQUAL_STR in the M221 program: the two strings go in as its inputs and its Boolean result is TRUE only when the texts match — no conversion to integers is needed.
Converting the scanned text to an integer was the dead end in this workflow: it discards letters and spaces and only works for short numeric-only codes. The direct path compares text. For a bench test, the working example declares a string at %MW10 with a length of 16 words — 32 characters — and assigns it the literal 'PAPA MAMAN': 10 characters that occupy 10 ÷ 2 = 5 words of that buffer. Wire the two strings into EQUAL_STR and use its output as the match signal.
Before that result gates anything on the machine, add one interlock: two empty strings also compare equal, so require a non-zero length on both before accepting the output — and let the M221, not the panel, own that decision.
How do you prove the whole chain works before production? #
On the bench, with both strings and the comparison result displayed: the same code scanned twice must give TRUE, two different codes must give FALSE, and the first scan must still be readable in the PLC after the second lands.
Work the checks in this order:
- After the SCAN 1 copy, watch the words from %MW100 in the controller: they change to the copied text.
- After the SCAN 2 copy, re-read %MW100 to %MW131: the first barcode is unchanged — that is the overwrite check passing.
- Set the %MW10 test string to the same text as a stored scan: the comparison reads TRUE; change one character in it: the comparison reads FALSE.
- Confirm the field is empty and both latches read 0 after each copy, so the next press-scan cycle starts clean.
If the words never change after a scan, the fault is upstream of the PLC — the field was not focused when the scanner fired, the latch was never set, or the write() call was replaced by an assignment. If the words change but the comparison is wrong, the fault is in the addresses or in the strings' declared lengths.
Questions asked
Can the scanner launch a script the moment it scans?
No. This designer raises no event when a scan arrives, so nothing can fire at that instant. The periodic script is the substitute: on its next tick it sees the field become non-empty and copies it. Keep the period short enough that at least one tick lands between two consecutive scans, or a very fast double-scan under one latch can copy only the second code.
What happens if the operator scans without pressing SCAN 1 or SCAN 2?
Nothing copies: both latches read 0, the field keeps the new text, and both PLC strings hold their previous values, so a comparison that runs anyway uses stale data. Two defenses close the gap: clear both destination strings when either button is pressed, and let the comparison run only after both press-scan cycles have completed.
Does one input field limit me to two barcodes?
No. The field is shared scratch space; the destinations do the work. Add a third button latch and an sMSG_3 external String in its own non-overlapping range, then repeat the same if-block. At 64 characters per string, consecutive blocks start 32 words apart: %MW100, %MW132, %MW164, and so on.
Do the stored scans survive a panel restart?
Yes, for the panel: after the copy the text exists only in M221 %MW memory, so restarting or re-downloading the HMI application leaves it untouched. The controller's %MW words are also retentive across its own power cycle, which is the reason to store the scans in the controller rather than in panel memory.