CRC adding to unsigned char

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

Original response before additional info was added to OP:
It’s not quite clear (to me) what your exact issue is.
When you talk about “string” it’s perfecly clear that you won’t be able to read any 0x00 byte as this - by design - is the string terminator byte which is per definition not part of the string content.
If you want to have 0x00 bytes contained you cannot treat the char[] as string.

What exactly does this sentence mean?

BTW, it’s also not really clear to me why you are using unsigned char and char and not just stick with char

 // with either one or the other in all places
 unsigned short csum = gen_crc((char *)&SensorMessage[0], 30);
 // would be the same as
 unsigned short csum = gen_crc(SensorMessage, 30);

Thanks for the clarification on the unsigned short and char,
Sorry not clear, there part that get me confused. Not easy for me to explain what the issue are. Ill edit the post when I can. Thanks for the look over.

edit the post to stand alone program which run and give out put to see if this better and can help solve my issue

Your problem is this line

  SensorMessage[nextIndex++] = (csum & 0xff>>8);

Order of execution: The shift operation applies to the number literal 0xff which results in 0x00 and consequently csum & 0x00 must result in 0x00.

Just change to

  SensorMessage[nextIndex++] = csum >> 8;
  // or more elaborate
  SensorMessage[nextIndex++] = (csum >> 8) & 0xFF;

And you’ll be good :wink:

1 Like