title: freemodbus eMBRegCoilsCB function body example
body: <p>Can anybody explain how to use <em><code>xMBUtilGetBits()</code></em> and <em><code>xMBUtilSetBits()</code></em> inside <em><code>eMBRegCoilsCB()</code></em>? I'm using freemodbus as modbus rtu slave driver.</p>
  
  <p>I can't add my code because it too big but you can see examples in demos (link below). In all examples <em><code>eMBRegCoilsCB()</code></em> not filled.</p>
  
  <pre><code>eMBErrorCode
  eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  {
      return MB_ENOREG;
  }
  
  eMBErrorCode
  eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  {
      return MB_ENOREG;
  }
  </code></pre>
  
  <p><strong>EDIT</strong>  </p>
  
  <p>My code not working if try to write (0x15) some bits with the offset > 0</p>
  
  <pre><code>if ( ( usAddress &gt;= REG_COILS_START )
      &amp;&amp; ( usAddress + usNCoils &lt;= REG_COILS_START + REG_COILS_NREGS ) )
  {
      iRegIndex = ( int ) ( usAddress - usRegCoilsStart );
  
      switch ( eMode )
      {
          case MB_REG_READ:
          {
              while ( usNCoils &gt; 0 )
              {
                  UCHAR ucResult = xMBUtilGetBits( usRegCoilsBuf, iRegIndex, 1 );
  
                  xMBUtilSetBits( pucRegBuffer, iRegIndex, 1, ucResult );
  
                  iRegIndex++;
                  usNCoils--;
              }
  
              break;
          }
  
          case MB_REG_WRITE:
          {                
              while ( usNCoils &gt; 0 )
              {
                  UCHAR ucResult = xMBUtilGetBits( pucRegBuffer, iRegIndex, 1 );
  
                  xMBUtilSetBits( usRegCoilsBuf, iRegIndex, 1, ucResult );
  
                  iRegIndex++;
                  usNCoils--;
              }
  
              break;
          }
      }
  }
  else
  {
      eStatus = MB_ENOREG;
  }
  </code></pre>
  
  <p><strong>Links</strong>  </p>
  
  <ol>
  <li><a href="https://github.com/webconn/freemodbus" rel="nofollow">freemodbus</a></li>
  </ol>