0

I'm looking for some help please. I'm trying to communicate to a device over TCP / IP using ASCII. The protocol includes a checksum that consists of two ASCII characters that represent a two character hexadecimal number in the range 00 through FF.

I know that the hexadecimal number is generated by performing a modulo-256 10 summation of all previous characters in the frame (that is, over <STX> … <ETX>, inclusive) and then expressing the resulting 8-bit unsigned integer in hexadecimal format.

For example I know that this checksum is 84, but how is that calculated? <STX>ID_DATA<FS><RS>aMOD<GS>LIS<GS><GS><GS><FS>iIID<GS>333<GS><GS><GS><FS><RS><ETX>84<EOT>

And that being said, what would the checksum be for this? <STX>SMP_REQ<FS><RS>aMOD<GS>LIS<GS><GS><GS><FS>iIID<GS>42731<GS><GS><GS><FS>rSEQ<GS>16<GS><GS><GS><FS><RS><ETX>{chksum}<EOT>

Any guidance is greatly appreciated? :) TIA!

1 Answers1

0

Late is better than never. Here is how to get the checksum:

public const byte STX = 2;
public const byte FS = 28;
public const byte GS = 29;
public const byte RS = 30;
public static byte[] STX_BUFF = { STX };
public static byte[] FS_BUFF = { FS };
public static byte[] GS_BUFF = { GS };
public static byte[] RS_BUFF = { RS };

string checksum = "00";
int byteVal = 0;
int sumOfChars = 0;
string frame = string.Format("{0}SMP_REQ{1}{2}aMOD{3}0500{3}{3}{3}{1}iIID{3}{6}{3}{3}{3}{1}rSEQ{3}{5}{3}{3}{3}{1}{2}{4}", Encoding.ASCII.GetString(STX_BUFF), Encoding.ASCII.GetString(FS_BUFF), Encoding.ASCII.GetString(RS_BUFF), Encoding.ASCII.GetString(GS_BUFF), Encoding.ASCII.GetString(ETX_BUFF), rSEQ, iIID);

// obtiene el checksum
for (int i = 0; i < frame.Length; i++)
{
   byteVal = Convert.ToInt32(frame[i]);
   sumOfChars += byteVal;
}
checksum = Convert.ToString(sumOfChars % 256, 16).ToUpper();
if (checksum.Length == 1) checksum = "0" + checksum;

frame += string.Format("{0}{1}", checksum, Encoding.ASCII.GetString(EOT_BUFF));