title: Arduino to Raspberry crc32 check
body: <p>I'm trying to send messages through the serial USB interface of my Arduino (C++) to a Raspberry Pi (Python). </p>
  
  <p>On the Arduino side I define a struct which I then copy into a char[]. The last part of the struct contains a checksum that I want to calculate using CRC32. I copy the struct into a temporary char array -4 bytes to strip the checksum field. The checksum is then calculated using the temporary array and the result is added to the struct. The struct is then copied into byteMsg which gets send over the serial connection. </p>
  
  <p>On the raspberry end I do the reverse, I receive the bytestring and calculate the checksum over the message - 4 bytes. Then unpack the bytestring and compare the received and calculated checksum but this fails unfortunately.</p>
  
  <p>For debugging I compared the crc32 check on both the python and arduino for the string "Hello World" and they generated the same checksum so doesn't seem to be a problem with the library. The raspberry is also able to decode the rest of the message just fine so the unpacking of the data into variables seem to be ok as well.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>The Python Code:</p>
  
  <pre><code>   def unpackMessage(self, message):
          """ Processes a received byte string from the arduino """
  
          # Unpack the received message into struct
          (messageID, acknowledgeID, module, commandType,
           data, recvChecksum) = struct.unpack('&lt;LLBBLL', message)
  
          # Calculate the checksum of the recv message minus the last 4
          # bytes that contain the sent checksum
          calcChecksum = crc32(message[:-4])
          if recvChecksum == calcChecksum:
              print "Checksum checks out"
  </code></pre>
  
  <p>The Aruino crc32 library taken from <a href="http://excamera.com/sphinx/article-crc.html" rel="nofollow">http://excamera.com/sphinx/article-crc.html</a></p>
  
  <p>crc32.h</p>
  
  <pre><code>#include &lt;avr/pgmspace.h&gt;
  
  static PROGMEM prog_uint32_t crc_table[16] = {
      0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
      0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
      0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
      0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  };
  
  unsigned long crc_update(unsigned long crc, byte data)
  {
      byte tbl_idx;
      tbl_idx = crc ^ (data &gt;&gt; (0 * 4));
      crc = pgm_read_dword_near(crc_table + (tbl_idx &amp; 0x0f)) ^ (crc &gt;&gt; 4);
      tbl_idx = crc ^ (data &gt;&gt; (1 * 4));
      crc = pgm_read_dword_near(crc_table + (tbl_idx &amp; 0x0f)) ^ (crc &gt;&gt; 4);
      return crc;
  }
  
  unsigned long crc_string(char *s)
  {
    unsigned long crc = ~0L;
    while (*s)
      crc = crc_update(crc, *s++);
    crc = ~crc;
    return crc;
  }
  </code></pre>
  
  <p>Main Arduino Sketch</p>
  
  <pre><code>struct message_t {
      unsigned long messageID;
      unsigned long acknowledgeID;
      byte module;
      byte commandType;
      unsigned long data;
      unsigned long checksum;
  };
  
  void sendMessage(message_t &amp;msg)
  {
      // Set the messageID
      msg.messageID = 10;
      msg.checksum = 0;
  
      // Copy the message minus the checksum into a char*
      // Then perform the checksum on the message and copy
      // the full msg into byteMsg
      char byteMsgForCrc32[sizeof(msg)-4];
      memcpy(byteMsgForCrc32, &amp;msg, sizeof(msg)-4);
      msg.checksum = crc_string(byteMsgForCrc32);
  
      char byteMsg[sizeof(msg)];
      memcpy(byteMsg, &amp;msg, sizeof(msg));
  
      Serial.write(byteMsg, sizeof(byteMsg));
  
  void loop() {
      message_t msg;
      msg.module = 0x31;
      msg.commandType = 0x64;
      msg.acknowledgeID = 0;
      msg.data = 10;
  
      sendMessage(msg);
  </code></pre>
  
  <p>Kind Regards,
  Thiezn</p>