Details
Distinguishing between UPC and EAN barcodes can be challenging. Typically, a UPC code contains 12 digits, often prefixed with a leading zero when formatted as a 13-digit string. In contrast, EAN codes are either 13 or 8 digits long. For the purpose of this example, we will be working exclusively with 13-digit EAN codes.
To ensure accuracy during scanning, the scripts will include a built-in 2-second delay after each successful read. This delay helps prevent accidental or repeated scans of the same barcode.
Data Formatting Script
'use strict';
// Constants
const VALID_CODE_LENGTH = 13;
const ENTER= "\x0D\x0A";
const WAIT_TIME = 2;
//Precofing
dmccSet('SYMBOL.C128','ON');
dmccSet('SYMBOL.UPC-EAN','ON');
dmccSet('UPC-EAN.UPC-A','OFF');
dmccSet('UPC-EAN.UPC-B','OFF');
dmccSet('UPC-EAN.UPCE1','OFF');
dmccSet('UPC-EAN.EAN8','OFF');
dmccSet('UPC-EAN.DELZERO','ON');
// Function to handle the result of barcode decoding
function onResult(decodeResults, readerProperties, output) {
let outputString = '';
if (decodeResults[0].decoded) {
// Check if the decoded content length is exactly 13 characters
if ((decodeResults[0].content.length === VALID_CODE_LENGTH) || (decodeResults[0].symbology.name == 'Code 128')) {
outputString = decodeResults[0].content + ENTER;
output.events.system = Event.system.goodRead; // Trigger good read event
} else {
output.events.system = Event.system.noRead; // Trigger no read event
outputString = ''; // Clear output string
}
dmccSet('SYMBOL.C128','OFF');
dmccSet('SYMBOL.UPC-EAN','OFF');
if (serialHandler !== null) { //Check if serial handler is enabled
serialHandler.setTimer(WAIT_TIME); //Lunch timer with defined time value
}
}
output.content = outputString;// Set the output content
}
Communication Script
'use strict';
const LOG = true; //Enable loggin. Results could be found in Device Log
let serialHandler = null; //Declaring serial (COM) handler
function CommHandler(localName) {
return {
onConnect: function (peerName) {
if (peerName === 'COM USB') { //Opening connection on COM USB
serialHandler = this; // Enableing serial handler
logMsg('Serial Handler Connected!'); //Logging message that connection have been established
return true;
}
return false;
},
onDisconnect: function () {
if (this === serialHandler) {
serialHandler = null; //Disableing serial handler
logMsg('Serial Handler Disconnected!'); //Logging message that connection have been clossed
}
},
onExpectedData: function () {},
onUnexpectedData: function () {},
onTimer: function () { //When timer counts to desired value send data
if (this === serialHandler) { //Check if connection is open
logMsg ('Timer reached!');
dmccSet('SYMBOL.C128','ON');
dmccSet('SYMBOL.UPC-EAN','ON');
dmccSet('UPC-EAN.UPC-A','OFF');
dmccSet('UPC-EAN.UPC-B','OFF');
dmccSet('UPC-EAN.EAN8','OFF');
dmccSet('UPC-EAN.DELZERO','ON');
}
}
};
}
function logMsg (msg) { //Function for loggin messages
if (LOG) {
console.log(msg);
}
}