Implementation of Serial Communication Between Monitoring Computer and Delta PLC Based on Visual Basic 中達電通上海機電PLC產品處 王乃全 Wang Naiquan 摘 要:PLCzhijieyuxianchangkongzhicengdejianceyiqiyibiaoshebeilianjie,shidicengkongzhiwangluodezuchengbufen,shigongchangzidonghuaxitongdejishi,yincishixianyujiankongcengjisuanjidetongxinduixitongdeyouhuayunxingyouzhongyaoyiyi。wenzhangjiyuVB討論台達DVP PLC與監控計算機的通信問題。 關鍵詞:VB 通信協議 可編程控製器 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 [中圖分類號] TP273 [文獻標識碼] B 文章編號 1 引 言 現場設備層中的設備種類繁多,有傳感器、啟動器、驅動器、I/O部件、變送器、閥門等等,當然也包括現場檢測儀器儀表設備。PLC是現場設備控製層不可缺少的部分,已經非常普及,如何簡便地實現與PLC的交互已經成為眾多廠商新的競爭戰場。由此產生了人機界麵等軟件產品,這些產品的出現簡化了對PLC的控製、操作,使PLC的應用更加方便;但也有其共同的缺點,價格普遍偏高和可二開發性較差。VB作為“原始”的編程語言在這兩方麵無疑有著明顯優勢的,本文結合一個簡單的案例,用比較基礎的概念討論如何通過VB實現PC與PLCzhijiandetongxinwenti,zheduigangshebukongzhilingyudegongchengshihuoxushiyoubangzhude。zaixianchangshebeicengzhonggeshigeyangdeshebeidoukeyihejisuanjilianji,erzuijiandandezidonghualianjifangshijiushishiyongchuanxingtongxin,VB提供了串行通信控件可以讓開發者開發出串行通信的係統程序,以下是對相關問題的粗略討論。 2 串行通信基本概念 數據通訊中有兩種類型的通信形式,即並行通信(Parallel Communication)和串行通信(Serial Communication)。例如,如果有8位數據需要傳送,那麼並行通信一次就可以完成8位的傳輸量;而串行一次隻能傳送1位。圖1表示了兩種不同的通信模式,常用的串行通信也有兩種,分別為RS-232和RS-485。
用按鈕控製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數據寫入的操作類似,限於篇幅其它代碼就不再列出了。 5 結束語 VB語言易於編程使用,為與串行設備的通信提供了很大的方便。台達PLC采用標準的目前廣為流行的MODBUS協議,為實現PLC與監控計算機的通信提供了簡化的平台。由於台達所有的機電產品都支持MODBUS協議,所以,掌握了VB與PLC通信過程,也就等於掌握了PC與台達所有機電產品的通信。 參考文獻[略] 作者簡介 王乃全(1978-),男,自動化設備工程師,主要從事PLC控製係統的開發與技術支持。