question

GeraldOakham-8014 avatar image
0 Votes"
GeraldOakham-8014 asked GeraldOakham-8014 commented

Create CRC from String

Hi,
I have been looking around the internet for a way to create a CRC from a String.
I have found many solutions, but the result I get out doesn't match what I know to be correct ( I am hardcoding the value at the moment, and the response I get back is correct ).

For example, If I send the following string to a device (- quotes),

     "A     273     SPA Reception 2          1        03                058"

I get an "ACK" response back.

All the code I have found and run gives me a different value to 058.

The value is created by XORing the Chars with the preceding one.

243496-screenshot-2022-09-21-172607.gif




Can someone please advise me on how I can, in C#, get the required answer?

thank you

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just so there's no ambiguity, can you point us to the protocol you're trying to code, so that we can see what the definition is?

0 Votes 0 ·

Hi, From the old manual I have :-

The last two characters preceding the "ETX" are reserved for the "CRC". It is calculated by making an

"exclusive or" on each character from the STX (excluded) to the "CRC". The result is split into 2 ASCII
characters corresponding to the translation in ASCII of its hexadecimal value.
For example, the one character result corresponding to binary "0001 0011" is split into the 2 ASCII
characters "1" and "3".


and then the picture from the original post.

I know if I send
"A 273 SPA Reception 2 1 03 "

the CRC excepted at the end is 058

So, If I send

"A 273 SPA Rec ption 2 1 03 "

with a CRC of 058, it errors, and the log says

CRC error : calculated value (1D) != message value (58)

if I run Viorel-1's code using the above string, then I get back

string=2D, crc1=32, crc2=44

If I send

"A 273 LearningIsFun 1 03 "

with a CRC of 058, I get

string=16, crc1=31, crc2=36

CRC error : calculated value (26) != message value (58)

So, the "string Value" seems to be 10 out each time (and adding "+10" to the calculations doesn't seem to increase the result by 10 ).

I hope this helps.

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered GeraldOakham-8014 commented

tweeting the original sample code, it return 58

 using System;
    
 class GFG
 {
     static int XorAscii(String str)
     {
         int ans = (str[0]);
         for (int i = 1; i < str.Length; i++) {
             ans = (ans ^ ((str[i])));
         }
         return ans;
     }
    
     // Driver code
     public static void Main(String[] args)
     {
         var str = "A     273     SPA Reception 2          1        03                0";
         var crc = XorAscii(str).ToString("X2");
         Console.WriteLine(str + crc);
     }
 }
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So the original string really did have all those white space characters in it - minus the 58 characters! :)

0 Votes 0 ·

THANK YOU.
I was not putting to "0" at the end of the link, so was coming up with a different number (068) every time

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered DavidLowndes-6766 commented

Check an example for your picture:

 string example = "D 3956";
    
 int crc = example.Aggregate( 0, ( a, b ) => a ^ b );
    
 string crc_s = crc.ToString( "X2" );
 byte crc1 = (byte)crc_s[0];
 byte crc2 = (byte)crc_s[1];
    
 // show the results:
 Console.WriteLine( "crc1={0:X2}, crc2={1:X2}", crc1, crc2 );

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi Viorel-1 ,

thank you for the quick reply.
when I substitute
"D 3956"
for
"A 273 SPA Reception 2 1 03 "

though, I still don't get 58, I get crc1=36, crc2=38 ( crc_s = 68 ).

I know 58 is the correct CRC as the device's error log shows what it gets against what it's expecting to get.

Ie (this is when I passed the wrong hardcoded CRC check )

 CRC error : calculated value (58) != message value (66)

Do you know how could be working this out, or how I could figure this out, as the example in the attached picture doesn't seem to produce the correct output, despite what the Manf says?

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 GeraldOakham-8014 ·

Do you know if the calculated value (58), which is shown in error message, is a decimal or hexadecimal number?


0 Votes 0 ·

Hi, according to the manual, is HEX, but that doesn't seem to tally with your code (thank you again by the way) 's output.

so, if it (your code) generates

string=2D, crc1=32, crc2=44

the error log on the device says

CRC error : calculated value (1D) != message value (58)



again, if it generates

string=16, crc1=31, crc2=36

the error reads

CRC error : calculated value (26) != message value (58)



0 Votes 0 ·
Show more comments