modbus-esp8266 icon indicating copy to clipboard operation
modbus-esp8266 copied to clipboard

Inquiry about CRC Check

Open Merdock1 opened this issue 3 years ago • 1 comments

Hi Emiliano. I want to congratulate you on your great work in this libraries. It really is very useful to me. I have just taken my first steps in the world of code and I consider myself a great ignorant. I want to make a comment regarding the CRC check. In your code you use a table of values for CRC. In this library the CRC calculates it, I don't know which of the 2 ways are more efficient but I wanted to share it since it might be useful. I hope I am not being too bold in making this comment. A big greeting and thanks again for your work. Here is the address: https://github.com/angeloc/simplemodbusng/blob/master/SimpleModbusSlave/SimpleModbusSlave.cpp

the part of code i mean:

unsigned int calculateCRC(byte bufferSize) 
{
  unsigned int temp, temp2, flag;
  temp = 0xFFFF;
  for (unsigned char i = 0; i < bufferSize; i++)
  {
    temp = temp ^ frame[i];
    for (unsigned char j = 1; j <= 8; j++)
    {
      flag = temp & 0x0001;
      temp >>= 1;
      if (flag)
        temp ^= 0xA001;
    }
  }
  // Reverse byte order. 
  temp2 = temp >> 8;
  temp = (temp << 8) | temp2;
  temp &= 0xFFFF;
  return temp; // the returned value is already swopped - crcLo byte is first & crcHi byte is last
}

Merdock1 avatar Oct 22 '21 00:10 Merdock1

@Merdock1 CRC calculation with helper table (that used by the library) is way faster. But your post has remind me that it's required to add table-less implementation for Arduino UNO and others PCL (to reduce memory footprint).

emelianov avatar Oct 23 '21 03:10 emelianov