{转码词1},{转码词2},{转码词3},{转码词4},{转码词5}

廈門海為科技有限公司

請上傳logo 請上傳logo

國產(chǎn)PLC—Haiwell(海為)PLC與VB通訊源程序講解

2008-05-07 11:09:14 haiwell 852

在做自動(dòng)化工程項(xiàng)目時(shí)常常需要用上位機(jī)對PLC進(jìn)行監(jiān)控,如果是大的工程項(xiàng)目可以使用組態(tài)軟件來完成,但對中小項(xiàng)目為了節(jié)約成本,可以采用自己寫上位機(jī)程序來完成對PLC的監(jiān)控。


海為提供了一個(gè)海為PLC的通訊控件,通過這個(gè)控件實(shí)現(xiàn)上位機(jī)與海為PLC之間的通訊十分方便,它封裝了Modbus通訊協(xié)議和HaiwellBus協(xié)議,以點(diǎn)的形式實(shí)現(xiàn)對海為PLC的數(shù)據(jù)訪問,對每個(gè)點(diǎn)可以定義它的名稱/數(shù)據(jù)類型/小數(shù)長度/點(diǎn)注釋等,不必去了解Modbus通訊協(xié)議和HaiwellBus協(xié)議的具體內(nèi)容,只要有上位機(jī)編程經(jīng)驗(yàn)就可以完成。

海為PLC的通訊控件和例子源程序可以到海為網(wǎng)站(http://www.jxcm2.com/download.asp)的“下載中心”下載。

 

下面以VB為例介紹該控件的使用,新建一個(gè)VB工程,添加2個(gè)窗口:

lQLPJxaJeo9MmU3NATLNAjCwLOMkAFDexwEC4k-9_sAnAA_560_306.png


lQLPJxaJeo9MmT_M2M0BdLAEmitl4IqTugLiT74KgNIA_372_216.png


窗口1的程序如下:

Option Explicit

'啟動(dòng)跑馬燈按鈕

Private Sub Command1_Click()

    '啟動(dòng)跑馬燈,M0置1,點(diǎn)序號(hào)30

    HWPLCComm1.HWPLCs(1).SetPointValue 30, 1

End Sub

'停止跑馬燈按鈕

Private Sub Command2_Click()

    '停止跑馬燈,M1置1,點(diǎn)序號(hào)31

    HWPLCComm1.HWPLCs(1).SetPointValue 31, 1

End Sub

'啟動(dòng)采樣按鈕

Private Sub Command3_Click()

' HWPLCComm1是海為PLC通訊控件名稱,Timer1是畫面刷新定時(shí)器

    HWPLCComm1.Run Not HWPLCComm1.RunFlag

    Timer1.Enabled = HWPLCComm1.RunFlag

    If HWPLCComm1.RunFlag Then

        Command3.Caption = "停止采樣"

        Shape2.FillColor = vbGreen ‘采樣指示燈綠色

    Else

        Command3.Caption = "啟動(dòng)采樣"

        Shape2.FillColor = vbWhite ‘采樣指示燈紅色

    End If

End Sub

'窗口1裝載

Private Sub Form_Load()

    Dim i As Long

    '添加1臺(tái)PLC,PLC地址為1,名稱為“1號(hào)PLC”

    HWPLCComm1.HWPLCs.AddPLC 1, "1號(hào)PLC"

    '在“1號(hào)PLC”下增加采樣點(diǎn)

    For i = 0 To 15

        HWPLCComm1.HWPLCs(1).AddPoint "y" & i '增加采樣點(diǎn)Y0-Y15

Next i

'在“1號(hào)PLC”下增加其他離散的采樣點(diǎn)

    HWPLCComm1.HWPLCs(1).AddPoint "sv0", , "當(dāng)前掃描時(shí)間 單位0.1ms"

    HWPLCComm1.HWPLCs(1).AddPoint "sv1", , "最小掃描時(shí)間 單位0.1ms"

    HWPLCComm1.HWPLCs(1).AddPoint "sv2", , "最大掃描時(shí)間 單位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", "時(shí)"

    HWPLCComm1.HWPLCs(1).AddPoint "sv16", "Minute", "分"

    HWPLCComm1.HWPLCs(1).AddPoint "sv17", "Second", "秒"

    HWPLCComm1.HWPLCs(1).AddPoint "sv18", "Week", "星期"

    HWPLCComm1.HWPLCs(1).AddPoint "sm3", , "10ms時(shí)鐘脈沖"

    HWPLCComm1.HWPLCs(1).AddPoint "sm4", , "100ms時(shí)鐘脈沖"

    HWPLCComm1.HWPLCs(1).AddPoint "sm5", , "1s時(shí)鐘脈沖"

    HWPLCComm1.HWPLCs(1).AddPoint "m0", "Start", "跑馬燈啟動(dòng)"

    HWPLCComm1.HWPLCs(1).AddPoint "m1", "Stop", "跑馬燈停止"

    HWPLCComm1.HWPLCs(1).AddPoint "v0"

    HWPLCComm1.HWPLCs(1).AddPoint "v2"

    HWPLCComm1.HWPLCs(1).AddPoint "v100", , "實(shí)數(shù)例子", [REAL ] '實(shí)數(shù)類型為5

    HWPLCComm1.HWPLCs(1).AddPoint "ccv50", , "32位計(jì)數(shù)器"

    HWPLCComm1.HWPLCs(1).AddPoint "ccv100", , "16位計(jì)數(shù)器"

    '將點(diǎn)加到Listview中顯示, Y0-Y15 16個(gè)點(diǎn)不加入,用指示燈來顯示狀態(tài)

    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地址和名稱用標(biāo)簽顯示

    Label3(0) = HWPLCComm1.HWPLCs(1).PLCAddress

    Label3(1) = HWPLCComm1.HWPLCs(1).PLCName

'將16只指示燈的標(biāo)簽以點(diǎn)名稱顯示(Y0~Y15)

    For i = 1 To 16

        Label1(i - 1) = HWPLCComm1.HWPLCs(1).iPoints(i).PointName

    Next i

End Sub

'雙擊列表中的點(diǎn)打開窗口2,對該點(diǎn)的值進(jìn)行修改

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是畫面刷新定時(shí)器

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

    '刷新其他點(diǎn)值

    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 '公共變量,存儲(chǔ)被雙擊點(diǎn)的序號(hào)

'確定按鈕

Private Sub Command1_Click()

    If Text1.Text = "" Then Exit Sub

'按確定,則寫入點(diǎn)的值

    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


運(yùn)行結(jié)果如下圖:


lQLPJxaJeo9Ml9DNAUrNAjCwcnyozf1ciXgC4k-92ICEAA_560_330.png

微信技術(shù)客服:08:30-21:00
0592-3278716
關(guān)注我們

海為公眾號(hào)

海為云APP