|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA 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. BackgroundIn 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:
Using the codeTo 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 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 When you want to change the type of CRC, call // 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 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 InterestNothing of interest yet. It depends on how you take the article! Please keep me informed about your experiences!
|
||||||||||||||||||||||