赞
踩
C#将OPCUA数据接口封装成类源码、智能控制算法源码、WinCC项目等完整项目源码:
https://download.csdn.net/download/weixin_37928884/53250225
主函数调用,其余类函数由于过多,这里不在给出,前往上述网址进行下载。
//方向:一级二级交叉应用 //功能:控制台、OpcUa读写操作 //说明:step7 v5.4本身不支持OpcUa功能、需要Scout.V10搭建OpcUa平台、连接至WinCC.V5.5 using System; using System.Collections; using System.Collections.Generic; using Opc.Ua.UaHelper; using Opc.Ua.Client; using Opc.Ua; using System.Threading.Tasks; using System.Timers; using System.Threading; namespace YangZheng_OpcUa { public struct Postn { public double Set_T; //设定值 public double Now_T; //当前值 public double Lst_T; //前一刻的值 }; public struct Flows //流量结构体 { public double Air; public double Gas; }; public struct Error//误差结构体 { public double E;//误差 public double Lst_E;//前一刻的误差 public double EC;//误差的变化量 }; public struct Times { public int Run_Stag; public double Air_Gas; public double All_Time, Now_Time, Scn_Time; public double Air_incr; public double Gas_incr; public double Rst_Time; }; /*------------------------------------------------------------------------------------------------------------------------*/ class Program { static void Main(string[] args) { //WINCC端口号:4862 KEPWARE端口号:49321 OpcUaClient client = new OpcUaClient(); client.Connect("opc.tcp://127.0.0.1:4862"); //client.Connect("opc.tcp://127.0.0.1:49320"); /*-----------初始化结构体参数--------*/ Postn Head,Tail; Error E_head, E_tail; Times T_para; T_para.Run_Stag = 1; T_para.Air_Gas = 0.50; T_para.All_Time = 3600.0; T_para.Now_Time = 0.0; T_para.Scn_Time = 3.0; T_para.Rst_Time = 0.0; T_para.Air_incr = 0.0; T_para.Gas_incr = 0.0; Head.Now_T = 0; Head.Lst_T = 0; Head.Set_T = 1300.00; Tail.Now_T = 0; Tail.Lst_T = 0; Tail.Set_T = 380.00; E_head.Lst_E = 0; E_head.E = 0; E_head.EC = 0; E_tail.Lst_E = 0; E_tail.E = 0; E_tail.EC = 0; FuzzyClass fuzzy = new FuzzyClass(); fuzzy.Fuzzy_init(); while (true) { Console.WriteLine(""); Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>多读<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); var retValues = client.ReadNodes(new List<string> { "ns=1;s=t|xx", "ns=1;s=t|oo" });//{"value1", "value2" , "value3" , "value4" , .... }//连接 WinCC //var retValues = client.ReadNodes(new List<string> { "ns=2;s=King.Win7.xx", "ns=2;s=King.Win7.oo" });//{"value1", "value2" , "value3" , "value4" , .... }//连接 Kepware //retValues.ForEach(x => Console.WriteLine(x)); Tail.Now_T = Convert.ToDouble(retValues[0]);// Head.Now_T = Convert.ToDouble(retValues[1]);// Console.WriteLine("______________________烟温当前值_______________________"); Console.WriteLine("—{0}—", Tail.Now_T); Console.WriteLine("______________________顶温当前值_______________________"); Console.WriteLine("—{0}—", Head.Now_T); double ext_v = (Tail.Set_T - Tail.Now_T) / (T_para.All_Time - T_para.Now_Time); Console.WriteLine("_________________烟温上升速率理想值____________________"); Console.WriteLine("—{0}—", ext_v); Console.WriteLine("_________________烟温上升速率实际值____________________"); double now_v = (Tail.Now_T - Tail.Lst_T) / T_para.Scn_Time; Console.WriteLine("—{0}—", now_v); E_tail.E = now_v - ext_v; E_tail.EC = E_tail.E - E_tail.Lst_E; E_head.E = Head.Now_T - Head.Set_T; E_head.EC = E_head.E - E_head.Lst_E; Console.WriteLine("____________________顶温误差值_________________________"); Console.WriteLine("—{0}—", E_head.E); Console.WriteLine("__________________顶温误差变化值_______________________"); Console.WriteLine("—{0}—", E_head.EC); Console.WriteLine("____________________烟温误差值_________________________"); Console.WriteLine("—{0}—", E_tail.E); Console.WriteLine("__________________烟温误差变化值_______________________"); Console.WriteLine("—{0}—", E_tail.EC); if (T_para.Run_Stag == 1 && T_para.Now_Time > 0) { T_para.Run_Stag = 2; } if (T_para.Run_Stag == 2 && Head.Now_T >= 1270) { T_para.Run_Stag = 3; } if (T_para.Run_Stag == 3 && (Head.Now_T >= 1290 && Head.Now_T < 1310)) { T_para.Run_Stag = 4; } if (T_para.Run_Stag == 4 && Tail.Now_T >= 370) { T_para.Run_Stag = 5; } if (Head.Now_T < 1270 && Tail.Now_T < 370) { T_para.Run_Stag = 1; } Console.WriteLine("_____________________工作模式号________________________"); Console.WriteLine("—{0}—", T_para.Run_Stag); switch (T_para.Run_Stag) { case 1://点火阶段 //固定风燃比例 T_para.Air_incr = 0; T_para.Gas_incr = T_para.Air_incr / T_para.Air_Gas; Console.WriteLine("______________________点火阶段_________________________"); Console.WriteLine("—{0}—", T_para.Air_incr); break; case 2://快速升温阶段 //固定风燃比例 T_para.Air_incr = 80; T_para.Gas_incr = T_para.Air_incr / T_para.Air_Gas; Console.WriteLine("______________________升温阶段_________________________"); Console.WriteLine("—{0}—", T_para.Air_incr); break; case 3://顶温恒定阶段 空燃比寻优 T_para.Air_incr = fuzzy.gdwd_fuzzy_control(E_head.E, E_head.EC); T_para.Scn_Time = 5; T_para.Gas_incr = T_para.Air_incr / T_para.Air_Gas; Console.WriteLine("________________顶温恒定阶段 空燃比寻优________________"); Console.WriteLine("—{0}—", T_para.Air_incr); break; case 4://烟温增长阶段 空燃比寻优 T_para.Air_incr = fuzzy.yqwd_fuzzy_control(E_tail.E, E_tail.EC); T_para.Scn_Time = 7; T_para.Gas_incr = T_para.Air_incr / T_para.Air_Gas; Console.WriteLine("________________烟温增长阶段 空燃比寻优________________"); Console.WriteLine("—{0}—", T_para.Air_incr); break; case 5://燃烧结束 T_para.Air_incr = 0; T_para.Now_Time = 0; T_para.Gas_incr = T_para.Air_incr / T_para.Air_Gas; Console.WriteLine("______________________燃烧结束_________________________"); Console.WriteLine("—{0}—", T_para.Air_incr); break; } T_para.Now_Time = T_para.Now_Time + T_para.Scn_Time; T_para.Rst_Time = T_para.All_Time - T_para.Now_Time; Head.Lst_T = Head.Now_T; Tail.Lst_T = Tail.Now_T; E_tail.Lst_E = E_tail.E; E_head.Lst_E = E_head.E; /*------------------------------------------------------------------------------------------------------------------------*/ String A = Convert.ToString(T_para.Air_incr);// String B = Convert.ToString(T_para.Gas_incr);// Console.WriteLine(""); Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>多写<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); client.WriteNodes(new List<string> { "ns=1;s=t|xx_out", "ns=1;s=t|oo_out" }, new List<string> { A, B });//{"value1", "value2" , "value3" , "value4" , .... }, new List<string> { A, B , C, D,...}//连接 WinCC //client.WriteNodes(new List<string> { "ns=2;s=King.Win7.xx_out", "ns=2;s=King.Win7.oo_out" }, new List<string> { E, F });//{"value1", "value2" , "value3" , "value4" , .... }, new List<string> { A, B , C, D,...}//连接 Kepware Console.WriteLine("____________________空气开度输出_______________________"); Console.WriteLine("—{0}—", A); Console.WriteLine("____________________煤气开度输出_______________________"); Console.WriteLine("—{0}—", B); Thread.Sleep(Convert.ToInt32(T_para.Scn_Time)*1000); } } } }
有一个简单的方法;想要获取C#(OPCUA)连接WinCC的地址(假设不知道以下四个地址ns=1;s=t|xx、ns=1;s=t|oo、ns=1;s=t|xx_out、ns=1;s=t|oo_out)。
可以通过Kepware以OPCUA通信协议连接WinCC,具体步骤如下:
注意:没有出现的截图均默认下一步,如果提示证书信任选择信任即可…
端口号更改为WinCC的端口号4862、安全策略和消息模式选择无。
观察地址:ns=1;s=t|xx、ns=1;s=t|oo、ns=1;s=t|xx_out、ns=1;s=t|oo_out,将地址输入C#中。
有一个简单的方法;想要获取C#(OPCUA)连接Kepware的地址(假设不知道以下四个地址ns=2;s=King.Win7.xx、ns=2;s=King.Win7.oo、ns=2;s=King.Win7.xx_out、ns=2;s=King.Win7.oo_out)。
可以通过WinCC以OPCUA通信协议连接Kepware,具体步骤如下:
注意:WinCC运行状态下…
选择连接参数。
双击添加服务器:输入127.0.0.1:49320;49320是Kepware的端口号。
选择浏览OPC服务器。
观察地址:ns=2;s=King.Win7.xx、ns=2;s=King.Win7.oo、ns=2;s=King.Win7.xx_out、ns=2;s=King.Win7.oo_out,将地址输入C#中。
学者浏览该篇文章时,如果遇到问题,建议先看以下两篇文章https://blog.csdn.net/weixin_37928884/article/details/128089765
https://blog.csdn.net/weixin_37928884/article/details/128087209
举一反三,C#通过OPCUA方式连接其它带有OPCUA功能的软件都可以用这种方法确认地址。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。