First of all, I made a fatal mistake in my function. Which is the reason why it's loosing the last byte:
This only writes one single byte: Serial.write(CRC::crc16(buf, sizeof(buf))); But I need two.
Using Python I can confirm that it works:
>>> data = bytearray(b'\x9a\xf9y\xc4\x00\x00\x00@\x00\x00@@\x00\x00\x80@I\x0c')
>>> import struct
>>> struct.unpack('f', data[0:4])
(-999.9000244140625,)
>>> struct.unpack('f', data[4:8])
(2.0,)
>>> struct.unpack('f', data[8:12])
(3.0,)
>>> struct.unpack('f', data[12:16])
(4.0,)
>>> import binascii
>>> binascii.hexlify(data[16:18])
b'490c'
This is the final test sketch which works:
#include "FastCRC.h"
FastCRC16 CRC16;
float valueA = -999.9;
float valueB = 2.0;
float valueC = 3.0;
float valueD = 4.0;
void printDataWithCRC(float *data1, float *data2, float *data3, float *data4) {
byte *byteData1 = (byte *)(data1);
byte *byteData2 = (byte *)(data2);
byte *byteData3 = (byte *)(data3);
byte *byteData4 = (byte *)(data4);
byte buf[16] = {
byteData1[0], byteData1[1], byteData1[2], byteData1[3],
byteData2[0], byteData2[1], byteData2[2], byteData2[3],
byteData3[0], byteData3[1], byteData3[2], byteData3[3],
byteData4[0], byteData4[1], byteData4[2], byteData4[3]
};
Serial.write(buf, 16);
uint16_t crc = CRC16.ccitt(buf, sizeof(buf));
Serial.write((byte) (crc >> 8));
Serial.write((byte) crc);
}
void setup() {
delay(100);
Serial.begin(230400);
}
void loop() {
printDataWithCRC(&valueA, &valueB, &valueC, &valueD);
delay(1000);
}