CRC calculation wrong output

Viewed 123

I am trying to figure out a CRC check for a serial controlled device. I have an example, when I send this example to the device, it responds correctly.

This is the complete serial string which the device responds to:

\x00\x17\x3d\x30\x32\x32\x30\x39\x39\x30\x30\x30\x30\x30\x30\x30\x30\x30\x37\x34\x30\x30\x30\x30\x30\x01\x28

(The last 2 bytes (\x01\x28) are the CRC outcomes).

This is my code:

Dim Send As String
Dim CRC1 As String
Dim CRC2 As String
Dim TEMP As String

Private Sub Command1_Click()

Send = &H0 & &H17 & "=" & "022" & "099" & "00" & "00" & "0000074" & "00000"

CRC1 = &H0
CRC2 = &H0
TEMP = &H0

For i = 1 To Len(Send)
    TEMP = CRC1
    CRC1 = CRC2 Xor Asc(Mid$(Send, i, 1))
    CRC2 = TEMP
   
Next i

Text1.Text = "CRC1= " & CRC1 & " / CRC2= " & CRC2

End Sub

The output should be: CRC1 = 1 (decimal) and CRC2 = 40 (decimal) But I am getting 51/60.

I think is has something to do with datatypes.

This is the original CRC formula from the device:

Set <CRC1> and <CRC2> to zero.
For every <CHAR> in <MSG> do
<TEMP> = <CRC1>
<CRC1> = <CRC2> XOR <CHAR>
<CRC2> = <TEMP>

Thanks in advance!

1 Answers

You should have looked at your string Send to make sure that it's what you wanted. It isn't. Its construction needs to be:

Send = Chr(&H0) & Chr(&H17) & ...

Then you get the answer you're looking for.

By the way, the thing you are computing is not in any way a CRC. Whoever wrote that formula had no idea what they were talking about. What they constructed is a lousy check value algorithm, so they also had no idea what they were doing.

Related