***********************************
Private Sub Timer1_Timer() '发送数据
Dim longCRC As Long '定义校验变量
Dim aa(7) As Byte '定义动态数组
aa(0) = &H1 '地址
aa(1) = &H3 '功能码 读
aa(2) = &H0 '起始地址
aa(3) = &HA
aa(4) = &H0 '数据点数
aa(5) = &H8
longCRC = CRC16(aa) 'CRC校验
aa(UBound(aa) - 1) = longCRC Mod 256
aa(UBound(aa)) = longCRC \ 256
MSComm1.OutBufferco u* nt = 0 '清空输出寄存器
MSComm1.Output = aa '发送
End Sub
***********************************
2,CRC校验程序
***********************************
Function CRC16(Coun() As Byte) As Long
Dim intBit, intTemp As Integer
Dim lonCRC As Long
lonCRC = &HFFFF&
For intCnt = 0 To UBound(Coun) - 2
lonCRC = lonCRC Xor Coun(intCnt)
For intBit = 0 To 7
intTemp = lonCRC Mod 2
lonCRC = lonCRC \ 2
If intTemp = 1 Then
lonCRC = lonCRC Xor &HA001&
End If
Next intBit
Next intCnt
CRC16 = lonCRC
End Function
***********************************