QuakeGod
2024-08-06 7d8ba5df7d883c86c24aa38449c4a6dd126e920f
ComLib/Src/ModbusRTU.c
@@ -61,25 +61,7 @@
   return(crc);
}
*/ 
const uint16_t crctalbeabs[] = {
   0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
   0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
};
uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len)
{
   uint16_t crc = 0xffff;
   uint16_t i;
   uint8_t ch;
   for (i = 0; i < len; i++) {
      ch = *ptr++;
      crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
      crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
   }
   return crc;
}
//**********************************************************************
//******************  Modbus slave  ************************************
@@ -168,11 +150,18 @@
            bitmask=1;
            for (int j=0;j<8&&i*8+j<nCount;j++)
            {
               Addr1=(Addr&0xff0)>>4;
               Addr1=(Addr&0xfff0)>>4;
               bitAddr=Addr&0xf;
               if (GetBitValue( KMem.WY[Addr1],bitAddr))
               {
                  thisValue|=bitmask;
               if (Addr < 2048) {
                  if (GetBitValue( KMem.WY[Addr1],bitAddr))
                  {
                     thisValue|=bitmask;
                  }
               }else {
                  if (GetBitValue( KMem.WR[Addr1-128],bitAddr))
                  {
                     thisValue|=bitmask;
                  }
               }
               bitmask<<=1;
               Addr++;
@@ -229,15 +218,17 @@
      case WriteCoil:   //5  bit
      //Store Datas;
         //SetBitValue(&KMem.WR[Addr1],bitAddr,pPkg->nCount);   
            Addr1=(Addr&0xfff0)>>4;
            bitAddr=Addr&0xf;
         if (nCount == 0)      // set to 0
         {
            ResetBit(&KMem.WY[Addr1],bitAddr);
            if (Addr < 2048) { ResetBit(&KMem.WY[Addr1],bitAddr);}
            else {ResetBit(&KMem.WR[Addr1-128],bitAddr);}
         }else if (nCount == 0xFF00)   // set to 1
         {
            SetAddrBit(&KMem.WY[Addr1],bitAddr);
         }else
         {
            //error
            if (Addr < 2048) { SetAddrBit(&KMem.WY[Addr1],bitAddr);}
            else {SetAddrBit(&KMem.WR[Addr1-128],bitAddr);}
         }
         memcpy(Pkgbuf,ptr,len);
         needcrc=0;
@@ -260,10 +251,13 @@
            bitmask=1;
            for (int j=0;j<8&&i*8+j<nCount;j++)
            {
               Addr1=(Addr&0xff0)>>4;
               Addr1=(Addr&0xfff0)>>4;
               bitAddr=Addr&0xf;
               SetBitValue( &KMem.WY[Addr1],bitAddr,bitmask&thisValue);
               if (Addr < 2048) {
                  SetBitValue( &KMem.WY[Addr1],bitAddr,bitmask&thisValue);
               } else {
                  SetBitValue( &KMem.WR[Addr1 - 128],bitAddr,bitmask&thisValue);
               }
               bitmask<<=1;
               Addr++;
            }