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

Match CRC from C code to crc-full #1

Closed
galvesribeiro opened this issue Sep 29, 2017 · 3 comments
Closed

Match CRC from C code to crc-full #1

galvesribeiro opened this issue Sep 29, 2017 · 3 comments

Comments

@galvesribeiro
Copy link

Hello,

I have to send a message from typescript to our device and at the device side, it validates the message using a CRC16.

Here is how the device validates it:

#define CRC_MASK 0x1021 /* x^16 + x^12 + x^5 + x^0 */

UINT16 CRC_Calc (unsigned char *pbData, int iLength)
{
    UINT16 wData, wCRC = 0;
    int i;

    for ( ;iLength > 0; iLength--, pbData++) {
        wData = (UINT16) (((UINT16) *pbData) << 8);
        for (i = 0; i < 8; i++, wData <<= 1) {
            if ((wCRC ^ wData) & 0x8000)
                wCRC = (UINT16) ((wCRC << 1) ^ CRC_MASK);
            else
                wCRC <<= 1;
        }
    }
    return wCRC;
}

The input data would be "OPN\x17" (where \x17 is an ETB control character). The output must be A8A9.

Given that input data, the C sample code, can you provide me a sample code using your package to achieve the same result?

Thank you! I appreciate any help.

Best regards,
Gutemberg

@RioloGiuseppe
Copy link
Owner

Hello,
I just made some tries. I think you get a wrong result because you are passing "OPN\x17" as string. If you pass the equivalent byte array of ascii characters, you will havethe same result of C/C++.

As in the readme:

import {CRC} from 'crc-full'
var crc = new CRC(16, "CRC16", 0x1021, 0x0000, 0x0000, false, false);
var res = crc .compute([0x4F, 0x50, 0x4E, 0x17]);        // Array with ascii integer of "OPN\x17"

The result (number) is 43177 but if you convert to an hex string you will have: 'a8a9', that is the same result obtained in C.

h_string = res.toString(16)

Hope to been useful
Best regards
Giuseppe Riolo

@galvesribeiro
Copy link
Author

@RioloGiuseppe hello! Thank you very much for the quick response.

I got it working!

If I may, I'm whiling to contribute an update to your repo and package so it can be easily consumed from npm. Right now, you are asking people to install typescript, which shouldn't be required since they may want to use it on JS as well.

If you accept the PR and will update the npm package, I can push it later Today.

Thanks!

@RioloGiuseppe
Copy link
Owner

RioloGiuseppe commented Sep 29, 2017

You're welcome!
Yes. To do it fast I used this online tool, so OPN became 0x4F, 0x50, 0x4E and \x17 is an escaped form of 0x17 (I don 't know in which convention).

If I may, I'm whiling to contribute an update to your repo and package so it can be easily consumed from npm. Right now, you are asking people to install typescript, which shouldn't be required since they may want to use it on JS as well.

Yes, you are right, including the compiled javascript tsc will not a dependency any more. You can make a PR when you want.

Best regards!

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