CRC16-CITT - Xmodem implement

Hello guys,
i have problem with CRC16 Xmodem.

When i take a part of the code from serial string {FC0100}, how can i calculated the CRC?

Thanks.

Which Windows program sends that string and where is the string documented? It is extremely unusual that the CRC is at the beginning of the string. Are you sure you have this right?

Pete

I’ve figured out how to get the CRC.
Of the example data given:
F5F1FC0100

  • the last 6 characters are each one ASCII character.
  • the F5F1 is two characters 0xF5 and 0xF1.
    The CRC uses the xmodem CRC polynomial but the initial value is zero - xmodem protocol CRC uses an initial value of 0xFFFF.

This code uses the example data above to calculate its CRC. You’ll have to extract the CRC code and add it to your sketch.

// This uses CRC16-CCITT
// From: http://arduino.cc/forum/index.php/topic,96539.0.html

// This data is from
// http://forum.arduino.cc/index.php?topic=529288.0
char CRC1 [6] = {
  // CRC is 0xF5F1
  'F','C','0','1','0','0'
}; 

void setup(void)
{
  uint16_t crc_out;
  Serial.begin(9600);
  while(!Serial);

  crc_out = calc_crc(CRC1,6); //Calculate CRC on-the-fly
  Serial.println(crc_out,HEX);
}

void loop(void)
{
}


uint16_t calc_crc(char *msg,int n)
{
  // Initial value. xmodem uses 0xFFFF but this example
  // requires an initial value of zero.
  uint16_t x = 0;

  while(n--) {
    x = crc_xmodem_update(x, (uint16_t)*msg++);
  }
  return(x);
}

// See bottom of this page: http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html
// Polynomial: x^16 + x^12 + x^5 + 1 (0x1021)
uint16_t crc_xmodem_update (uint16_t crc, uint8_t data)
{
  int i;

  crc = crc ^ ((uint16_t)data << 8);
  for (i=0; i<8; i++) {
    if (crc & 0x8000)
      crc = (crc << 1) ^ 0x1021; //(polynomial = 0x1021)
    else
      crc <<= 1;
  }
  return crc;
}

Pete

It works, thank you very well :slight_smile: