《国产PLC-VB与海为PLC通讯源程序讲解.doc》由会员分享,可在线阅读,更多相关《国产PLC-VB与海为PLC通讯源程序讲解.doc(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、VB与海为PLC通讯源程序讲解作者:厦门海为科技有限公司在做自动化工程项目时常常需要用上位机对PLC进行监控,如果是大的工程项目可以使用组态软件来完成,但对中小项目为了节约成本,可以采用自己写上位机程序来完成对PLC的监控。海为提供了一个海为PLC的通讯控件,通过这个控件实现上位机与海为PLC之间的通讯十分方便,它封装了Modbus通讯协议和HaiwellBus协议,以点的形式实现对海为PLC的数据访问,对每个点可以定义它的名称/数据类型/小数长度/点注释等,不必去了解Modbus通讯协议和HaiwellBus协议的具体内容,只要有上位机编程经验就可以完成。海为PLC的通讯控件和例子源程序可以
2、到海为网站(下面以VB为例介绍该控件的使用,新建一个VB工程,添加2个窗口如下图:窗口1的程序如下:Option Explicit启动跑马灯按钮Private Sub Command1_Click()启动跑马灯,M0置1,点序号30HWPLCComm1.HWPLCs(1).SetPointValue 30, 1EndSub停止跑马灯按钮Private Sub Command2_Click()停止跑马灯,M1置1,点序号31HWPLCComm1.HWPLCs(1).SetPointValue 31, 1EndSub启动采样按钮Private Sub Command3_Click()HWPLCCo
3、mm1是海为PLC通讯控件名称,Timer1是画面刷新定时器HWPLCComm1.Run Not HWPLCComm1.RunFlagTimer1.Enabled = HWPLCComm1.RunFlagIf HWPLCComm1.RunFlag ThenCommand3.Caption = 停止采样Shape2.FillColor = vbGreen 采样指示灯绿色ElseCommand3.Caption = 启动采样Shape2.FillColor = vbWhite 采样指示灯红色End IfEndSub窗口1装载Private Sub Form_Load()Dim i As Long添
4、加1台PLC,PLC地址为1,名称为“1号PLC”HWPLCComm1.HWPLCs.AddPLC 1, 1号PLC在“1号PLC”下增加采样点For i = 0 To 15HWPLCComm1.HWPLCs(1).AddPoint y & i 增加采样点Y0-Y15Nexti在“1号PLC”下增加其他离散的采样点HWPLCComm1.HWPLCs(1).AddPoint sv0, , 当前扫描时间 单位0.1msHWPLCComm1.HWPLCs(1).AddPoint sv1, , 最小扫描时间 单位0.1msHWPLCComm1.HWPLCs(1).AddPoint sv2, , 最大扫
5、描时间 单位0.1msHWPLCComm1.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).AddPoi
6、nt 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 v0HWPLCComm1.HWPLCs(1).AddPo
7、int v2HWPLCComm1.HWPLCs(1).AddPoint v100, , 实数例子, REAL 实数类型为5HWPLCComm1.HWPLCs(1).AddPoint ccv50, , 32位计数器HWPLCComm1.HWPLCs(1).AddPoint ccv100, , 16位计数器将点加到Listview中显示, Y0-Y1516个点不加入,用指示灯来显示状态Dim Newitem As ListItemFor i = 17 ToHWPLCComm1.HWPLCs(1).PointCountSet Newitem = ListView1.ListItems.Add(, ,
8、i)Newitem.SubItems(1) =HWPLCComm1.HWPLCs(1).iPoints(i).PointAddressNewitem.SubItems(2) =HWPLCComm1.HWPLCs(1).iPoints(i).PointNameNewitem.SubItems(4) =HWPLCComm1.HWPLCs(1).iPoints(i).PointNoteNext i将PLC地址和名称用标签显示Label3(0) = HWPLCComm1.HWPLCs(1).PLCAddressLabel3(1) = HWPLCComm1.HWPLCs(1).PLCName将16只指示
9、灯的标签以点名称显示(Y0Y15)For i = 1 To 16Label1(i - 1) =HWPLCComm1.HWPLCs(1).iPoints(i).PointNameNext iEndSub双击列表中的点打开窗口2,对该点的值进行修改Private Sub ListView1_DblClick()Dim i As LongIf Not ListView1.SelectedItem Is NothingTheni = ListView1.SelectedItem.Index + 16Form2.vPointIndex = iForm2.Caption = 写入值 &HWPLCComm1
10、.HWPLCs(1).iPoints(i).PointNameForm2.Label1.Caption = 写入值 &HWPLCComm1.HWPLCs(1).iPoints(i).PointName & :Form2.Text1 =HWPLCComm1.HWPLCs(1).iPoints(i).PointValueForm2.Show 1End IfEndSubTimer1是画面刷新定时器Private Sub Timer1_Timer()刷新值Dim i As Long刷新Y0-Y15值For i = 1 To 16If HWPLCComm1.HWPLCs(1).iPoints(i).Po
11、intValue = 1ThenShape1(i - 1).FillColor = vbRed 值=1,为真ElseShape1(i - 1).FillColor = vbWhite 值=0,为假End IfNext i刷新其他点值For i = 17 ToHWPLCComm1.HWPLCs(1).PointCountListView1.ListItems(i - 16).SubItems(3) =HWPLCComm1.HWPLCs(1).iPoints(i).PointValueNext iEndSub窗口2的程序如下:Option ExplicitPublic vPointIndex As
12、 Long 公共变量,存储被双击点的序号确定按钮Private Sub Command1_Click()If Text1.Text = Then Exit Sub按确定,则写入点的值Form1.HWPLCComm1.HWPLCs(1).SetPointValue vPointIndex,Text1.TextUnload MeEndSub取消按钮Private Sub Command2_Click()Unload MeEndSubPrivate Sub Text1_GotFocus()Text1.SelStart = 0Text1.SelLength = Len(Text1)EndSubPrivate Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 ThenCommand1_ClickEnd IfEndSub运行结果如下图: