On Tue, 25 Oct 2022 08:30:19 +0200, Bo Berglund via fpc-pascal <fpc-pascal@lists.freepascal.org> wrote:
>I am working on a handler for IoT data from an electricity meter which >communicates via TTL serial at 115200 baud. It only sends data in packets of >less than 1 kbytes every 5-10 seconds. > >In order to validate the data I need to check the CRC16 checksum at the end and >here is where I don't find a good way to do it... >The application will run on Linux so I checked for operating support but found >only sksum, which apparently does not do CRC16. > >Is there some package or code available somewhere which can calculate the CRC16 >value over a byte array of some 1000 bytes? > >In a description of the protocol after showing the data structure it adds this: > >crc16 = libscrc.ibm(example_data).to_bytes(2, 'big').hex() >> '7945' > >Googling libscrc brings me to Python code on GitHub, which I am not really able >to use, never programmed Python... >https://github.com/hex-in/libscrc > >Anyone able to help with FPC implementation? I have found a c++ function that purportedly does CRC16: unsigned int CRC16(unsigned int crc, unsigned char *buf, int len) { for (int pos = 0; pos < len; pos++) { crc ^= (unsigned int)buf[pos]; // * XOR byte into least sig. byte of crc // * Loop over each bit for (int i = 8; i != 0; i--) { // * If the LSB is set if ((crc & 0x0001) != 0) { // * Shift right and XOR 0xA001 crc >>= 1; crc ^= 0xA001; } // * Else LSB is not set else // * Just shift right crc >>= 1; } } return crc; } Can someone please decode this into pascal? -- Bo Berglund Developer in Sweden _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal