Private Sub Command4_Click()
'单通道发送区代码
On Error GoTo PortError
If Shape1.FillColor = RGB(0, 255, 0) Then
MsgBox "PLC在运行状态不能修改"
Else
Timer1.Enabled = False
Dim Inputstr As String
Inputstr = Text2.Text '输入通道地址及修改值
Data = Inputstr
outdata = "@00WD" + Data
L = Len(outdata)
fcs = 0
For i = 1 To L
fcs1 = fcs1 Xor Asc(Mid(outdata, i, 1))
Next i
fcs = Hex(fcs1)
Dim Instring As String
frmMain.MSComm1.CommPort = 2
frmMain.MSComm1.Settings = "9600,E,7,2"
frmMain.MSComm1.InputLen = 0
frmMain.MSComm1.PortOpen = True
frmMain.MSComm1.Output = outdata + fcs + "*" + Chr$(13)
Do
DoEvents
Loop Until frmMain.MSComm1.InBufferCount >= 10
Instring = frmMain.MSComm1.Input
frmMain.MSComm1.PortOpen = False
If Mid(Instring, Len(Instring) - 1, 1) <> "*" Then
MsgBox "通讯出错"
Else
fcs2 = 0
For i = 1 To Len(Instring) - 4
fcs2 = fcs2 Xor Asc(Mid(Instring, i, 1))
Next i
fcs3 = Hex(fcs2)
If (fcs3) = Mid(Instring, Len(Instring) - 3, 2) Then
MsgBox "·发送成功"
Else
MsgBox "通讯出错"
End If
End If
Timer1.Enabled = True
End If
PortError: '错误处理
Select Case Err.Number
Case 8005
MsgBox ("该串口正在使用,请等待!!")
End Select
End Sub