Details
This script is used to search the keywords in string and extract the required number of characters from it.
Here we will take the example to search the cognex serial number. Cognex device start with 1A always then we will search "1A" in string and length of serial number is 14 digits. Eg. 1A1234PP123456
In below script we use "1A" as a searching keyword.
In below condition replace "1A" with your keyword and put the length of keyword in i + 2 value.
if (data.substring(i, i + 2) == "1A")
In below substring condition it will crop the given range of data from the string. So put your length of data in i + 14.
serial = data.substring(i, i + 14);
function onResult(decodeResults, readerProperties, output)
{
if (decodeResults[0].decoded)
{
var data = decodeResults[0].content;
var serial = "";
// Scan the whole string
for (var i = 0; i <= data.length - 14; i++)
{
// Check if it starts with "1A"
if (data.substring(i, i + 2) == "1A")
{
serial = data.substring(i, i + 14);
break; // Stop after first match
}
}
// If found, send only the serial
if (serial != "")
{
output.content = serial + "\r\n";
}
else
{
output.content = "SERIAL NOT FOUND\r\n";
}
}
}