CRC XModem/ZModem解决方案 [英] CRC XModem/ZModem solution
问题描述
我需要计算VB2008中某些电报的CRC,但不确定是X还是Zmodem.老实说,我无法理解该算法,因为我看到了一些带有查询表的表,其他带有策略,移位的表...
我使用的这些电报示例以及CRC计算中使用的数据在0x02和0x04之间(不包括02和04).
STX ADDR数据EOT CRCH CRCL ETX
02 38 51 4D 4E 30 30 23 04 F1 D7 03
02 38 51 4D 4E 31 30 04 EC CC 03
02 38 51 4D 4E 32 30 52 04 27 D7 03
我想知道是否没有流程图解释如何计算CRC,我可以使用的DLL甚至VB中的代码.
如果有人可以帮助我...
问候.
I need to calculate the CRC of some telegrams in VB2008 but not sure if it is X or Zmodem. And honestly I cant understand the algorithm because i''ve seen some with lookup tables, other with polinomes, shifts...
These examples of telegrams i''m using and the data used in the CRC calculation is between 0x02 and 0x04 (02 and 04 excluded).
STX ADDR DATA EOT CRCH CRCL ETX
02 38 51 4D 4E 30 30 23 04 F1 D7 03
02 38 51 4D 4E 31 30 04 EC CC 03
02 38 51 4D 4E 32 30 52 04 27 D7 03
I wonder if there aren''t any flowcharts explaining how to calculate the CRC, or a DLL i can use, or even the code in VB.
If anyone can help me with this...
Regards.
推荐答案
在这里,您是VB
函数(非常类似于C
的代码:它是人造"例程,请参见[ ^ ]).
如果效率很重要,请考虑移植基于表的表.
Here you are theVB
function (veryC
-like code: it is a direct porting of the ''ufficial''C
routine, see [^]).
If efficiency matters, consider porting a table-based one.
Function calcrc(ByVal data() As Byte, ByVal count As Integer) As Integer
Dim crc As Integer = 0
Dim i, j As Integer
Dim d As Integer
For i = 0 To count - 1
d = data(i)
crc = crc Xor (d << 8)
For j = 0 To 7
If ((crc And &H8000) <> 0) Then
crc = (crc << 1) Xor &H1021
Else
crc = (crc << 1)
End If
Next
Next
calcrc = crc And &HFFFF
End Function
以下代码是对您的第一批数据的测试:
The following code is a test with your first chunk of data:
Sub Main()
'Please note data(6) declare 7 items (OK, that's VB madness)
Dim data(6) As Byte
Dim crc As Integer
data(0) = &H38
data(1) = &H51
data(2) = &H4D
data(3) = &H4E
data(4) = &H30
data(5) = &H30
data(6) = &H23
crc = calcrc(data, 7)
End Sub
:)
:)
这篇关于CRC XModem/ZModem解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!