UART communication with CRC

sherzaad:
Not sure what you doing but I check the code with the fix I suggested and it return the expected value.

/*CRC-16 CCITT

CRC 16 bits
   Polynomial 0x1021
   Initial value 0xFFFF
   No inverted input
   No inverted output
   No final XOR (0x0000)
/
uint16_t crc16_ccitt(int16_t
data, int16_t data_len) {
 uint16_t crc = 0xFFFF;

if (data_len == 0)
   return 0;

for (unsigned int i = 0; i < data_len; ++i) {
   uint16_t dbyte = data[i];
   crc ^= dbyte << 8;

for (unsigned char j = 0; j < 8; ++j) {
     uint16_t mix = crc & 0x8000;
     crc = (crc << 1);
     if (mix)
       crc = crc ^ 0x1021;
   }
 }

return crc;
}

void setup() {
 uint16_t test_data[9] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};

Serial.begin(9600);

Serial.println("crc16_ccitt algorithm test");
 Serial.print("Data set: ");
 for (int i = 0; i < 9; ++i) {
   Serial.print(test_data[i], DEC);
   Serial.print(", ");
 }
 Serial.print("\ncrc16_ccitt: 0x");
 Serial.print(crc16_ccitt(test_data, 9), HEX);
 Serial.println("");
}

void loop() {

}




Serial Monitor Output:


crc16_ccitt algorithm test
Data set: 49, 50, 51, 52, 53, 54, 55, 56, 57,
crc16_ccitt: 0x29B1




CRC calculator check:
![Untitled.png|1366x600](upload://hdft4S6e4QBSEQG0pwPsOs2LKu8.png)

Yes, that's work Thank you ! ???

I have another issue: If I insert data in string like below code, I'll have error? Could you expalin me what will happen? I'm a little bit confused on the datatype configuration!

char test_data = "123456789";

Thank you in advance,