CRC-16 Modbus计算 [英] CRC-16 Modbus Calculation
本文介绍了CRC-16 Modbus计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai All,
我正在编写代码来计算CRC16 Modbus。但是给出了一些错误的价值任何人都可以纠正我.......
这是我的代码....我输入的是ASCII值
< span class =code-keyword>公共 函数 getCRC16( ByVal strInput 作为 字符串)
Dim lngCheck 作为 长
Dim 电源(< span class =code-digit> 7 )作为 整数
< span class =code-keyword> Dim I As 整数
< span class =code-keyword> Dim J 作为 整数
< span class =code-keyword> Dim Poly As Long
Dim CRC As < span class =code-keyword> Long
Dim TestBit As < span class =code-keyword> Boolean
Dim TestBit1 As < span class =code-keyword> Boolean
Dim TestBit2 As < span class =code-keyword> Boolean
Poly =& H1021
CRC =& HFFFF
对于 J = 0 要 7
功率(J)= 2 ^ J
下一步 J
对于 I = 1 Len(strInput) Step 2
lngCheck = Val( & H & Mid $(strInput,I, 2 ))
对于 J = 7 0 步骤 -1
如果(CRC 和 32768 )= 32768 然后
TestBit1 = True
Else
TestBit1 = False
结束 如果
如果(lngCheck 和 Power(J))= Power(J)那么
TestBit2 = True
Else
TestBit2 = False
结束 如果
TestBit = TestBit1 Xor TestBit2
CRC =(CRC 32767 )* 2
如果 TestBit = True 然后
CRC = CRC Xor Poly
结束 如果
下一步 J
下一步我
Dim tm p As String
tmp = Hex(CRC)
getCRC16 = tmp
MsgBox(tmp)
结束 功能
解决方案
(strInput,I, 2 ))
对于 J = 7 要 0 步骤 -1
如果(CRC 并且 32768 )= 32768 然后
TestBit1 = True
否则
TestBit1 = False
结束 如果
如果(lngCheck 和 Power(J))= Power(J)然后
TestBit2 = True
否则
TestBit2 = False
结束 如果
TestBit = TestBit1 Xor TestBit2
CRC =(CRC 32767 )* 2
如果 TestBit = True 然后
CRC = CRC Xor Poly
结束 如果
下一页 J
下一页 I
< span class =code-keyword> Dim tmp As String
tmp = Hex(CRC)
getCRC16 = tmp
MsgBox(tmp)
结束 功能
通常 CRC16
计算是基于表的(以获得更好的性能)。您可以在此处找到它:C#和VB.Net中的Modbus CRC16算法 [ ^ ]
Hai All,
While i am writing code to calculate CRC16 Modbus. But gives some wrong values. Can any one correct me .......
Here is my code.... my input is ASCII value
Public Function getCRC16(ByVal strInput As String)
Dim lngCheck As Long
Dim Power(7) As Integer
Dim I As Integer
Dim J As Integer
Dim Poly As Long
Dim CRC As Long
Dim TestBit As Boolean
Dim TestBit1 As Boolean
Dim TestBit2 As Boolean
Poly = &H1021
CRC = &HFFFF
For J = 0 To 7
Power(J) = 2 ^ J
Next J
For I = 1 To Len(strInput) Step 2
lngCheck = Val("&H" & Mid$(strInput, I, 2))
For J = 7 To 0 Step -1
If (CRC And 32768) = 32768 Then
TestBit1 = True
Else
TestBit1 = False
End If
If (lngCheck And Power(J)) = Power(J) Then
TestBit2 = True
Else
TestBit2 = False
End If
TestBit = TestBit1 Xor TestBit2
CRC = (CRC And 32767) * 2
If TestBit = True Then
CRC = CRC Xor Poly
End If
Next J
Next I
Dim tmp As String
tmp = Hex(CRC)
getCRC16 = tmp
MsgBox(tmp)
End Function
解决方案
(strInput, I, 2)) For J = 7 To 0 Step -1 If (CRC And 32768) = 32768 Then TestBit1 = True Else TestBit1 = False End If If (lngCheck And Power(J)) = Power(J) Then TestBit2 = True Else TestBit2 = False End If TestBit = TestBit1 Xor TestBit2 CRC = (CRC And 32767) * 2 If TestBit = True Then CRC = CRC Xor Poly End If Next J Next I Dim tmp As String tmp = Hex(CRC) getCRC16 = tmp MsgBox(tmp) End Function
Usually the CRC16
calculation is table-based (to achieve better performance). You may find it, for example here: "Modbus CRC16 Algorithm in C# and VB.Net"[^]
这篇关于CRC-16 Modbus计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文