# Native Mode Checksum

The checksum that is used by Native Mode file transfer commands is a standard 16-bit Cyclical Redundancy Check (CRC). The following steps describe the algorithm for calculating the CRC. In this example, the variable CRC is unsigned 16 bits, and the input byte is 8 bits.

1. Set CRC to 0.
2. Get next byte from data stream.

3. Test the most significant bit of CRC and the input byte and perform the following:

   1. If they are equal, shift CRC left by 1.

   2. If one bit is 0 and the other is 1, then shift CRC left by 1, and XOR with 4129.

4. Shift the input byte left by 1.
5. If you have not shifted the input byte 8 times, go to step 3.
6. If you have remaining bytes in the data stream, go to step 2.

You can find a theoretical explanation of this algorithm in Numerical Recipes in C:The Art of Scientific Computing by William H. Press et al, page 896-901.

The following is a C function Functions are tools that are available in Spreadsheet for processing and analyzing acquisitions or other results. You can add functions to your Spreadsheet job to create tool chains and produce results for specific applications. that calculates the CRC one bit at a time:

Copy

    unsigned short CalculateCRC(char* buffer, int size)
    {
      unsigned short cword = 0;
      unsigned short ch;
      int i,j;
      for (i = 0; i < size; i++) {
        ch = buffer[i] << 8;
        for (j = 0; j < 8; j++) {
          if ((ch & 0x8000) ^ (cword & 0x8000)) {
            cword = (cword << 1) ^ 4129;
          }
          else {
            cword <<= 1;
          }
          ch <<= 1;
        }
      }
      return cword;
    }

The vision system calculates the checksum of a transferred file from the ASCII character values of the hexadecimal encoded file contents. For example:

File Contents | 0xAB 0xCD 0xEF 0x01
ASCII Representation | ABCDEF01
Byte Values to Check | 0x41 0x42 0x43 0x44 0x45 0x46 0x30 0x31

This encoding for file transfers over Native Mode commands adds a sequence of a carriage return and a line feed character after every 80 visible characters.