title: how to make a CRC in python for a serial device?
body: <p>I have to build a serial comunication app using python, the old app is only working on windows xp and was written in C. Now i have to switch to linux and i dont have a working driver. I started to code it myself. I got the protocol from the producing company of the serial device.
     The serial device receives and sends data wich is ended by a CRC. I'm new to python and i dont have a solution for this, maybe someone can help me.</p>
  
  <p>This is the CRC alghoritm:</p>
  
  <pre><code>  ALGORITHM FOR CRC CALCULATION
  
  The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB.
  Three working bytes are used - S1, S0 and TR
  S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
  S0 - Least significant byte from the CRC ( It is transmitted after S1)
  TR - the current transmitted byte in the block.
  
  The CRC is calculated as follows:
  1. S1 and S0 are zeroed
  2. TR is loaded with the current transmitted byte. The byte is transmitted.
  3. Points 3.1 and 3.2 are executed 8 times:
  3.1. S1, S0 and TR are shifted one bit to the left.
  3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
  Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END.
  4. TR is loaded with 0 and point 3 is executed
  5. TR is loaded with 0 and point 3 is executed
  6. Byte S1 is transmitted
  7. Byte S0 is transmitted
  
  ALGORITHM FOR CRC CHECK ON RECEIVING
  
  Three working bytes are used S1, S0 and RC
  
  S1 - Most significant byte from the CRC ( it is received immediately after END)
  S0 - Least significant byte from the CRC ( transmitted after S1)
  RC - the current received byte in the block ( beginning from the first byte after BEG and ending 2 bytes after END).
  
  The CRC is obtained as follows:
  1. S1 and S0 are zeroed
  2. RC is loaded with the current received byte
  3. Points 3.1 and 3.2 are executed 8 times:
  3.1. S1, S0 and RC are shifted 8 times to the left
  3.2. if the MSB of S1 is 1 then MSB of S1 and LSB of S0 are inverted. 
  Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including 2 bytes after END.
  S1 and S0 must be 0.
  </code></pre>
  
  <p>If there is someone wich can show me how to do it i'll be very gratefull.Thank you all.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I managed to get the same CRC procedure made by someone, but its made in java, i'm not that good with java. Maybe you can guide me into converting it in python. This is the code:</p>
  
  <pre><code>public class Crc {
      public static final String CRC_NAME = "CRC-16-ECR";
      private static final int POLYNOMIAL = 32769;
      public static final int WIDTH = 16;
      public static final int TOPBIT = 32768;
      short CRC;
      short[] crcTable = new short[256];
  
      public Crc() {
          this.crcInit();
      }
  
      private void crcInit() {
          for(int dividend = 0; dividend &lt; 256; ++dividend) {
              int remainder = dividend &lt;&lt; 8;
  
              for(byte bit = 8; bit &gt; 0; --bit) {
                  if((remainder &amp; '耀') != 0) {
                      remainder = (remainder &lt;&lt; 1 ^ '老') &amp; '\uffff';
                  } else {
                      remainder = remainder &lt;&lt; 1 &amp; '\uffff';
                  }
              }
  
              this.crcTable[dividend] = (short)remainder;
          }
  
      }
  
      public short crcFast(byte[] message, int nBytes) {
          int remainder = 0;
  
          for(int oneByte = 0; oneByte &lt; nBytes; ++oneByte) {
              int data = (message[oneByte] ^ remainder &gt;&gt; 8) &amp; 255;
              remainder = this.crcTable[data] ^ remainder &lt;&lt; 8;
          }
  
          return (short)remainder;
      }
  }
  </code></pre>