03-11-2015 08:50 AM
Hi everyone,
I have a problem with CRC generator that I found on this forum and I was hopeing that someone can help me.
I must send following structure to device:
You see at the end that I must calculate CRC-16 but the vi that I use does not give me the right values and I don't know why. (have in mind I'm total beginner in CRC)
This is what the result should look like:
STX | Slave ID | LENGTH | DATA | CRCL | CRCH |
7F | 80 | 01 | 11 | 65 | 82 |
7F | 80 | 01 | 0C | 2B | 82 |
7F | 80 | 01 | 05 | 1D | 82 |
7F | 00 | 09 | 4A 2B6D 4F25 0000 0000 | AA | 5A |
7F | 80 | 09 | 4B 51EF D04F 0000 0000 | 7D | 6D |
7F | 00 | 09 | 4C CA2D 222C 0000 0000 | 5A | 42 |
Please help!
Solved! Go to Solution.
03-11-2015 09:02 AM - edited 03-11-2015 09:08 AM
The first issue is that there are about a thousand different ways to calculate a CRC. You first of all need to make sure you are using the right algorithm. The documentation on your device gives you examples. All you have to do is find the one that gives the correct result. The good news is that there are 3 or 4 common ones that are used most often. The way a CRC is defined in by the polynomial that defines it. Google CRC and the article will fill in a lot of blanks for you.
Like, the one you are using is defined as:
CRC-16-IBM |
Mike...
PS: You also need to make sure that you are including the right things (the device documentation will tell you what to include in the CRC), that the resulting bytes are put in the right order, and that the algorithm is initialized correctly, this algorithm can be initialized with 0xFFFF, 0x0000 or some custom value. Also note that if you are wanting to check the response, if you run the same CRC on the entire message including the CRC the device transmitted, the result will always be 0x0000.
03-11-2015 09:08 AM
The first issue is that there are about a thousand different ways to calculate a CRC. You first of all need to make sure you are using the right algorithm. The documentation on your device gives you examples. All you have to do is find the one that gives the correct result. The good news is that there are 3 or 4 common ones that are used most often. The way a CRC is defined in by the polynomial that defines it. Google CRC and the article will fill in a lot of blanks for you.
Like, the one you are using is defined as:
CRC-16-IBM |
Mike...
PS: You also need to make sure that you are including the right things (the device documentation will tell you what to include in the CRC), that the resulting bytes are put in the right order, and that the algorithm is initialized correctly, this algorithm can be initialized with 0xFFFF, 0x0000 or some custom value. Also note that if you are wanting to check the response, if you run the same CRC on the entire message including the CRC the device transmitted, the result will always be 0x0000.
03-11-2015 10:33 AM - edited 03-11-2015 10:49 AM
Thank you for answer mike,
device documentation says thic about CRC:
So the method is CRC-16, Polynomial (0x8005 or 0x1021) and seed 0xFFFF.
When I use seed 0x0FFFF and plynomial 0x8005 for first sequence --> 80 01 11 (all except STX) I get result 1F 94 insted 65 82
When I use seed 0x0FFFF and plynomial 0x1021 for first sequence --> 80 01 11 I get result 1F 94 insted 65 82
With polynomial 0xA001 the result is B1 B4
With polynomial 0xC002 the result is 6A 2B
(http://en.wikipedia.org/wiki/Polynomial_representations_of_cyclic_redundancy_checks)
Any idea why?
03-11-2015 11:39 AM
This would not be the first time I ran across some "Documentation" of a CRC calculation that diverged between what the tech writer wrote and what the firmware engineer actually implemented in both sides of the code he used to validate his CRC implementation was correct. (guess what? it wasn't but the code worked and it sort of looked like a CRC to him)
This gets painful fast and you are going to need to see an example of a working algorithym and duplicate it.
03-11-2015 03:40 PM
One problem that I can see is that you are converting every character in the string to a byte and NOT the numbers them self.
So a string of "80 01 11" will become 0x38, 0x30, 0x30, 0x31, 0x31 and 0x31.
03-11-2015 04:00 PM - edited 03-11-2015 04:00 PM
This seems to work...
03-11-2015 04:19 PM
@crossrulz wrote:
This seems to work...
I have no Idea how you reverse engineerd that! no XOR between Data In[i] and Remainder Polynomial. Yeah, that's not a "CRC" in a strict sense. but probably what got implemented.
My hats off to you my friend
03-11-2015 04:37 PM
03-11-2015 04:49 PM
@mikeporter wrote:
There are a variety of potential algorithms for accomplishing the same mathematical operation -- including one that optimizes operation by generating a lookup table of intermediate values...
Mike...
Yeah, but this isn't one of those methods that generate the same result.