首页上一页 1 下一页尾页 1 条记录 1/1页
“\ 2”是什么意思?
发表在VB答疑区
2013-03-02
是否精华
是
否
版块置顶:
是
否
MODBUS-RTU CRC校验码计算 VB源代码
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("&H" + Mid(STR1, R, 2))
CRCREG = CRCREG Xor MVAL
CRCREG = CRCREG And 65535
For T = 1 To 8 Step 1
If (CRCREG And &H1) Then
CRCREG = [font color=#FF0000](CRCREG \ 2)[/font] Xor &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) < 16 Then
CRC = "000" + Hex(A + B)
ElseIf (A + B) < 256 Then
CRC = "00" + Hex(A + B)
ElseIf (A + B) < 4096 Then
CRC = "0" + Hex(A + B)
Else
CRC = Hex(A + B)
End If
End Function
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("&H" + Mid(STR1, R, 2))
CRCREG = CRCREG Xor MVAL
CRCREG = CRCREG And 65535
For T = 1 To 8 Step 1
If (CRCREG And &H1) Then
CRCREG = [font color=#FF0000](CRCREG \ 2)[/font] Xor &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) < 16 Then
CRC = "000" + Hex(A + B)
ElseIf (A + B) < 256 Then
CRC = "00" + Hex(A + B)
ElseIf (A + B) < 4096 Then
CRC = "0" + Hex(A + B)
Else
CRC = Hex(A + B)
End If
End Function