Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.60/5 (3 votes)
See more:
C#
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:

C#
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;
       }


C#
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();
       }


C#
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;
       }
Posted
Updated 5-Sep-18 21:17pm

C#
public static byte [] chk( byte [] buffer)
{
  int sum = 0;
  foreach ( var b in buffer)
    sum += b;
  sum %= 0x100;
  byte [] ch = new byte[2];
  ch[0] = (byte) ((sum >> 4) + 0x30);
  ch[1] = (byte) ((sum & 0xF) + 0x30);
  return ch;
}
 
Share this answer
 
Comments
EssenceGold 8-Dec-16 10:25am    
As an example, the calculation of that sequence. Please include all codes. Thanks.

<STX>.....D.BI|1234ABC5678|30500000|?7<ETX>
CPallini 8-Dec-16 15:42pm    
What does the dot ('.') stand for? What does the pipe ('|') stand for?
EssenceGold 8-Dec-16 16:21pm    
..... Filled with blanks ( Space by number of dots )

| divider. Pipe symbol ASCII 0x7C (I'm not sure about this) There is no information on the article about it
CPallini 8-Dec-16 16:26pm    
You have to be sure. Should the divider be included or not in the checksum computation?
EssenceGold 8-Dec-16 16:32pm    
Works in two ways.

Dataflow examples for Protocol version 1
HCTS -> LIS <STX>·····D·BI01234ABC567830500000;3<ETX>

Dataflow examples for Protocol version 2
HCTS -> LIS <STX>·····D·BI|1234ABC5678|30500000|?7<ETX>


But the checksum is different. Probably included in the calculation. Protocol 1 using 0 ZERO
Protokol 2 using | pipe

My head mixed well :(
C#
// Full data : <STX>     D BI|1234ABC5678|30500000|?7<ETX>

// Data to be calculated = <STX>     D BI|1234ABC5678|30500000|

// Result = ?7

C#
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;
       }

C#
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();
       }

C#
private static string Csum(string data)
       {
           int total = StringToByteArray(data).Sum(x => x);
           total %= 0x100;
           string totalStr = string.Format("{0:x2}", total);
           int hexa = int.Parse(totalStr.Substring(0, 1), NumberStyles.HexNumber) + 0x30;
           int hexb = int.Parse(totalStr.Substring(1, 1), NumberStyles.HexNumber) + 0x30;
           char a = (char)hexa, b = (char)hexb;
           data = a + b.ToString();
           return data;
       }

C#
private void button2_Click(object sender, EventArgs e)
      {
          try
          {
              string hexstring = "02" + ConvertToHex(dataBox.Text.Replace("<STX>", ""));            
              sumBox.Text = Csum(hexstring);
          }
          catch (Exception)
          {
              sumBox.Text = "??";
          }
      }
 
Share this answer
 
v3
private string Pruefsumme(string buffer)
{
int i;
int sum;
sum = 256;
for (i = 0; i < buffer.Length; i++)
{
sum -= buffer[i];
if (sum < 0)
{
sum += 256;
}
}
Pruefsumme_a1 = (sum & 0xF0) >> 4;
a2 = sum & 0x0F;
Pruefsumme_Hex = StringFunctions.ChangeCharacter(Pruefsumme_Hex, 0, ASCII[Pruefsumme_a1]);
Pruefsumme_Hex = StringFunctions.ChangeCharacter(Pruefsumme_Hex, 1, ASCII[a2]);
Pruefsumme_Hex = Pruefsumme_Hex.Substring(0, 2);
return (Pruefsumme_Hex);
}


internal static class StringFunctions
{

public static string ChangeCharacter(string sourceString, int charIndex, char newChar)
{
return (charIndex > 0 ? sourceString.Substring(0, charIndex) : "")
+ newChar.ToString() + (charIndex < sourceString.Length - 1 ? sourceString.Substring(charIndex + 1) : "");
}

public static bool IsXDigit(char character)
{
if (char.IsDigit(character))
return true;
else if ("ABCDEFabcdef".IndexOf(character) > -1)
return true;
else
return false;
}


public static string StrChr(string stringToSearch, char charToFind)
{
int index = stringToSearch.IndexOf(charToFind);
if (index > -1)
return stringToSearch.Substring(index);
else
return null;
}


public static string StrRChr(string stringToSearch, char charToFind)
{
int index = stringToSearch.LastIndexOf(charToFind);
if (index > -1)
return stringToSearch.Substring(index);
else
return null;
}


public static string StrStr(string stringToSearch, string stringToFind)
{
int index = stringToSearch.IndexOf(stringToFind);
if (index > -1)
return stringToSearch.Substring(index);
else
return null;
}

private static string activeString;
private static int activePosition;
public static string StrTok(string stringToTokenize, string delimiters)
{
if (stringToTokenize != null)
{
activeString = stringToTokenize;
activePosition = -1;
}


if (activeString == null)
return null;

if (activePosition == activeString.Length)
return null;

activePosition++;
while (activePosition < activeString.Length && delimiters.IndexOf(activeString[activePosition]) > -1)
{
activePosition++;
}

if (activePosition == activeString.Length)
return null;

int startingPosition = activePosition;
do
{
activePosition++;
} while (activePosition < activeString.Length && delimiters.IndexOf(activeString[activePosition]) == -1);

return activeString.Substring(startingPosition, activePosition - startingPosition);
}

}
 
Share this answer
 
Comments
Ztibi 6-Sep-18 3:19am    
Just string data = "{STX}1"; ---->> D8 I hope I could help. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900