在做自動化工程項目時常常需要用上位機對PLC進行監控,如果是大的工程項目可以使用組態軟件來完成,但對中小項目為了節約成本,可以采用自己寫上位機程序來完成對PLC的監控。
海為提供了一個海為PLC的通訊控件,通過這個控件實現上位機與海為PLC之間的通訊十分方便,它封裝了Modbus通訊協議和HaiwellBus協議,以點的形式實現對海為PLC的數據訪問,對每個點可以定義它的名稱/數據類型/小數長度/點注釋等,不必去了解Modbus通訊協議和HaiwellBus協議的具體內容,只要有上位機編程經驗就可以完成。
海為PLC的通訊控件和例子源程序可以到海為網站(http://www.www.kj366333.com/download.asp)的“下載中心”下載。
下面以VB為例介紹該控件的使用,新建一個VB工程,添加2個窗口:
窗口1的程序如下:
Option Explicit
'啟動跑馬燈按鈕
Private Sub Command1_Click()
'啟動跑馬燈,M0置1,點序號30
HWPLCComm1.HWPLCs(1).SetPointValue 30, 1
End Sub
'停止跑馬燈按鈕
Private Sub Command2_Click()
'停止跑馬燈,M1置1,點序號31
HWPLCComm1.HWPLCs(1).SetPointValue 31, 1
End Sub
'啟動采樣按鈕
Private Sub Command3_Click()
' HWPLCComm1是海為PLC通訊控件名稱,Timer1是畫面刷新定時器
HWPLCComm1.Run Not HWPLCComm1.RunFlag
Timer1.Enabled = HWPLCComm1.RunFlag
If HWPLCComm1.RunFlag Then
Command3.Caption = "停止采樣"
Shape2.FillColor = vbGreen ‘采樣指示燈綠色
Else
Command3.Caption = "啟動采樣"
Shape2.FillColor = vbWhite ‘采樣指示燈紅色
End If
End Sub
'窗口1裝載
Private Sub Form_Load()
Dim i As Long
'添加1臺PLC,PLC地址為1,名稱為“1號PLC”
HWPLCComm1.HWPLCs.AddPLC 1, "1號PLC"
'在“1號PLC”下增加采樣點
For i = 0 To 15
HWPLCComm1.HWPLCs(1).AddPoint "y" & i '增加采樣點Y0-Y15
Next i
'在“1號PLC”下增加其他離散的采樣點
HWPLCComm1.HWPLCs(1).AddPoint "sv0", , "當前掃描時間 單位0.1ms"
HWPLCComm1.HWPLCs(1).AddPoint "sv1", , "最小掃描時間 單位0.1ms"
HWPLCComm1.HWPLCs(1).AddPoint "sv2", , "最大掃描時間 單位0.1ms"
HWPLCComm1.HWPLCs(1).AddPoint "sv12", "Year", "年"
HWPLCComm1.HWPLCs(1).AddPoint "sv13", "Month", "月"
HWPLCComm1.HWPLCs(1).AddPoint "sv14", "Day", "日"
HWPLCComm1.HWPLCs(1).AddPoint "sv15", "Hour", "時"
HWPLCComm1.HWPLCs(1).AddPoint "sv16", "Minute", "分"
HWPLCComm1.HWPLCs(1).AddPoint "sv17", "Second", "秒"
HWPLCComm1.HWPLCs(1).AddPoint "sv18", "Week", "星期"
HWPLCComm1.HWPLCs(1).AddPoint "sm3", , "10ms時鐘脈沖"
HWPLCComm1.HWPLCs(1).AddPoint "sm4", , "100ms時鐘脈沖"
HWPLCComm1.HWPLCs(1).AddPoint "sm5", , "1s時鐘脈沖"
HWPLCComm1.HWPLCs(1).AddPoint "m0", "Start", "跑馬燈啟動"
HWPLCComm1.HWPLCs(1).AddPoint "m1", "Stop", "跑馬燈停止"
HWPLCComm1.HWPLCs(1).AddPoint "v0"
HWPLCComm1.HWPLCs(1).AddPoint "v2"
HWPLCComm1.HWPLCs(1).AddPoint "v100", , "實數例子", [REAL ] '實數類型為5
HWPLCComm1.HWPLCs(1).AddPoint "ccv50", , "32位計數器"
HWPLCComm1.HWPLCs(1).AddPoint "ccv100", , "16位計數器"
'將點加到Listview中顯示, Y0-Y15 16個點不加入,用指示燈來顯示狀態
Dim Newitem As ListItem
For i = 17 To HWPLCComm1.HWPLCs(1).PointCount
Set Newitem = ListView1.ListItems.Add(, , i)
Newitem.SubItems(1) = HWPLCComm1.HWPLCs(1).iPoints(i).PointAddress
Newitem.SubItems(2) = HWPLCComm1.HWPLCs(1).iPoints(i).PointName
Newitem.SubItems(4) = HWPLCComm1.HWPLCs(1).iPoints(i).PointNote
Next i
'將PLC地址和名稱用標簽顯示
Label3(0) = HWPLCComm1.HWPLCs(1).PLCAddress
Label3(1) = HWPLCComm1.HWPLCs(1).PLCName
'將16只指示燈的標簽以點名稱顯示(Y0~Y15)
For i = 1 To 16
Label1(i - 1) = HWPLCComm1.HWPLCs(1).iPoints(i).PointName
Next i
End Sub
'雙擊列表中的點打開窗口2,對該點的值進行修改
Private Sub ListView1_DblClick()
Dim i As Long
If Not ListView1.SelectedItem Is Nothing Then
i = ListView1.SelectedItem.Index + 16
Form2.vPointIndex = i
Form2.Caption = "寫入值 " & HWPLCComm1.HWPLCs(1).iPoints(i).PointName
Form2.Label1.Caption = "寫入值 " & HWPLCComm1.HWPLCs(1).iPoints(i).PointName & ":"
Form2.Text1 = HWPLCComm1.HWPLCs(1).iPoints(i).PointValue
Form2.Show 1
End If
End Sub
' Timer1是畫面刷新定時器
Private Sub Timer1_Timer()
'刷新值
Dim i As Long
'刷新Y0-Y15值
For i = 1 To 16
If HWPLCComm1.HWPLCs(1).iPoints(i).PointValue = 1 Then
Shape1(i - 1).FillColor = vbRed '值=1,為真
Else
Shape1(i - 1).FillColor = vbWhite '值=0,為假
End If
Next i
'刷新其他點值
For i = 17 To HWPLCComm1.HWPLCs(1).PointCount
ListView1.ListItems(i - 16).SubItems(3) = HWPLCComm1.HWPLCs(1).iPoints(i).PointValue
Next i
End Sub
窗口2的程序如下:
Option Explicit
Public vPointIndex As Long '公共變量,存儲被雙擊點的序號
'確定按鈕
Private Sub Command1_Click()
If Text1.Text = "" Then Exit Sub
'按確定,則寫入點的值
Form1.HWPLCComm1.HWPLCs(1).SetPointValue vPointIndex, Text1.Text
Unload Me
End Sub
'取消按鈕
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Command1_Click
End If
End Sub
運行結果如下圖:
海為公眾號
海為云APP
廈門海為科技有限公司
0592-3278716 (技術) | 4000-360-362 (商務)
海為總部:廈門市集美區軟件園三期C03棟13-14層 | 制造中心:廈門市翔安區火炬高新區(翔安)產業區垵邊南路336-2號4樓