工控网首页
>

应用设计

>

VB 编程实现控制系统的时间同步

VB 编程实现控制系统的时间同步

2007/9/3 14:04:00
0引言 时间同步在控制系统中非常重要,在不同的现场控制器如果时间不同,上传数据的时间有错位,在历史记录、操作记录、分析事故发生的顺序等等与时间相关的工作就会发生问题, 一般情况下,PLC系统的绝对时间是在操作站上(人机界面)形成的,控制器中只有相对时间,当同时有多个操作站或控制站时绝对时间或相对时间不可能保持一致,有时甚至相差几个小时,而且操作站的时间更容易被人为修改,在时间要求精度不是太高的场合,通常的做法是以一个控制站的CPU时间为基准,其他控制站和操作站与之实现时间同步,在时间精度要求很高的场合,例如锅炉和汽机的保护系统,SOE卡件等,就要求时间精确到毫秒级,这时就可以把卫星时间GPS(GLOBAL POSITION SYSTEM)作为基准,来校对PLC或DCS的时间,GPS是目前世界上传播范围最广、精度最高的时间发布系统,以GPS时间为基准可以实现跨越整个控制系统与国际标准时间UTC保持高度一致,最高精度可达10~1ns。时间同步概念大致分为三种,一是PLC与计算机同步,二是计算机之间同步,三是计算机与GPS的同步,下面重点介绍以PLC时间为基准实现操作站的同步方法。 1 VB编程实现控制系统的时间同步 PLC控制系统多种多样,仅以AB的Logix5000系列为例说明: 1.1提取PLC时间 l 建立类型为DINT的时间数组标签DATE[0~6] 如图1
图1 l 编写读取或写入时间标签的梯形逻辑 如图2
图2 l 设定PLC当前时间 如图3
图3 1.2设定OPC服务器,以便OPC客户端读取或写入时间标签 如图四
图四 1.3 使用VB6.0开发OPC客户端和同步程序 打开VB6.0建立如图五所示的窗口
图五 其中Label1显示当前PLC时间, Label2显示当前计算机时间,TEXTBOX(0)~ TEXTBOX(5)用于输入OPC服务器中对应的PLC时间标签,时间同步按钮用于建立OPC连接以及执行同步命令,标签设置按钮用于存储输入的时间标签名称到文本文件,避免下次运行时重复输入.退出按钮用于释放OPC连接以及占用的内存空间。 初始化窗口程序 Private Sub Form_Load() //窗口初始化操作 Dim i As Integer Dim stringtmp As String If App.PrevInstance = True Then End //’避免程序多次执行 hMenu = GetSystemMenu(Me.hwnd, 0) Call DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND) Call DeleteMenu(hMenu, 5, MF_BYPOSITION) //调用API函数,删除窗口关闭最小化最大化按钮.. On Error Resume Next Open "a.txt" For Input As #1 //打开文本文件A.TXT用于存储输入的时间标签 For i = 0 To 5 Input #1, stringtmp OPCItemName(i).Text = stringtmp //调用现存的标签作为默认OPC服务器对应的时间标签 Next i Close #1 SetWindowPos Me.hwnd, -1, Me.CurrentX, Me.CurrentY, 0, 0, SWP_NOMOVE Or SWP_NOSIZE // ’使该窗口总在应用程序的最前面 End Sub 建立OPC的连接 先在工具栏中“工程\引用”将” “OPC Automation 2.0“加入,然后定义全局变量。在本程序中,使用了一个OPC组进行OPC访问。首先定义OPC服务类型与计算机结点名。定义OPC组与OPC标签组。并定义OPC的标签数组与值数,注意,值数组一定要设为Variant。 Option Explicit Option Base 1 //这数组必须由1开始,不能由0开始 Const ServerName = "RSLinx OPC Server" //OPC服务器名称 Dim NodeName As String //结点名,即计算机名,如是访问本机可以省略 Dim WithEvents ConnectedOPCServer As OPCServer //OPCServer对象 Dim WithEvents ConnectedGroup As OPCGroup //OPCGroup对象 Dim ConnectedServerGroup As OPCGroups //OPC OPCGrous对象,添加OPC组 Dim OPCItemCollection As OPCItems //OPC标签集合 Dim OneOPCItem As OPCItem //OPC标签 Dim ClientHandles(10) As Long //客户端OPCItem句柄 Dim ServerHandles() As Long //服务器端OPCItem句柄 Dim OPCItemIDs(7) As String //记录OPC的标签 Dim ItemIDsValue(7) As Variant //存放OPC的值 Dim ItemCount As Long //要读取标签的个数 Dim ItemServerHandles() As Long Dim ItemServerErrors() As Long Dim ClientHandles(7) As Long //标签索引 Dim Sumdate //定义用于比较时间的变量 在定义所有变量后,就要进行OPC连接了,要进行OPC连接之前,先要配置要访问的OPC标签名 Set ConnectedOPCServer = New OPCServer //生成OPC对象 ConnectedOPCServer.Connect OPCServername, OPCNodeName //连接OPC服务器 Set ConnectedServerGroup = ConnectedOPCServer.OPCGroups //生成组对象 ConnectedServerGroup.DefaultGroupIsActive = 1 //设置组为活动对象 ConnectedServerGroup.DefaultGroupDeadband = 0 Set ConnectedGroup = ConnectedServerGroup.Add("Group1") //添加组对象 ConnectedGroup.UpdateRate = 500 //设定数据刷新率为500毫秒 ConnectedGroup.IsSubscribed = True ItemCount = 6 Dim i As Integer Open "a.txt" For Input As #1 //读取存储的OPC时间标签 For i = 0 To 5 Input #1, stringtmp OPCItemName(i).Text = stringtmp OPCItemIDs(i + 1) = OPCItemName(i).Text ClientHandles(i + 1) = i Next i Close #1 Set OPCItemCollection = ConnectedGroup.OPCItems //设置OPC条目连接对象 OPCItemCollection.DefaultIsActive = True //激活对象 OPCItemCollection.AddItems ItemCount,OPCItemIDs,ClientHandles,ItemServerHandle _ItemServerErrors //初始化OCP连接 //如果连接成功,程序就转如后台运行 If ItemServerErrors(1) = 0 And ItemServerErrors(2) = 0 And ItemServerErrors(3) = 0 And ItemServerErrors(4) = 0 And ItemServerErrors(5) = 0 And ItemServerErrors(6) = 0 Then MsgBox "OPC服务器连接正常", vbOKOnly, "OPC 连接正常" Me.hide End If OPC的标签数据读出可以通过OPCItemIDs 的DataChange事件来读取。该事件有多个参数:其中NumItems是指标签改变值的个数,ClientHandles是改变值的标签索引,ItemValues为改变值的数据,只有数据发生变化时才会触发该事件。 Sub ConnectedGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date) OldTime = Now Dim tmp1 Label2.Caption = Now //窗口中显示当前时间 sumdate=ConnectedGroup.OPCItems.Item(1).Value&ConnectedGroup.OPCItems.I_tem(2).Value&ConnectedGroup.OPCItems.Item(3).Value&ConnectedGrou_p.OPCItems.Item(4).Value&Format(ConnectedGroup.OPCItems.Item(5).V_alue, "00") //读取PLC中的时间标签值 Label1.Caption=(ConnectedGroup.OPCItems.Item(1).Value)&"-"&ConnectedGroup.OPCItems.Item(2).Value)&"-"&(ConnectedGroup.OPCItems.Item(3).Value)&""&(ConnectedGroup.OPCItems.Item(4).Value)&":"&Format((ConnectedGroup.OPCItems.Item(5).Value),"00")&":"&Format((ConnectedGroup.OPCItems.Item(6).Value), "00") //窗口中显示计算机系统时间 tmp1 =Year(OldTime) & Month(OldTime) & Day(OldTime) & Hour(OldTime) &Format(Minute(OldTime), "00") //格式化计算机系统时间 If tmp1 <> Sumdate Then //比较PLC与计算机时间是否相同 精确到秒 Call SetToOldTime //调用对时过程 End If: End Sub 执行时间同步程序,精确到毫秒。 Public Function SetToOldTime() As String Dim lpSystemTime As SYSTEMTIME lpSystemTime.wYear = ConnectedGroup.OPCItems.Item(1).Value //对年份 lpSystemTime.wMonth=ConnectedGroup.OPCItems.Item(2).Value //对月份 lpSystemTime.wDay = ConnectedGroup.OPCItems.Item(3).Value /对日 lpSystemTime.wHour=ConnectedGroup.OPCItems.Item(4).Value//’对小时 lpSystemTime.wMinute=ConnectedGroup.OPCItems.Item(5).Valu对分钟 lpSystemTime.wSecond=ConnectedGroup.OPCItems.Item(6).Value //对秒 lpSystemTime.wMilliseconds=ConnectedGroup.OPCItems.Item(7).Value //’对毫秒 tmp2 = tmp2 + 1 timecontrol.Caption = "时间同步次数为" & tmp2 //记录同步时间次数 SetLocalTime lpSystemTime //调用API函数进行系统对时 End Function OPC连接断开   OPC客户端连接后要占用服务器资源,所以如果不需要使用OPC时,必须进行OPC连接断开,断开的程序相当简单,释放资源即可。如下: If Not ConnectedOPCServer Is Nothing Then ConnectedOPCServer.Disconnect //与服务器断开连接并且清除 Set ConnectedOPCServer = Nothing End If 如图六所示的运行效果图 图六 1.4
投诉建议

提交

查看更多评论
其他资讯

查看更多

基于FTE结构的PKS系统软件安装与配置

基于FTE结构的PKS系统软件安装与配置

无模型自适应控制方案在特种氧化铝厂的应用