Hii guys. I have a problem. Can you help me. There is an article on the elimination. According to this article I can not find Checksum. Here are a few examples and articles;
Articles:
2.1 Framing
Each data transfer takes place within a frame. Each data packet has the following structure:
<STX> FL FN FI [DATA] Checksum <ETX>
<STX> 1 Byte (02hex) Start of frame
FL 3 Byte Frame length, not used currently, filled with blanks (20hex)
FN 2 Byte Frame number, not used currently, filled with blanks (20hex)
FI 2 Byte Frame identifier, only ‘E’, ‘I’, ‘D’ and ‘S’ used for 1. Byte
currently, 2. Byte always blank (20hex)
[DATA] Application data (if present)
Checksum 2 Byte Checksum
<ETX> 1 Byte (03hex) Frame termination
2.2 Error detection and flow control
In order to ensure data integrity, a checksum is transmitted with each frame. This checksum
is calculated by adding the values of all transferred bytes from <stx> to [DATA] (both
inclusive), modulus 100hex. The sum is devided by 10hex. 30hex is added to both the
quotient and the remainder of the division. The results (ASCII ‘0’ to ‘?’) represent the 2- character checksum. For example, the sum 3f9hex will result in “?9”.
Sample Communication Log;
HCTS -> LIS <STX>.....D.BI|1234ABC5678|30500000|?7<ETX> --- Checksum ?7
LIS -> HCTS <ACK>
LIS -> HCTS <STX>.....D.DW|1234ABC5678|||CHOL....BILI_TOT|5=<ETX> --- Checksum 5=
HCTS -> LIS: <ACK>
HCTS -> LIS <STX>.....D.BI|1234ABC5678|D050000F|1><ETX> -----Checksum 1>
LIS -> HCTS <ACK>
HCTS -> LIS <STX>.....D.BI|9555907034|A000000B|;7<ETX> ------Checksum ;7
HCTS -> LIS <STX>.....D.BI||A000000Z|<0<ETX> ------- Checksum <0
Not: ..... = blank
I did not calculate the Checksum. Where am i making mistakes.
What I have tried:
public string ConvertToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += string.Format("{0:X2}", Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
public static byte[] StringToByteArray(string text)
{
return Enumerable.Range(0, text.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(text.Substring(x, 2), 16))
.ToArray();
}
private void button2_Click(object sender, EventArgs e)
{
string hex = ConvertToHex(dataBox.Text);
int total = StringToByteArray(hex).Sum(x => x);
string totalStr = string.Format("{0:x}", total);
resultBox.Text = totalStr;
}