Click here to Skip to main content
11,640,445 members (63,254 online)
Click here to Skip to main content

CRC Encoding

, 6 Dec 2003 206.2K 5.5K 65
Rate this:
Please Sign up or sign in to vote.
An article on using CRCxxx checksum calculations in C#, combined with the serialstreamreader example found on this site.

Introduction

A lot of medical devices use the RS232 port to communicate and they all use a kind of CRC algorithm. Since a few months I tried to migrate to .NET and C#. However a lot of the old things just don't exist in C#. So you have to write or port them yourself.

This code can generate any CRCXX calculation, as long as XX is 8, 16, 32. The CCITT is just 16 but with a different polynomial. The project also uses the serialstream sample on this site. I used the demo project to communicate with an existing device.

Background

In this section I won't explain the CRC checksum calculation. A lot of the stuff can be found on the web. Especially on the site of Rocksoft, the original designers.

Just to introduce it:

Cycle Redundancy Check is added to a communication protocol to make sure no data is lost or has been changed. It is a far more advanced version of the parity check in the basic RS232 communication protocol. It uses a polynomial to create a unique checksum, so if somewhere a bit is changed, the total checksum is also changed.

Using the code

To use the code, just add the CRCTool.cs class to your C# project and add the namespace to your class:

        using Communication.IO.Tools

Add the CRCTool to the class in which you'll be using it.

class Class1
    {
        /// <SUMMARY>
        /// The main entry point for the application.
        /// </SUMMARY>
        /// 

        .......
        // Crc Computation Class
        private static CRCTool compCRC = new CRCTool();
}

Tell the class which kind of CRC you'll be using. Most functions are generic for all kinds of CRC encoding. The Init function is used to specify to start values for each kind of encoding: CRC16, CRC_ITT, CRC32. It also creates the encoding tables needed for the fastest encoding algorithm!

When you want to change the type of CRC, call Init again.

        // use of these values,
        // enum CRCCode{CRC_CCITT, CRC16, CRC32};
        // if the protocol you need is not here
        // you can add it to this enum and change the relevant parameters
        // in the Init function.       
        compCRC.Init(CRCTool.CRCCode.CRC_CCITT);

There are four ways to calculate the CRC checksum value and a fifth one to show you another way. The difference it the speed of calculation, with the crctablefast begin the fastest and the crcbitbybit the slowest.

However there are situations in which one could be preferred for the other. Keep, for example, in mind that the calculation of the lookup table also takes time. So for one time calculation this would be slower.

     //Four function with a little different algoritm but the same endresult.
     public ulong crcbitbybit(byte[] p); 
     public ulong crcbitbybitfast(byte[] p); 
     public ulong crctable(byte[] p); 
     public ulong crctablefast (byte[] p);
         
     // Not a generic but an example of how to do everything in one function 
     public ushort CalcCRCITT(byte[] p);
        

I have added it to the serialstream example and now the command writing code is changed to this. I use the CRCITT 16 bits encoding. The device I talk to needed a ! in front of the message and a |XXXX\r as end of the message with XXXX being the CRCITT value.

         // Write command to port
         byte[] rawBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(command);
              
         compCRC.Init(CRCTool.CRCCode.CRC_CCITT);
         ushort usCrc16 = (ushort)compCRC.crctablefast(rawBytes);

         // The following is equal to 
         // sprintf( message,"!%s|%04X\r", buf, crc );
         string sMessageString = 
            String.Format("!{0}|{1:X4}\r",command,usCrc16);
         sw.WriteLine(sMessageString );
         sw.Flush();
        

I have tested it with CRC16, CRCITT, CRC32, and all algorithms worked fine.

Points of Interest

Nothing of interest yet. It depends on how you take the article! Please keep me informed about your experiences!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marcel de Wijs
Web Developer
Netherlands Netherlands
Just like most a C++/C# programmer who spent his life programming in a medical environment.

Apart from programming i love my wife, reading, and sailing.

You may also be interested in...

