×

MODBUS-RTU CRC校验码计算 VB源代码

Kalet Kalet 发表于2009-05-31 11:14:02 浏览370 评论0

抢沙发发表评论

中国PLC网最新消息: Public Function CRC(STR1 As String) As String
Dim CRCREG As Long
Dim MVAL As Long
Dim R As Integer


CRCREG = 65535


For R = 1 To Len(STR1) Step 2
MVAL = Val("%26amp;H" + Mid(STR1, R, 2))

CRCREG = CRCREG Xor MVAL
CRCREG = CRCREG And 65535


For T = 1 To 8 Step 1
If (CRCREG And %26amp;H1) Then
CRCREG = (CRCREG \ 2) Xor %26amp;HA001
CRCREG = CRCREG And 65535



Else

CRCREG = CRCREG \ 2
CRCREG = CRCREG And 65535
End If
Next

Next
Dim A As Long
Dim B As Long
A = CRCREG And 255
B = CRCREG And 65280
A = A * 256
B = B / 256

If (A + B) %26lt; 16 Then
CRC = "000" + Hex(A + B)
ElseIf (A + B) %26lt; 256 Then




CRC = "00" + Hex(A + B)
ElseIf (A + B) %26lt; 4096 Then
CRC = "0" + Hex(A + B)
Else
CRC = Hex(A + B)
End If
 

群贤毕至

访客