当前位置:文档之家› VB编写的Modbus RTU协议通讯源程序

VB编写的Modbus RTU协议通讯源程序

modbus rtu协议可以算是一种事实上的工业标准协议,为许多仪表、PLC等所支持。以前有几个用户问如何使用VB编程来与我们的KND-K3系列PLC通讯,于是整了一个demo 程序。这次把这个demo共享,希望能给大家一点帮助。

1)模块文件:modCRC,其中包含了CRC校验的函数。

'data 待校验的数组名称

'no 数组中元素个数

'btLoCRC 算出的CRC高字节

'btHiCRC 算出的CRC低字节

Public Function CalCRC16Fast(data() As Byte, no As Integer, btLoCRC As Byte, btHiCR C As Byte) As String

Dim CL As Byte, CH As Byte '多项式码&HA001

Dim SaveHi As Byte, SaveLo As Byte

Dim i As Integer

Dim Flag As Integer

btHiCRC = &HFF

btLoCRC = &HFF

CL = &H1

CH = &HA0

For i = 0 To (no - 1)

btHiCRC = btHiCRC Xor data(i) '每一个数据与CRC寄存器进行异或

For Flag = 0 To 7

SaveHi = btLoCRC

SaveLo = btHiCRC

btLoCRC = btLoCRC \ 2 '高位右移一位

btHiCRC = btHiCRC \ 2 '低位右移一位

If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1

btHiCRC = btHiCRC Or &H80 '则低位字节右移后前面补1

End If '否则自动补0

If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或

btLoCRC = btLoCRC Xor CH

btHiCRC = btHiCRC Xor CL

End If

Next Flag

Next i

Dim ReturnData(1) As Byte

ReturnData(0) = btHiCRC 'CRC高位

ReturnData(1) = btLoCRC 'CRC低位

CalCRC16Fast = ReturnData

End Function

Public Function CalCRC16Tbl(data() As Byte, no As Integer, btLoCRC As Byte, btHiCRC As Byte) As String

Dim btLoCRC As Byte

Dim btHiCRC As Byte

btLoCRC = &HFF

btHiCRC = &HFF

Dim i As Integer

Dim iIndex As Long

For i = 0 To (no - 1)

iIndex = btHiCRC Xor data(i)

btHiCRC = btLoCRC Xor GetCRCLo(iIndex) '低位处理

btLoCRC = GetCRCHi(iIndex) '高位处理

Next i

Dim ReturnData(1) As Byte

ReturnData(0) = btHiCRC 'CRC高位

ReturnData(1) = btLoCRC 'CRC低位

CalCRC16Tbl = ReturnData

End Function

'CRC低位字节值表

Function GetCRCLo(Ind As Long) As Byte

GetCRCLo = Choose(Ind + 1, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _

&H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, _

&H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, _

&H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, _

&H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, _

&H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, _

&H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, _

&H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, _

&H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _

&H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _

&H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _

&H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, _

&H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, _

&H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &H C1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H8 1, &H40)

End Function

'CRC高位字节值表

Function GetCRCHi(Ind As Long) As Byte

GetCRCHi = Choose(Ind + 1, _

&H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7 , &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, & HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, & HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3, &H11, &HD1, &HD 0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H 37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &H

相关主题