Electronic and Electric Circuit Simulation
Home C Sharp Code Examples c sharp class for crc checksum calculator
c sharp class for crc checksum calculator PDF Print E-mail

 

About CRC checksum calculus, you can also read:

A cyclic redundancy check (CRC) is an error-detecting code. It is very useful whenever there is digital communication or data transfer (together with data compression) is involved. Since during bite transmission one or more errors can occur, it is important to have some verification of transferred data validity. Idea is to treat all transferred bits as one extremely large number. Dividing this number with certain constant and obtaining the reminder, we obtain checksum byte or bytes. Practically, reminder is obtained with bitwise binary operation, without actually doing division calculus. There are different crc codes such as CRC-16 (Modbus), CRC-32, CRC-5-USB, with corresponding divisors. In order to have  c sharp crc checksum calculator, we will develop C# crc checksum class for CRC-16 (Modbus). C# crc checksum class actually needs no constructor, since it can be applied many times and only one object of this class is enough. So C Sharp crc checksum class needs to have to public bytes (as the outcome of the calculus), one private integer array (data that checksum is calculated for) and one method for calculating checksum.

public class crc
{

public int int_crc_byte_a, int_crc_byte_b;
int [] int_array = new int[100];

Calculating method has to be public and of void type. It should take array as an argument. Code is going like this:

public void crc_bytes(int[] int_input)
{

int_array = int_input;
int int_crc = 0xFFFF;
int int_lsb;
for (int int_i = 0; int_i < int_array.Length; int_i++)
{

int_crc = int_crc ^ int_array[int_i];
for (int int_j = 0; int_j < 8; int_j ++ )
{

int_lsb = int_crc & 0x0001;  // Mask of LSB
int_crc = int_crc >> 1;
int_crc = int_crc & 0x7FFF;
if (int_lsb == 1) int_crc = int_crc ^ 0xA001;

}

}

int_crc_byte_a = int_crc & 0x00FF;
int_crc_byte_b = (int_crc >> 8) & 0x00FF;

}

Whole code is following:

public class crc

{

public int int_crc_byte_a, int_crc_byte_b;
int [] int_array = new int[100];
public void crc_bytes(int[] int_input)
{

int_array = int_input;
int int_crc = 0xFFFF;
int int_lsb;
for (int int_i = 0; int_i < int_array.Length; int_i++)
{

int_crc = int_crc ^ int_array[int_i];
for (int int_j = 0; int_j < 8; int_j ++ )
{

int_lsb = int_crc & 0x0001;  // Mask of LSB
int_crc = int_crc >> 1;
int_crc = int_crc & 0x7FFF;
if (int_lsb == 1) int_crc = int_crc ^ 0xA001;

}


}

int_crc_byte_a = int_crc & 0x00FF;
int_crc_byte_b = (int_crc >> 8) & 0x00FF;

}


}

static void Main(string[] args)
{

int[] int_input = new int[6];
int_input[0] = 0x0001;
int_input[1] = 0x0003;
int_input[2] = 0x001D;
int_input[3] = 0x0063;
int_input[4] = 0x0000;
int_input[5] = 0x0002;


crc crc_calculate = new crc();
crc_calculate.crc_bytes(int_input);
Console.WriteLine("a:" + crc_calculate.int_crc_byte_a + " b:" + crc_calculate.int_crc_byte_b );

}

This class can be easily adjusted for some other check sum calculus, apart from CRC-16 (Modbus)

Download project

<< Previous reading Next reading >>

External links:

C Sharp CRC Checksum on lammertbies
C Sharp CRC Checksum on stackoverflow
C Sharp CRC Checksum on Wikipedia

 
Copyright © 2014 Electronic and Electric Circuit Simulation. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License. Top joomla 1.5 themes
Top joomla 1.5 themes designed by Lonex.

Recent/Mostly Visited

Simulate recent1:Forward Converter

Simulate recent2:Sawtooth Generator

Simulate recent3:Flyback Converter