using Newtonsoft.Json; using Opc.Ua; using OpcUaHelper; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Kede; using Newtonsoft.Json.Linq; namespace TestAgreement.utils { internal class OpcUaUtils { public static ResponseBody OpcCaiji(string url,string userName,string password,List list) { Console.WriteLine("===============科德机床OPCUa数据采集start================"); ResponseBody responseBody = new ResponseBody(); responseBody.code = 1; try { /*string url = "opc.tcp://192.168.11.63:12686"; string userName = ""; string password = "";*/ OpcUaClient opcUaClient = new OpcUaClient(); // 创建连接 if (!string.IsNullOrEmpty(url) && string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) { opcUaClient.ConnectServer(url); } else if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { opcUaClient.UserIdentity = new UserIdentity(userName, password); opcUaClient.ConnectServer(url); } // 批量获取值 List nodeIdList = new List(); list.ForEach(vo => { nodeIdList.Add(vo); }); //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//主轴负载 //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//进给倍率 List values = opcUaClient.ReadNodes(nodeIdList.ToArray()); List valuesList = new List(); for (int i = 0; i < values.Count; i++) { valuesList.Add(values[i].ToString()); } responseBody.values = valuesList; // 关闭连接 if (opcUaClient != null) { opcUaClient.Disconnect(); } } catch (Exception ex) { Console.WriteLine("连接失败报错:" + ex); responseBody.code=0; responseBody.msg = ex.Message; } return responseBody; } } }