C#获取PLC信息 (KepServer)二
具体应用呢,不多说了,上代码,取长补短就是原创
using OPCAutomation; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace XSDLOPCClinet { public partial class Form1 : Form { OPCServer ObjOPCServer; OPCGroups ObjOPCGroups; OPCGroup ObjOPCGroup; string OPCServerName; private OPCItem[] OPC_ITEMS_ADDED; Dictionary<string, string> MAP_CLIENTHANDLE_TAG = new Dictionary<string, string>(); public Form1() { try { InitializeComponent(); InitServer();// 启动服务 } catch (Exception e) { MessageBox.Show("cuowu1"+e.Message); } } private void InitServer() { OPCServerName = "Kepware.KEPServerEX.V5"; ObjOPCServer = new OPCServer(); ObjOPCServer.Connect(OPCServerName, "127.0.0.1"); if (ObjOPCServer.ServerState == (int)OPCServerState.OPCRunning) { label1.Text = $"OPC服务器名:{ObjOPCServer.ServerName}"; label4.Text = $"连接启动时间:{ObjOPCServer.StartTime.ToString()}"; label3.Text = "服务运行中,请勿关闭"; //创建并设置组 bool bSucce = this.InitGetDataConfig(); //group添加items AddGroupItems(); //采集opc数据 GetOPCData(); } else { label3.Text = "OPC服务器状态异常: " + ObjOPCServer.ServerState.ToString(); } } private bool InitGetDataConfig() { ObjOPCGroups = ObjOPCServer.OPCGroups; ObjOPCGroup = ObjOPCGroups.Add();// 初始化Groups组 ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(GroupAsyncReadComplete); ObjOPCGroup.IsActive = true; ObjOPCGroup.IsSubscribed = true; ObjOPCServer.OPCGroups.DefaultGroupDeadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组 return true; } private void GetOPCData() { try { //异步读opc数据 int[] temp = new int[OPC_ITEMS_ADDED.Length + 1]; temp[0] = 0; for (int i = 1; i <= OPC_ITEMS_ADDED.Length; i++) { temp[i] = OPC_ITEMS_ADDED[i - 1].ServerHandle; } Array serverHandles = (Array)temp; Array Errors; int cancelID; Random rd = new Random(); int TransactionID = rd.Next(1, 100); ObjOPCGroup.AsyncRead(serverHandles.Length - 1, ref serverHandles, out Errors, TransactionID, out cancelID);//第一参数为item数量 } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void AddGroupItems() { List<string> l_str = new List<string>(); //读取配置文件,获取需要的传感器ID l_str.Add("10A.PLC"); l_str.Add("10B.PLC"); List<OPCItem> ItemsAdded = new List<OPCItem>(); int n = 0; foreach (string tag in l_str) { ItemsAdded.Add(ObjOPCGroup.OPCItems.AddItem($"{tag}.address12138", n)); //clientHandle tag关系 MAP_CLIENTHANDLE_TAG.Add(n + "", tag); n++; } OPC_ITEMS_ADDED = ItemsAdded.ToArray(); } // 异步获取t,v信息 private void GroupAsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { try { //C# Dictionary 字典 Dictionary<string, string> tagValueMap = new Dictionary<string, string>(); for (int i = 1; i <= NumItems; i++) { string clientHandle = ClientHandles.GetValue(i).ToString(); string tag = MAP_CLIENTHANDLE_TAG[clientHandle]; string val = ItemValues.GetValue(i).ToString(); //C# Dictionary 字典 添加数据 tagValueMap.Add(tag, val); } // 在这里可以调用SQL语句进行value值的更新,是异步的,不是同步更新 // textBox1.Text = tagValueMap["10A.PLC"]; } catch (Exception ex) { label3.Text = ex.Message; } } // 退出时释放资源 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ObjOPCServer.Disconnect(); } } }
多设备多地址的设置
private void AddGroupItems() { List<string> l_str = new List<string>(); l_str.Add("10A.PLC"); l_str.Add("10B.PLC"); List<OPCItem> ItemsAdded = new List<OPCItem>(); int n = 0; foreach (string tag in l_str) { string address1 = $"{tag}.address12138"; string address2 = $"{tag}.address12139"; string address3 = $"{tag}.address12140"; ItemsAdded.Add(ObjOPCGroup.OPCItems.AddItem(address1, n)); ItemsAdded.Add(ObjOPCGroup.OPCItems.AddItem(address2, n +1)); ItemsAdded.Add(ObjOPCGroup.OPCItems.AddItem(address3, n + 2)); //clientHandle tag关系 MAP_CLIENTHANDLE_TAG.Add(n + "", address1); MAP_CLIENTHANDLE_TAG.Add(n + 1 + "", address2); MAP_CLIENTHANDLE_TAG.Add(n + 2 + "", address3); n = n + 3; } OPC_ITEMS_ADDED = ItemsAdded.ToArray(); }