1

I would like to compare two values z1 (CRC built from last two concatenated of outWord[10] and outWord[11]), and z2 that is also CRC but calculated upon packet numbers.
z1 is as it should be e568 and z2 too, but when I compare both values I got a difference.

Can you tell me please where the problem is?

#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int inWord = 0;
int outWord[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int index = 0;
unsigned char Data2Calc[]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned short CRC2Calc = 0;
unsigned char Bytes2Calc = 9;

void setup()
{
    pinMode(13, OUTPUT);
    lcd.begin(16,2);    
    lcd.backlight();    
    lcd.setCursor(0,0); 
    lcd.print("Test RFID");
    lcd.setCursor(0,1); 
    lcd.print("Zeskanuj TAG");
    Serial.begin(9600);
    Serial1.begin(9600);
    lcd.setCursor(0,0);
}

void loop()
{
    if (Serial1.available())
    {

        RFID();

    }
    else
    {
        if (index==11)
        {
            index=0;
        }
    }
}



unsigned char RFID(void)
{
    char z1 [10] = { "" };
    char z2 [10] = { "" };
    unsigned short crc1 [] =  { 0 };
    unsigned short crc2[]  = { 0 };

    //-----
    inWord = Serial1.read();
    index++;

    if (index == 1)
    {
        if (inWord == 1)
        {
            outWord[index] = inWord;
        }
        else
        {
            index=index-1;
        }

    }
    else if (index > 1)
    {
        if (index == 11)
        {
            outWord[index] = inWord;

            for (int i = 1; i <12; i++)
            {
            Serial.print(outWord[i],HEX);   // --> 1B31687DBC7FFE568
            Data2Calc[i-1] = outWord[i];
            }
            Serial.println();

            CRC16(Data2Calc, &CRC2Calc, Bytes2Calc);

            itoa(outWord[10],z1,16);
            itoa(outWord[11],z2,16);
            strcat(z1, z2);
            *crc2 = CRC2Calc;

            Serial.print("crc1=");      //
            Serial.print(z1);       // --> e568 
            Serial.println("");     //
            Serial.print("crc2=");      //
            sprintf(z2, "%x", *crc2);   // 
            Serial.print(z2);       // --> e568
            Serial.println("");     //
            Serial.print("CRC2Calc=");  //
            Serial.print(CRC2Calc,HEX); // --> E568
            Serial.println("");     //

            if (z1 == z2)
            {
                Serial.print("OK");
            }
            else
            {
                Serial.print("FAILED");
            }

            Serial.println("");

        }
        else
        {
        outWord[index] = inWord;
        }
    }




    return *z1;
}



void CRC16(unsigned char * Data, unsigned short * CRC, unsigned char Bytes)
{
    int i, byte;
    unsigned short C;

    *CRC = 0;
    for (byte = 1; byte <= Bytes; byte++, Data++)
    {
        C = ((*CRC >> 8) ^ *Data) << 8;
        for (i = 0; i < 8; i++)
        {
            if (C & 0x8000)
                C = (C << 1) ^ 0x1021;
            else
                C = C << 1;
        }
        *CRC = C ^ (*CRC << 8);
    }
}

The entire output of the program is as below:

    Currently there are no serial ports registered - please use the + button to add a port to the monitor.
    Connect to serial port COM4 at 9600
    1B31687DBC7FFE568 
    crc1=e568 
    crc2=e568
    CRC2Calc=E568
    FAILED
gre_gor
  • 6,669
  • 9
  • 47
  • 52
bzc0fq
  • 579
  • 1
  • 3
  • 18
  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Jorengarenar Jul 10 '20 at 13:21
  • 4
    Does this answer your question? [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – BallisticTomato Jul 10 '20 at 13:35

1 Answers1

4

You can't compare the contents of two char* string by comparing their pointers, as in if (z1 == z2). This will (almost) always be false, as the strings are in two different memory locations, so their addresses will be different.

You should use the strcmp() function, which returns zero if the strings are the same. So:

    if (strcmp(z1,z2) == 0)
    {
        Serial.print("OK");
    }
    else
    {
        Serial.print("FAILED");
    }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83