Next i
L = Len(Lenth2) '读取字节数
If L = 2 Then
OutByte(12) = Asc(Mid(Lenth2, 1, 1))
OutByte(13) = Asc(Mid(Lenth2, 2, 1))
Else
OutByte(12) = Asc(0)
OutByte(13) = Asc(Mid(Lenth2, 1, 1))
End If
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 Command2_Click()
Dim OutByte(0 To 32) As Byte
Dim Num As Double
Dim Num2 As String
Dim L
Dim Lenth2 As String
Dim ByteXor As Byte
Dim StrXor As String
Dim Data_Send As Double
Dim Data_Send2 As String
Dim SetLenth2 As String
Read = False
If Text2.Text = "" Then
MsgBox "请输入寄存器地址", vbOKOnly, "系统提示"
Else
Num = Val(Text2.Text)
Num2 = Hex(Num)
SetLenth2 = Hex(SetLenth * 2)
Data_Send = Val(Text4.Text)
Data_Send2 = Hex(Data_Send)
ByteXor = 0
OutByte(0) = 103 '起始字符
OutByte(1) = 6 '指令写
OutByte(2) = Asc(0) 'PLC站地址
OutByte(3) = Asc(2)
OutByte(4) = Asc(Mid(Address_W, 1, 1)) '寄存器类型
OutByte(5) = Asc(Mid(Address_W, 2, 1))
OutByte(6) = Asc(Mid(Address_W, 3, 1))
OutByte(7) = Asc(Mid(Address_W, 4, 1))
L = Len(Num2) '寄存器地址
For i = 0 To L - 1
OutByte(11 - i) = Asc(Mid(Num2, L - i, 1))
Next i
For i = 0 To 3 - L
OutByte(11 - L - i) = Asc(0)
Next i
L = Len(SetLenth2) '写入数据的长度

雅虎收藏
