本系统以PC对基于PLC的交通灯控制系统的监控为例,在上位机与PLC正常通信的前提下,设计如下界面。窗体的内容主要包括,四大区:端口、站号选择区,设定时间输入区,监控显示区,控制按纽区。从站号选择区选择不同站号即不同PLC,实现对多个PLC的实时监控。
3.1 串口初始化程序
If MSComm1.PortOpen <> True Then
MSComm1.PortOpen=True
End If
MSComm1.Settings=“9600,E,7,2”
MSComm1.InputLen=0
MSComm1.InBufferCount=0
MSComm1.InputMode=comInputModeText
MSComm1.Handshaking=comNone
3.2 计算机与PLC通信的VB程序
Public Function ReadData(ByVal InputStr As String, ByVal Num1 As Integer, ByVal Num2 As Integer) As String
Dim OutputStr As String
Dim InString As String
Dim ReturnStr As String
Dim EndString As String
Dim FCSString As String
Dim ReturnFCSString As String
Form1.MSComm1.InBufferCount=0
OutputStr=InputStr+FCS(InputStr)+“*”
If Form1.MSComm1.PortOpen=True Then
Form1.MSComm1.Output=OutputStr+Chr$(13)’按照PLC帧格式输出
End If
Do
DoEvents
Loop Until Form1.MSComm1.InBufferCount >=Num2’返回帧的长度
InString=Form1.MSComm1.Input
EndString=Mid$(InString,6,2)’取返回帧的响应码
If EndString <> “00”Then
ReadData=“Error”
Exit Function
End If
EndString=Mid$(InString,1,Len(InString)-4)
ReturnFCSString=Mid$(InString, Len(InString) -3,2)’取返回帧校验码
FCSString=FCS(EndString)
If FCSString <> ReturnFCSString Then
ReadData=“Error”
Exit Function
End If
If Len(InString)>=Num2 Then
ReturnStr=Mid$(InString, Len(InString)-Num1-3, Num1)’取返回帧的数据,其度为Num1
ReadData = ReturnStr
End If
End Function