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

[Q] How to do a CRC8 #13

Closed
Sineos opened this issue Feb 4, 2023 · 5 comments
Closed

[Q] How to do a CRC8 #13

Sineos opened this issue Feb 4, 2023 · 5 comments

Comments

@Sineos
Copy link

Sineos commented Feb 4, 2023

I have following information:
image

and would like to calculate crc8 but somehow fail to get it working:

const result = crc.crc8(
    0x00, // polyLow
    0x31, // polyHigh
    8,
    0xFF, // initLow
    0xFF, // initHigh
    0x00, // XORlow
    0x00, // XORhigh
    false, // reflect
    Buffer.from([0xBE, 0xEF])
).toString('hex'));

Any pointer to where I'm wrong is appreciated. Thanks in advance.

@magiclen
Copy link
Owner

magiclen commented Feb 9, 2023

Modify polyLow to 0x31 and polyHigh to 0x00, and you can get the correct result.

@Sineos
Copy link
Author

Sineos commented Feb 9, 2023

Thank you very much for your help. Unfortunately, I still seem to hit a road block.

I'm using your module in node-red and:
const result = nodeCrc.crc8(Buffer.from([0xBE, 0xEF])).toString('hex')
works and returns 1A.

Using

const result = nodeCrc.crc8(
    0x31, // polyLow
    0x00, // polyHigh
    8,
    0xFF, // initLow
    0xFF, // initHigh
    0x00, // XORlow
    0x00, // XORhigh
    false, // reflect
    Buffer.from([0xBE, 0xEF])
).toString('hex')

does not work and throws TypeError: failed to downcast any to Buffer
Since the first example is working, I concluded that the error is due to me messing up the usage, i.e. setting the wrong parameters.

Using:

  • Linux 5.15.84-v7+ arm LE
  • Node-RED version: v3.0.2
  • Node.js version: v18.14.0
  • NPM version: v9.3.1

@magiclen
Copy link
Owner

magiclen commented Feb 9, 2023

Use the crc function, not crc8.

@magiclen
Copy link
Owner

magiclen commented Feb 9, 2023

@Sineos
Copy link
Author

Sineos commented Feb 9, 2023

D'oh! Now that you put me on the right track, it is quite obvious. Thanks!

@Sineos Sineos closed this as completed Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants