厦门海为科技有限公司

请上传logo 请上传logo

国产PLC—Haiwell(海为)PLC与VB通讯源程序讲解

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

在做自动化工程项目时常常需要用上位机对PLC进行监控,如果是大的工程项目可以使用组态软件来完成,但对中小项目为了节约成本,可以采用自己写上位机程序来完成对PLC的监控。


海为提供了一个海为PLC的通讯控件,通过这个控件实现上位机与海为PLC之间的通讯十分方便,它封装了Modbus通讯协议和HaiwellBus协议,以点的形式实现对海为PLC的数据访问,对每个点可以定义它的名称/数据类型/小数长度/点注释等,不必去了解Modbus通讯协议和HaiwellBus协议的具体内容,只要有上位机编程经验就可以完成。

海为PLC的通讯控件和例子源程序可以到海为网站(http://www.haiwell.com/download.asp)的“下载中心”下载。

 

下面以VB为例介绍该控件的使用,新建一个VB工程,添加2个窗口:

lQLPJxaJeo9MmU3NATLNAjCwLOMkAFDexwEC4k-9_sAnAA_560_306.png


lQLPJxaJeo9MmT_M2M0BdLAEmitl4IqTugLiT74KgNIA_372_216.png


窗口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


运行结果如下图:


lQLPJxaJeo9Ml9DNAUrNAjCwcnyozf1ciXgC4k-92ICEAA_560_330.png

微信技术客服:08:30-21:00
0592-3278716
关注我们

海为公众号

海为云APP