Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I calculate a Checksum?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
0
0
AU
Would anyone know how to calculate the correct CRC16 checksum for sending to a M30624 microcontroller?
I tried a number of CRC16 examples but get different answers.
I have a hardware device that works OK and produces the correct checksum EG. Data:- @CLP produces a hex checksum of E642
but I cant seem to find any routine that reproduces it. I want to drive it from my computer instead of the hardware device.

The controller docs says it uses ISO2111 checksum but I haven't seen anything in VB that does this.
Any help will be appreciated
 
Um .. do you have other examples of input versus output?

 
We are all lost children!
I worked out the polynominial was &H8408 and by borrowing from other examples came up with he following fast code that may be useful to anyone looking to communicate with robots etc that I believe this chip is used for.

Function CRC_Calc(CRC_Message As String) As String
Dim Polynomial16 As Long
Dim Y As Integer
Dim Char_Text_DEC As Long
Dim X As Integer
Y = 1
X = 0
CRC_Value = 0
Polynomial16 = 33800 'Polynomial &H8408
For Y = 1 To Len(CRC_Message)
CRC_Text_Single = Mid$(CRC_Message, Y, 1)
Char_Text_DEC = Asc(CRC_Text_Single)
For X = 1 To 8
LSB_CRC = CRC_Value And &H1
LSB_Char = Char_Text_DEC And &H1
If LSB_CRC = 1 And LSB_Char = 1 Or LSB_CRC = 0 And LSB_Char = 0 Then
CRC_Value = Fix(CRC_Value / 2)
Char_Text_DEC = Fix(Char_Text_DEC / 2)
ElseIf LSB_CRC = 0 And LSB_Char = 1 Or LSB_CRC = 1 And LSB_Char = 0 Then
CRC_Value = Fix(CRC_Value / 2)
Char_Text_DEC = Fix(Char_Text_DEC / 2)
CRC_Value = Polynomial16 Xor CRC_Value
Else
End If
Next X
Next Y
If Len(Hex(CRC_Value)) = 4 Then
CRC_LO = Mid$(Hex(CRC_Value), 3, 2)
CRC_HI = Mid$(Hex(CRC_Value), 1, 2)
ElseIf Len(Hex(CRC_Value)) = 3 Then
CRC_STRING = "0" & Hex(CRC_Value)
CRC_LO = Mid$(CRC_STRING, 3, 2)
CRC_HI = Mid$(CRC_STRING, 1, 2)
ElseIf Len(Hex(CRC_Value)) = 2 Then
CRC_STRING = "00" & Hex(CRC_Value)
CRC_LO = Mid$(CRC_STRING, 3, 2)
CRC_HI = Mid$(CRC_STRING, 1, 2)
ElseIf Len(Hex(CRC_Value)) = 1 Then
CRC_STRING = "000" & Hex(CRC_Value)
CRC_LO = Mid$(CRC_STRING, 3, 2)
CRC_HI = Mid$(CRC_STRING, 1, 2)
ElseIf Len(Hex(CRC_Value)) = 0 Then
CRC_LO = "00"
CRC_HI = "00"
End If
CRC_Calc = CRC_HI & CRC_LO
End Function

This gives the hex letters (EG 4A2F) but if you need asc characters to send to a comms port, replace the last line with
CRC_Calc = Chr("&h" & CRC_LO) & Chr("&h" & CRC_HI)

Also sometimes you need the checksum LSB & MSB the other way around depending on the receiving end.

The actual Send string became:-
StartCharacter & Message & CRC_Calc(Message) & StopCharacter

Eg. CRC_Calc = Chr("&h" & CRC_HI) & Chr("&h" & CRC_LO)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top