| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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<string> 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<NodeId> nodeIdList = new List<NodeId>();
- list.ForEach(vo =>
- {
- nodeIdList.Add(vo);
- });
- //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//主轴负载
- //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//进给倍率
- List<DataValue> values = opcUaClient.ReadNodes(nodeIdList.ToArray());
-
-
- List<string> valuesList = new List<string>();
- 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;
- }
- }
- }
|