发表于:2007-10-31 10:50:00
楼主
基于MODBUS协议的串行通讯例程(VB)
'基于MODBUS协议的和平软起动器(http://www.hapn.com)串行通讯例程
'可通过MODBUS协议获取:主电压、电流、运行状态、DIP开关设置、硬接线状态、统计数据等;
'并可通过MODBUS协议控制:启动、急停、软停、双重参数调节、慢速正/反转、节能等;
'本例程为通过通讯获取软启动器当前运行状态
'编制:董斌 dbboss@21cn.com
Private Const Read_Coil_Status = &H1
Private Const Read_Input_Status = &H2
Private Const Read_Holding_Registers = &H3
Private Const Read_Input_Registers = &H4
Private Const Force_Single_Coil = &H5
Private Const Single_Registers = &H6
Private Const Diagnostics = &H8
Private Const Force_Multiple_Coil = &HF
Private Const Force_Multiple_Register = &H10
Public Function CRC16(data() As Byte) As tCRC
Dim CRC16Hi As Byte
Dim CRC16Lo As Byte
Dim Result As tCRC
CRC16Hi = &HFF
CRC16Lo = &HFF
Dim i As Integer
Dim iIndex As Long
For i = 0 To UBound(data)
iIndex = CRC16Lo Xor data(i)
CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex) '低位处理
CRC16Hi = GetCRCHi(iIndex) '高位处理
Next i
With Result
.bytLow = CRC16Lo 'CRC低位
.bytHigh = CRC16Hi 'CRC高位
End With
CRC16 = Result
End Function
'CRC低位字节值表
Private 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,