Fletcher 16 Checksum generator

Value:

The task of the Fletcher 16 algorithm is to transform any long input string to 16 bit namely 4 hexadecimal number.

We use this algorithm for generating checksum in order to get proof of whether the message was delivered precisely. If the message on the serial port is X&s=abe4\n, then the Fletcher 16 algorithm is applied to the X. X is one string.

Here you can see examples for its application. The following C++ code demonstrates the operating of the algorithm:

//Fletcher 16 algorithm gives 4 hexadecimal number as follows :
void CommandParser::fletcher16(char* data)
{
  uint16_t sum1 = 0xff, sum2 = 0xff;
  size_t tlen;
  
  // s-ig levagas
  auto x = strstr_P(data, PSTR("&s="));
  // pointer aritmetika: megkaptjuk a levagott string hosszat
  auto bytes = x - data;

  while (bytes) {
    tlen = bytes >= 20 ? 20 : bytes;
    bytes -= tlen;
    do {
      sum2 += sum1 += *data++;
    } while (--tlen);
    sum1 = (sum1 & 0xff) + (sum1 >> 8);
    sum2 = (sum2 & 0xff) + (sum2 >> 8);
  }

  sum1 = (sum1 & 0xff) + (sum1 >> 8);
  sum2 = (sum2 & 0xff) + (sum2 >> 8);
  _checksum = sum2 << 8 | sum1;
}

The activation/deactivation of checksum can be reached using the ID manager 'linksetup' order. For details please review the protocol of 'linksetup'!

More information