Next i
'位地址
OutByte(12) = Asc(Mid(Bit, 1, 1))
OutByte(13) = Asc(Mid(Bit, 2, 1))
For i = 1 To 29 'BCC校验码计算
ByteXor = ByteXor Xor OutByte(i)
Next i
StrXor = Hex(ByteXor)
If Len(StrXor) = 2 Then
OutByte(30) = Asc(Mid(StrXor, 1, 1))
OutByte(31) = Asc(Mid(StrXor, 2, 1))
Else
OutByte(30) = Asc(0)
OutByte(31) = Asc(Mid(StrXor, 1, 1))
End If
OutByte(32) = 71
End If
MSComm1.Output = OutByte
End Sub
Private Sub Form_Load()
Instruction = &H11
Address_R = "0100"
Address_W = "0100"
Address_B = "0100"
Bit = "00"
Lenth = 2
SetLenth = 2
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
If (Err) Then
MsgBox "端口打开错误", vbOKOnly, "系统信息"
End If
MSComm1.RThreshold = 20
MSComm1.InputMode = 1
MSComm1.InBufferCount = 0
End Sub
Private Sub MSComm1_OnComm()
Dim Temp() As Byte
Dim Read(0 To 32) As Byte
Dim BBC As Byte
Dim XorByte As Byte
Dim BBC_Temp As Byte
Dim Value As Double
Dim Trans(1 To 8) As Byte
Dim TransValue As Double
Value = 0
XorByte = 0
Temp = MSComm1.Input
If ReadPlc = True Then
If Temp(LBound(Temp)) = 103 And Temp(LBound(Temp) + 1) = 1 Then '如果起始正确
For i = 0 To 20
Read(i) = Temp(i)
Next i
For i = 2 To 17 '计算校验码
XorByte = XorByte Xor Read(i)
Next i
For i = 2 To 19
If Read(i) > &H40 Then
Read(i) = Read(i) - &H37
Else
Read(i) = Read(i) - &H30
End If
Next
BBC_Temp = Read(19) + Read(18) * &H10
If XorByte = BBC_Temp Then

雅虎收藏
