This is a set alone program that works and take the
unsigned char SensorMessage[32]; //Set Threshold and report
void setup() {
Serial.begin(9600);
buildSensorMessage();
}
void loop(){
while(Serial.available()) {
char c = Serial.read();
if (c != '\n please enter C ') {
delay (1000);
buildSensorMessage();
}
}
}
//******************************************************************************
unsigned short crc_1021(unsigned short crc, unsigned char b)
{
unsigned short x;
x = ((crc >> 8) ^ b) & 0xff;
x ^= x >> 4;
crc = (crc << 8) ^ (x << 12) ^ (x << 5) ^ x;
crc &= 0xffff;
return crc;
}
//******************************************************************************
unsigned short gen_crc(char * pData, int length)
{
unsigned short crc = 0xffff;
for(int i=0; i<length; i++) {
crc = crc_1021(crc, pData[i]);
}
return crc;
}
//******************************************************************************
void buildSensorMessage(){
int nextIndex = 0;
unsigned char TestCRC[]={0x07, 0xD8, 0x80, 0x39, 0xAD, 0x55, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
for (int id=0; id<(int)sizeof(TestCRC); id++) {
SensorMessage[nextIndex++] = TestCRC[id];
Serial.print(SensorMessage[id],HEX);
}
// correct CRC 0x67, 0x26
// Full SensorMessage need to read
//07D88039AD5573000000000000000000000002667
// checksum
unsigned short csum = gen_crc((char *)&SensorMessage[0], nextIndex);
SensorMessage[nextIndex++] = csum & 0xff;
SensorMessage[nextIndex++] = (csum & 0xff>>8);
Serial.println(" ");
Serial.print("SensorMessage CRC: ");
Serial.print(csum,HEX);
Serial.println(" ");
Serial.println(" ");
Serial.print("Full Sensor Message add CRC: ");
for (int id=0; id<(int)sizeof(SensorMessage); id++) {
Serial.print(SensorMessage[id],HEX);
}
}
This could clarify things Trying to get csum the value of 2667 add to the end of
TestCRC[]={0x07, 0xD8, 0x80, 0x39, 0xAD, 0x55, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
so the last digits are (0x67,0x26); or any value calculated by the csum