Program to calculate crc8


















It's manual discusses a the layout for a custom frame structure that is used to send data. The 8bit CRC is then converted to two hexadecimal values with the upper part being transmitted first. Thursday, April 9, AM. Something like this ought to do it. Generic; using System.

Linq; using System. ToString "X2" ; Result[input. The first one called CheckSum8 Xor. And in real code I would probably make it flexible. Aka, not hard code the 19 and split it into functions. The code is running great now! I can still improve the code here and there, per your suggestion.

After that I can implement it in my project. Usually up till now at least all canbus message I would send to the car were pre defined in ibusGlobals. Now when I create a new array not const of course and calculate the checksum afterwards, I want to send the new message to the car. When printing the new array after the calculation and checksum have been done, everything will show up as expected.

If I manually send the calculated array to the car with a different program, the correct lights will turn on. So the calculation and checksum are absolutely fine.

However, if I call the function to send the newly created array to the car, random characters will be sent. The array that will show up is even longer! I also tried defining the array next to the other const byte arrays, but then the compiler will throw an error:.

Don't do that. Data with all-upper-case names are traditionally definitions of constants usually instructions to the compiler, not part of the actual code. The register size that we use will always be equal to the width of the CRC we're calculating. As long as we're cleaning up the code, we should also recognize that most CRCs are computed over fairly long messages.

The entire message can usually be treated as an array of unsigned data bytes. The CRC algorithm should then be iterated over all of the data bytes, as well as the bits within those bytes. The result of making these two changes is the code shown in Listing 2. This implementation of the CRC calculation is still just as inefficient as the previous one. However, it is far more portable and can be used to compute a number of different CRCs of various widths. The most common way to improve the efficiency of the CRC calculation is to throw memory at the problem.

For a given input remainder and generator polynomial, the output remainder will always be the same. If you don't believe me, just reread that sentence as "for a given dividend and divisor, the remainder will always be the same. So it's possible to precompute the output remainder for each of the possible byte-wide input remainders and store the results in a lookup table.

That lookup table can then be used to speed up the CRC calculations for a given message. The speedup is realized because the message can now be processed byte by byte, rather than bit by bit. The code to precompute the output remainders for each possible input byte is shown in Listing 3. The computed remainder for each possible byte-wide dividend is stored in the array crcTable[].

In practice, the crcInit function could either be called during the target's initialization sequence thus placing crcTable[] in RAM or it could be run ahead of time on your development workstation with the results stored in the target device's ROM. You'll also need a function to compute the CRC of a given message that is somehow able to make use of the values stored in that table. Without going into all of the mathematical details of why this works, suffice it to say that the previously complicated modulo-2 division can now be implemented as a series of lookups and XORs.

In modulo-2 arithmetic, XOR is both addition and subtraction. A function that uses the lookup table contents to compute a CRC more efficiently is shown in Listing 4. The amount of processing to be done for each byte is substantially reduced. As you can see from the code in Listing 4, a number of fundamental operations left and right shifts, XORs, lookups, and so on still must be performed for each byte even with this lookup table approach. The results of this experiment were as follows:.

So, at least on one processor family, switching to the lookup table approach results in a more than five-fold performance improvement. That's a pretty substantial gain considering that both implementations were written in C. A bit more could probably be done to improve the execution speed of this algorithm if an engineer with a good understanding of the target processor were assigned to hand-code or tune the assembly code.

My somewhat-educated guess is that another two-fold performance improvement might be possible. Actually achieving that is, as they say in textbooks, left as an exercise for the curious reader.

Now that we've got our basic CRC implementation nailed down, I want to talk about the various types of CRCs that you can compute with it. As I mentioned last month, several mathematically well understood and internationally standardized CRC generator polynomials exist and you should probably choose one of those, rather than risk inventing something weaker.

In addition to the generator polynomial, each of the accepted CRC standards also includes certain other parameters that describe how it should be computed. Table 1 contains the parameters for three of the most popular CRC standards. Improve this answer. Mark Adler Mark Adler Kami Kaze Kami Kaze 2, 12 12 silver badges 28 28 bronze badges. You mistake your input for 8 bit on the website, you have to enter for the correct calculation.

For different length you would have to add a length parameter other wise your input is always 2bytes — Kami Kaze. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.



0コメント

  • 1000 / 1000