title: Modbus functional code 1 & crc check for PIC microcontoller
body: <p>I need help in calculating modbus CRC check for function code 1. I.e read coil register. I have sample code for CRC check for function code 3 i.e holding register  for analog input.</p>
  
  <pre><code># Read Coil Status (FC=01)
  
  ## Request
  </code></pre>
  
  <p>This command is requesting the ON/OFF status of discrete coils # 20 to 56
  from the slave device with address 17.</p>
  
  <p>11 01 0013 0025 0E84</p>
  
  <ul>
  <li>11: The Slave Address (17 = 11 hex)</li>
  <li>01: The Function Code (read Coil Status)</li>
  <li>0013: The Data Address of the first coil to read. (Coil 20 - 1 = 19 = 13 hex)</li>
  <li>0025: The total number of coils requested. (coils 20 to 56 = 37 = 25 hex)</li>
  <li>0E84: The CRC (cyclic redundancy check) for error checking.</li>
  </ul>
  
  <h2>Response</h2>
  
  <p>11 01 05 CD6BB20E1B 45E6</p>
  
  <ul>
  <li>11: The Slave Address (17 = 11 hex)</li>
  <li>01: The Function Code (read Coil Status)</li>
  <li>05: The number of data bytes to follow (37 Coils / 8 bits per byte = 5 bytes)</li>
  <li>CD: Coils 27 - 20 (1100 1101)</li>
  <li>6B: Coils 35 - 28 (0110 1011)</li>
  <li>B2: Coils 43 - 36 (1011 0010)</li>
  <li>0E: Coils 51 - 44 (0000 1110)</li>
  <li>1B: 3 space holders &amp; Coils 56 - 52 (0001 1011)</li>
  <li>45E6: The CRC (cyclic redundancy check).</li>
  </ul>
  
  <h1>Read Holding Registers (FC=03)</h1>
  
  <h2>Request</h2>
  
  <p>This command is requesting the content of analog output holding registers # 40108 to
  40110 from the slave device with address 17.</p>
  
  <p>11 03 006B 0003 7687</p>
  
  <ul>
  <li>11: The Slave Address (17 = 11 hex)</li>
  <li>03: The Function Code (read Analog Output Holding Registers)</li>
  <li>006B: The Data Address of the first register requested. (40108-40001 = 107 = 6B hex)</li>
  <li>0003: The total number of registers requested. (read 3 registers 40108 to 40110)</li>
  <li>7687: The CRC (cyclic redundancy check) for error checking.</li>
  </ul>
  
  <h2>Response</h2>
  
  <p>11 03 06 AE41 5652 4340 49AD</p>
  
  <ul>
  <li>11: The Slave Address (17 = 11 hex)</li>
  <li>03: The Function Code (read Analog Output Holding Registers)</li>
  <li>06: The number of data bytes to follow (3 registers x 2 bytes each = 6 bytes)</li>
  <li>AE41: The contents of register 40108</li>
  <li>5652: The contents of register 40109</li>
  <li>4340: The contents of register 40110</li>
  <li>49AD: The CRC (cyclic redundancy check).</li>
  </ul>
  
  <p><strong>I am no issue for getting response for FC3. because i am sending properly the 2 byte address , but i dont know how can i send single byte and modify crc function for FC1 ->read coil register</strong></p>
  
  <p><a href="https://i.stack.imgur.com/r163Y.png" rel="nofollow noreferrer">Discription of read coil register </a></p>
  
  <pre><code>unsigned int crc_fn(unsigned char *dpacket,unsigned int len) { // CRC Function(Error calcualtion)
      unsigned int crc = 0xffff,poly = 0xa001;
      unsigned int i=0;
  
      for(i=0; i&lt;len; i++) {
          crc^= dpacket[i];
          for(j=0; j&lt;8; j++) {
              if(crc &amp; 0x01) {
                  crc &gt;&gt;= 1;
                  crc ^= poly;
              } else
                  crc &gt;&gt;= 1;
          }
      }
      return (crc);
  }
  </code></pre>