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數據寫入的操作類似,限于篇幅其它代碼就不再列出了。