大家好,欢迎来到IT知识分享网。
在通过OPCAutomation我们先了解下这个类下的三个对象
1.OPCServer:创建OPC的连接
2.OPCBrowser:创建OPC浏览对象
3.OPCGroups:OPC通信组对象
4.OPCItem:单个代操作(读取的对象)
可以看下面这张结构图,方便理解几个对象之间的关系
具体的读取流程如下:
1.获取当前程序下的所有IP地址/计算机名,将得到的对象存入控件
IPHostEntry IPHost = Dns.GetHostEntry(Environment.MachineName); if (IPHost.AddressList.Length>0) { for (int i = 0; i < IPHost.AddressList.Length; i++) { string host = Dns.GetHostEntry(IPHost.AddressList[i].ToString()).HostName; if (!this.cmb_ServerNode.Items.Contains(host)) { this.cmb_ServerNode.Items.Add(host); } } }
2.根据选择的IP地址/计算机名得到OPC服务名称列表,并加载进控件
KepSever = new OPCServer(); object serverList = KepSever.GetOPCServers(this.cmb_ServerNode.Text); foreach (var item in (Array) serverList) { if (!this.cmb_ServerName.Items.Contains(item) ) { this.cmb_ServerName.Items.Add(item); } }
3.连接OPC服务器,其中KepBrowser记录的就是服务器内所有的标签变量,接下来就是创建组并激活,开启订阅功能,并且绑定读的操作(也可以绑定值改变的事件,根据自己需要进行选择)
//建立连接 if (KepSever == null) { KepSever = new OPCServer(); } try { KepSever.Connect(this.cmb_ServerName.Text, this.cmb_ServerNode.Text); } catch (Exception ex) { MessageBox.Show("连接失败:" + ex.Message); return; } this.btn_Connect.Text = "disConn"; //获取OPC所有的变量 KepBrowser = KepSever.CreateBrowser(); KepBrowser.ShowBranches(); KepBrowser.ShowLeafs(true); this.lst_Items.Items.Clear(); foreach (var item in KepBrowser) { this.lst_Items.Items.Add(item); //将获取到的变量标签存入控件 } //初始化OPCGroup KepGroups = KepSever.OPCGroups; KepGroups.DefaultGroupDeadband = 0; //死区值,设为0时,服务器端该组内任何数据变化都通知组 KepGroups.DefaultGroupIsActive = true; KepGroup = KepGroups.Add("测试通讯组"); KepGroup.IsActive = true; //激活组 KepGroup.IsSubscribed = true; //开启订阅功能 KepGroup.UpdateRate = 250; //增加一个事件 KepGroup.AsyncReadComplete +=KepGroup_AsyncReadComplete;
4.添加需要读取的标签到列表中
this.OPCList.Add(new OPCItem() { ItemID = this.lst_Items.SelectedItem.ToString() }); int count = this.OPCList.Count; List<string> ItemIDs = new List<string>(); List<int> ClientHandles = new List<int>(); ItemIDs.Add("0"); ClientHandles.Add(0); for (int i = 0; i < count; i++) { ItemIDs.Add(this.OPCList[i].ItemID); ClientHandles.Add(i); } //集合转换成array Array ItemID = ItemIDs.ToArray(); Array ClientHandle = ClientHandles.ToArray(); KepGroup.OPCItems.AddItems(count, ref ItemID, ref ClientHandle, out SeverHandles, out Errors);
5.开启定时器,定时读取
private void timer1_Tick(object sender, EventArgs e) { if (KepSever != null) { if (this.OPCList.Count>0) { KepGroup.AsyncRead(this.OPCList.Count, ref SeverHandles, out Errors, TransactionID, out CancelID); } } }
6.读取数据并解析
private void KepGroup_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors) { //解析数据 for (int i = 1; i <= NumItems; i++) { object value = ItemValues.GetValue(i); if (value != null) { int ClientHandle=Convert.ToInt32(ClientHandles.GetValue(i)); for (int j = 0; j < this.OPCList.Count; j++) { if (j == ClientHandle) { this.OPCList[j].Value = value.ToString(); this.OPCList[j].UpdateTime = TimeStamps.GetValue(i).ToString(); this.OPCList[j].Quanlity = Qualities.GetValue(i).ToString(); } } } } this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = this.OPCList; }
6.1.如果用的是值改变事件,而非读取完成事件,则是以下内容
private void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { for (int i = 1; i <= NumItems; i++) { object value = ItemValues.GetValue(i); if (value != null) { int ClientHandle = Convert.ToInt32(ClientHandles.GetValue(i)); for (int j = 0; j < this.OPCList.Count; j++) { if (j == ClientHandle) { this.OPCList[j].Value = value.ToString(); this.OPCList[j].UpdateTime = TimeStamps.GetValue(i).ToString(); this.OPCList[j].Quanlity = Qualities.GetValue(i).ToString(); } } this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = this.OPCList; } } }
参考文档:https://blog.csdn.net/guorui_java/article/details/104247734
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32456.html