Skip to Main Content
Cognex Support Hub
Logo

Script to Save Last 70 Codes and Match it with Saved Codes and Reject Duplicate

Save last 70 codes and reject the latest code if it matches with last 70 readed codes.

23.02.2026

Details

In below script we save the last 70 read codes and reject the latest code if it matches with saved codes.

Note: The saved codes will vanish after power off. And if the code string length is big then it will increase the processing time. This code is not useful for high-speed applications.

 

var bufferSize = 70;
var barcodeBuffer = []; // Stores the last 70 scanned barcodes

function isDuplicate(barcode) {
   // Manual loop to check if barcode exists in the buffer
   for (var i = 0; i < barcodeBuffer.length; i++) {
       if (barcodeBuffer[i] == barcode) {
           return true; // Found duplicate
       }
   }
   return false; // No duplicate found
}

function onResult(decodeResults, readerProperties, output) {
   if (decodeResults.length > 0) {
       var currentBarcode = decodeResults[0].content; // Get scanned barcode

       // Check if the barcode is already in the buffer
       if (isDuplicate(currentBarcode)) {
           output.content = "Repeated Code: " + currentBarcode; // Mark as duplicate
           return;
       }

       // Add new barcode to the buffer
       barcodeBuffer.push(currentBarcode);

       // Ensure buffer does not exceed 70 barcodes
       if (barcodeBuffer.length > bufferSize) {
           barcodeBuffer.shift(); // Remove oldest barcode
       }

       output.content = "Barcode Accepted: " + currentBarcode; // Success message
   }
}

Verwandte Materialien