469,579 Members | 1,026 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Programming Serial Port

JT
I have been tasked with a project that needs to communicate with a
serial port device, a contactless smart card reader. In documentation
for the device, a sample code to compute the checksum for the packet
data is written in C.

I am not familar with C and can anyone tell me if the code i have
written to replicate the checksum method in C# is correct... Thanks in
advance.

C Method:
/**
* Syntax:
* U8 ComputeCheckSum(U8 *bDataLn, U8 bLength)
* Description:
* Compute the checksum for packet length and data packets.
* In:
* *bDataLn -> Pointer to the packet length byte of data packets
* bLength -> 1 for the packet length;
* number of bytes for the data packets
* Out:
* None
* Return:
* One byte of the final checksum.
* Usage:
* To calculate the packet length checksum:
* Eg., length = 0x09:
* U8 bCheckSum;
* bCheckSum = ComputeCheckSum(0x09, 1);
*
* To calculate the data checksum:
* Eg., data (hex string) is "\x05\x00\x10\x00\x46" and data length = 0x05:
* U8 bCheckSum;
* bCheckSum = ComputeCheckSum("\x05\x00\x10\x00\x46", 0x05);
*/
U8 ComputeCheckSum(U8 *bDataLn, U8 bLength)
{
U8 i, bData = 0;
for (i=0; i<bLength; i++)
bData = bData - bDataLn[i];
return bData;
}

C# Method:
private byte ComputeCheckSum(byte[] pData, int pLength)
{
byte byteData = 0;
for (int i=0; i<pLength; i++)
{
byteData = (byte)(byteData - pData[i]);
}
return byteData;
}

Nov 16 '05 #1
0 1472

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ^CeFoS^ | last post: by
3 posts views Thread by rusttree | last post: by
3 posts views Thread by Math55 | last post: by
1 post views Thread by pei_world | last post: by
7 posts views Thread by Brian | last post: by
1 post views Thread by Z.K. | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.