Abstract: The PLC is a composing part of Infranet, it links to the measuring instrument and device in field control layer directly, and is the footstone of factory automation system. Therefore it is very important to realize the communication between PLC and monitoring layer computer for optimization of syste running. The paper discussed the related communication problems between Delta DVP PLC and monitoring computer based on Visual Basic. Key Words: Visual Basic Communication protocal Programable logic controller
用按钮控制PLC的起动停止,Y0、Y1的ON/OFF及D256、D512写完数据的发送;用Shape组件做指示灯,表示PLC的运行状态和Y的状态;用timer组件不停的读取M1072的状态,以判断PLC的运行情况;用MScomm控件实现PC与PLC的通信。 4)编程实现的代码构成 (1) LRC算法校验的实现 Public Function LRC(str As String) As String c = 0 l = Len(str) For c = c + 1 To l c_data = Mid$(str, c, 2) d_lrc = d_lrc + Val("&H" + c_data) c = c + 1 Next c If d_lrc > &HFF Then d_lrc = d_lrc Mod &H100 End If h_lrc = Hex(&HFF - d_lrc + 1) If Len(h_lrc) > 2 Then h_lrc = Mid(h_lrc, Len(h_lrc) - 1, 2) End If LRC = h_lrc End Function (2) 运行的开始就判断PLC的状态并设置标志位 '初次运行打开串口,并显示PLC运行状态 Private Sub Form_Load() Dim s1 As String Dim s2 As String Dim s22 As String Dim s3 As String Dim s4 As String MSComm1.PortOpen = True s2 = "01010C300001" s22 = LRC(s2) s1 = ":" + s2 + s22 + Chr$(13) + Chr$(10) MSComm1.Output = s1 s3 = MSComm1.Input s4 = Mid$(s, 6, 8) If s4 = "0C30FF00" Then plc = 1 'PLC为运行标志 Else plc = 0 'PLC为停止标志 End If End Sub (3) 下面一段为用指示灯表示PLC的运行状态 Private Sub Timer5_Timer() Dim s1 As String Dim s2 As String Dim s22 Dim s3 As String Dim s4 As String s2 = "01010C300001" s22 = LRC(s2) s1 = ":" + s2 + s22 + Chr$(13) + Chr$(10) MSComm1.Output = s1 s3 = MSComm1.Input s4 = Mid$(s3, 8, 2) If s4 = "31" Then plc = 1 'PLC为运行标志 Else: If s4 = "30" Then plc = 0 'PLC为停止标志 End If If plc = 1 Then Label2.Caption = "PLC正在运行......" Shape1.FillColor = RGB(0, 255, 0) 'green Else Label2.Caption = "PLC已经停止" Shape1.FillColor = RGB(255, 0, 0) 'red End If End Sub (4) PLC的起动与停止 '起动PLC Private Sub start_Click() Dim strout As String Timer5.Enabled = False str = "00050C30FF00" 'M1072 为PLC起动停止标志位。查地址表,M1072为OC30.FF00为置 ON,0000为置OFF。 '以上都是固定格式,要牢记。 LRCC = LRC(str) '计算 str的lrc校验码。 strout = ":" + str + LRCC + Chr$(13) + Chr$(10) '欲传送之数据。13为D,10为A MSComm1.Output = strout Timer5.Enabled = True End Sub '停止PLC Private Sub stop_Click() Dim strout As String Timer5.Enabled = False str = "00050C300000" LRCC = LRC(str) strout = ":" + str + LRCC + Chr$(13) + Chr$(10) MSComm1.Output = strout Timer5.Enabled = True End Sub Y0、Y1的ON/OFF与PLC起动/停止的控制方式相同,指示灯的表示方式也相同。D256,D512数据写入的操作类似,限于篇幅其它代码就不再列出了。