Comments and Discussions

 
GeneralThanks a lot Pin
Member 1145397415-Feb-15 8:46
memberMember 1145397415-Feb-15 8:46 
QuestionIt appears the zip file is corrupted Pin
turbosupramk324-Oct-14 3:09
memberturbosupramk324-Oct-14 3:09 
QuestionLicense? Pin
Pandu E Poluan9-Jun-11 17:33
memberPandu E Poluan9-Jun-11 17:33 
QuestionI need checksum in numeric value Pin
RashmitaS8-Jun-11 21:52
memberRashmitaS8-Jun-11 21:52 
Generalcrc translate code from C to C# Pin
A4ad27-Jan-09 1:01
memberA4ad27-Jan-09 1:01 
GeneralRe: crc translate code from C to C# Pin
hdjim13-Oct-09 7:00
memberhdjim13-Oct-09 7:00 
Question8Bit CRC Pin
Florian Leitner17-May-07 21:39
memberFlorian Leitner17-May-07 21:39 
QuestionBig Endian or Little Endian? Pin
SwamiDot20-Dec-06 14:57
memberSwamiDot20-Dec-06 14:57 
AnswerRe: Big Endian or Little Endian? Pin
JDSteampunk7-Feb-07 9:48
memberJDSteampunk7-Feb-07 9:48 
GeneralEnglishmen Pin
billfoster30-Oct-06 6:33
memberbillfoster30-Oct-06 6:33 
GeneralI need checksum in hex Pin
Julnana12-Oct-06 17:11
memberJulnana12-Oct-06 17:11 
GeneralRe: I need checksum in hex Pin
Marcel de Wijs12-Oct-06 20:44
memberMarcel de Wijs12-Oct-06 20:44 
AnswerRe: I need checksum in hex Pin
Horia Tudosie7-Apr-07 10:36
memberHoria Tudosie7-Apr-07 10:36 
QuestionOperator '!=' cannot be applied to operands of type 'ushort' and 'string'" Pin
Muzilli9-Jun-06 16:51
memberMuzilli9-Jun-06 16:51 
AnswerRe: Operator '!=' cannot be applied to operands of type 'ushort' and 'string'" Pin
XTV8-Oct-07 2:32
memberXTV8-Oct-07 2:32 
GeneralCRCTool Returns Inaccurate Answers... Pin
Code Deamon16-Nov-04 23:49
memberCode Deamon16-Nov-04 23:49 
GeneralRe: CRCTool Returns Inaccurate Answers... Pin
Code Deamon19-Nov-04 7:01
memberCode Deamon19-Nov-04 7:01 
GeneralRe: CRCTool Returns Inaccurate Answers... Pin
SoftwareDrone21-Apr-06 10:45
memberSoftwareDrone21-Apr-06 10:45 
GeneralRe: CRCTool Returns Inaccurate Answers... Pin
Bo Skjoett31-Jul-06 23:38
memberBo Skjoett31-Jul-06 23:38 
GeneralRe: CRCTool Returns Inaccurate Answers... Pin
LiamD1-Sep-06 2:46
memberLiamD1-Sep-06 2:46 
There seems to be a number of on-line calculators that return different values for the "123456789" test string.

0xE5CC or 0x29B1

http://www.joegeluso.com/software/articles/ccitt.htm thinks that the result should be E5CC.

But it looks like this author does not.

Does anyone know what the correct result should be?
GeneralRe: CRCTool Returns Inaccurate Answers... [modified] Pin
balazs_hideghety25-Oct-06 0:15
memberbalazs_hideghety25-Oct-06 0:15 
GeneralRe: CRCTool Returns Inaccurate Answers... Pin
A4ad27-Jan-09 3:10
memberA4ad27-Jan-09 3:10 
GeneralModbus Pin
David Gallagher12-Jan-04 6:48
memberDavid Gallagher12-Jan-04 6:48 
GeneralRe: Modbus Pin
Marcel de Wijs12-Jan-04 21:20
memberMarcel de Wijs12-Jan-04 21:20 
GeneralRe: Modbus Pin
PaulJ11-Feb-04 7:51
memberPaulJ11-Feb-04 7:51 
GeneralRe: Modbus Pin
kelsner26-Oct-05 4:17
memberkelsner26-Oct-05 4:17 
GeneralRe: Modbus Pin
pmartike6-Jun-07 4:13
memberpmartike6-Jun-07 4:13 
GeneralProject not opening Pin
Nikhil alulkar9-Dec-03 21:49
memberNikhil alulkar9-Dec-03 21:49 
GeneralRe: Project not opening Pin
Marcel de Wijs9-Dec-03 23:18
memberMarcel de Wijs9-Dec-03 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 7 Dec 2003
Article Copyright 2003 by Marcel de Wijs
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid