找传奇、传世资源到传世资源站!

kepOPC 通讯示例源码(c#)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.Linq;using System.Text;using OPCAutomation;namespace kepserverHelp{ class kepOpcHelp { private static OPCServer kepServer =null; //服务器 private static OPCGroups kepGroups = null; //组 private static OPCGroup KepGroup = null; //组 private static OPCItems KepItems = null; public static myOpcItem[] item; private static String serIp; //服务器IP private static String serverName; //服务器名字 public static bool OpcErr = false;//错误标志 /// <summary> /// 连接服务器,创建组,添加变量 /// </summary> public static string kepOpcConnect() { serIp = "localhost";//服务器在本机 serverName = "KEPware.KEPServerEx.V4"; try { //step1打开服务器 kepServer = new OPCServer(); kepServer.Connect(serverName, serIp); //step2添加组 kepGroups = kepServer.OPCGroups; KepGroup = kepGroups.Add("Group0"); KepGroup.UpdateRate = 100; //更新循环时间 KepGroup.IsActive = true; KepGroup.IsSubscribed = true; //step3添加组变量组 KepItems = KepGroup.OPCItems; //初始化自定义标签变量数组 init_itemName(); //在变量组中初始化标签变量数组 addItem(); return "OK"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// 初始化OPC标签的连接字符串 /// </summary> private static void init_itemName() { item =new myOpcItem[12]; for (int i = 0; i < item.Length; i ) { item[i] = new myOpcItem(); item[i].ClientHandles = i; } item[0].ConnectName = "Channel1.Device1.T100"; item[1].ConnectName = "Channel1.Device1.T101"; item[2].ConnectName = "Channel1.Device1.T102"; item[3].ConnectName = "Channel1.Device1.T103"; item[4].ConnectName = "Channel1.Device1.T104"; item[5].ConnectName = "Channel1.Device1.T105"; item[6].ConnectName = "Channel1.Device1.T106"; item[7].ConnectName = "Channel1.Device1.T107"; item[8].ConnectName = "Channel1.Device1.T108"; item[9].ConnectName = "Channel1.Device1.T109"; item[10].ConnectName = "Channel1.Device1.T110"; item[11].ConnectName = "Channel1.Device1.T111"; } /// <summary> /// 在服务器组中添加变量 /// </summary> /// <param name="theNum"></param> public static void addItem() { if (OpcErr) { throw new Exception("添加OPC标签异常,OpcErr==true"); } try { for (int i = 0; i < item.Length; i )//在OPC组中增加变量标签 { item[i].MyOpcItem = KepItems.AddItem(item[i].ConnectName, item[i].ClientHandles); item[i].ServerHandle = item[i].MyOpcItem.ServerHandle; } } catch (Exception ex) { OpcErr = true; throw new Exception("添加OPC标签异常," ex.Message); } } /// <summary> /// 销毁资源,引用赋值NULL /// </summary> public static void destory() { try { foreach (myOpcItem myitem in item) { if (myitem.MyOpcItem != null) { myitem.MyOpcItem = null; } } if (KepItems!= null) { KepItems = null; } if (KepGroup != null) { KepGroup = null; } if (kepGroups != null) { kepGroups = null; } if (kepServer != null) { kepServer.Disconnect(); kepServer = null; } } catch (Exception) { return; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复