Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inquiry about CRC Check #159

Open
Merdock1 opened this issue Oct 22, 2021 · 1 comment
Open

Inquiry about CRC Check #159

Merdock1 opened this issue Oct 22, 2021 · 1 comment

Comments

@Merdock1
Copy link

Merdock1 commented Oct 22, 2021

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
}
@emelianov
Copy link
Owner

@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 emelianov added this to the 4.0.0 milestone Oct 23, 2021
@emelianov emelianov modified the milestones: 4.0.0, 4.1.0 Nov 2, 2021
@emelianov emelianov modified the milestones: 4.1.0, 4.2.0 Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants