New Homepage Forums Sensor Shields Barcode Scanner: compare stings issue

This topic contains 1 reply, has 2 voices, and was last updated by avatar Amr 1 year, 11 months ago. This post has been viewed 372 times

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #101342
    avatartcflem7
    Participant

    Hi, I am using the BarcodeScanner example and modifying it. The goal is to be able to scan a barcode and compare against an already known barcode. I have barcode that is “9797” and I declare that as:

    char packageBarcode = “9797”;

    I then want to compare to the BarcodeScanner.getData() that is in the example code. I declare this as:

    char * scannedBarcode = BarcodeScanner.getData();

    From a very simmilar example on youtube (by 1Sheeld) I use the following code to compare the 2 strings:

    if(!strcmp(packageBarcode, scannedBarcode))
    {
    digitalWrite(ledPin, HIGH);
    Serial.println(“Match”);
    }else
    {
    digitalWrite(ledPin, LOW);
    Serial.println(“Not a match”);
    }

    Here is the link to the youtube video:

    The code runs just fine but it doesn’t work for saying that they match. The stange part is that when I print the scannedBarcode on the serial monitor, it shows this:

    ÿ&Zþü4
    ÿ&Òþù9797
    9797

    So it is obviously having strange characters in the scannedBarcode. So it makes sense why it wouldn’t say a match.

    Does anyone know why this is happening and how I can fix it?

    The complete code is:

    #define CUSTOM_SETTINGS
    #define INCLUDE_BARCODE_SCANNER_SHIELD
    #define INCLUDE_TERMINAL_SHIELD

    /* Include 1Sheeld library. */
    #include <OneSheeld.h>

    int ledPin = 13;

    char packageBarcode = “9797”;
    char * scannedBarcode = BarcodeScanner.getData();
    void setup()
    {
    /* Start communication. */
    OneSheeld.begin();
    /* Subscribe to the error callback. */
    BarcodeScanner.setOnError(&errorFunction);
    }
    void loop() {
    /* Check if a new barcode is scanned. */
    if(BarcodeScanner.isNewBarcodeScanned())
    {
    /* Print max data length saved in barcode. */
    Terminal.println(BarcodeScanner.getMaxDataLength());

    /* Print first 128 bytes of barcode data. */
    Terminal.println(BarcodeScanner.getData());

    Serial.println(BarcodeScanner.getData());
    // Serial.println(packageBarcode);
    // Serial.println(scannedBarcode);
    if(strcmp(packageBarcode, scannedBarcode))
    {
    digitalWrite(ledPin, HIGH);
    Serial.println(“Match”);
    }else
    {
    digitalWrite(ledPin, LOW);
    Serial.println(“Not a match”);
    }

    /* If the barcode has more that 128 bytes, query its next data bytes. */
    if(BarcodeScanner.getMaxDataLength() > 128)
    {
    /* Query the next 128 (default) bytes. */
    BarcodeScanner.queryNextDataBytes();
    }
    }
    /* Check if the data is the next bytes of same barcode. */
    if(BarcodeScanner.isNextDataBytesReceived())
    {
    /* Check if the barcode data was fully sent. */
    if(!BarcodeScanner.isFullySent())
    {
    /* Query the next 128 (default) bytes. */
    BarcodeScanner.queryNextDataBytes();
    }
    else
    {
    /* Print data is fully sent from barcode shield. */
    Terminal.println(“Fully sent”);
    }
    /* Print the next data bytes. */
    Terminal.println(BarcodeScanner.getData());
    }
    }

    void errorFunction(byte errorData)
    {
    /* Switch on error and print it on the terminal. */
    switch(errorData)
    {
    case BARCODE_NOT_SUPPORTED: Terminal.println(“Barcode is not supported”);break;
    case BARCODE_SCANNING_ERROR: Terminal.println(“Barcode scanning error”);break;
    case WRONG_PARAMETER: Terminal.println(“Wrong parameter”);break;
    case NOT_ENOUGH_BYTES: Terminal.println(“No enough bytes”);break;
    case CATEGORY_NOT_SUPPORTED: Terminal.println(“Category not supported”);break;
    }
    }

    #103965
    avatarAmr
    Keymaster

    Hi,

    Your code doesn’t make the print functions because you are using [Serial.print] statements which confuse the Arduino since 1Sheeld also uses the same serial line to communicate with the Arduino.

    But we have solved that of course by using the Terminal Shield that operates the same way just like [Serial.print] functions.

    And don’t forget to add the #define INCLUDE_TERMINAL_SHIELD line at the beginning of the code to be able to use the terminal shield functionalities.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.