C#获取PLC信息 (KepServer)
首先注册DLL
我这里把此DLL放到我自己的网站上了,供大家下载
https://blog.nwctwang.top/OPCDAAuto.dll
把此dll复制到C:\Windows\SysWOW64目录下
以管理员身份运行CMD
cd C:\Windows\SysWOW64
regsvr32 OPCDAAuto.dll
然后就可以在VS中使用了
由于手头没有现成的OPC服务器和PLC设备,故采用KepServer模拟
安装方法就不赘述了,安装完打开是这样
启动完服务器,先到项目里引入刚才的DLL然后再撸代码,直接上成品
其中的treeView控件、textBox控件需要你自己定义。
其余要注意的点已用注释写出
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; public Form1() { try { InitializeComponent(); OPCServerName = "Kepware.KEPServerEX.V5"; ObjOPCServer = new OPCServer(); ObjOPCServer.Connect(OPCServerName, "127.0.0.1");// 启动一个Connect会顶掉其他线程的Connect,注意 object serverList = ObjOPCServer.GetOPCServers(); OPCBrowser opcBrowser = ObjOPCServer.CreateBrowser(); TreeNode Root = treeView1.Nodes.Add(ObjOPCServer.ServerName); Root.Tag = ObjOPCServer; opcBrowser.MoveToRoot(); ShowInTreeView(opcBrowser, Root); ObjOPCGroups = ObjOPCServer.OPCGroups; ObjOPCGroup = ObjOPCGroups.Add("Data Type Examples.16 Bit Device.R Registers");// 组名对应的是左边树状第一大项里的小项 ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange); // 第一个参数是设备Tag名(加上group),第二个参数是item ID,是自己定义的,用于ObjOPCGroup_DataChange获取相应ID的item数据 ObjOPCGroup.OPCItems.AddItem("Data Type Examples.16 Bit Device.R Registers.Double1", 1);// Item对应的是组名里的Tag,一定要写全称,否则报错 ObjOPCGroup.OPCItems.AddItem("Data Type Examples.16 Bit Device.R Registers.Float1", 2); // ObjOPCGroup.UpdateRate = 100; // 刷新速率 ObjOPCGroup.IsActive = true; ObjOPCGroup.IsSubscribed = true; } catch (Exception e) { MessageBox.Show(e.ToString()); } } // 当数据发生变化时,采取的动作 private void ObjOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { for (int i = 1; i <= NumItems; i++) { if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1))// ID为1的设备数据 { textBox1.Text = ItemValues.GetValue(i).ToString(); } if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 2))// ID为2的设备数据 { textBox2.Text = ItemValues.GetValue(i).ToString(); } } } // 树状展示设备节点 private void ShowInTreeView(OPCBrowser brower, TreeNode node) { TreeNode childNode = null; brower.ShowBranches(); int BranchCount = brower.Count; List<string> lst = null; if (brower.Count > 0) //回溯的添加branch { lst = new List<string>(); foreach (string t in brower) lst.Add(t); foreach (string branch in lst) { childNode = new TreeNode(branch); childNode.Tag = brower.GetItemID(branch); node.Nodes.Add(childNode); brower.MoveDown(branch); ShowInTreeView(brower, childNode); brower.MoveUp(); } } brower.ShowLeafs(); int LeafsCount = brower.Count; foreach (string leaf in brower) { childNode = new TreeNode(brower.GetItemID(leaf)); childNode.Tag = brower.GetItemID(leaf); node.Nodes.Add(childNode); } } // 退出时释放资源 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ObjOPCServer.Disconnect(); } } }