123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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;
- using System.IO;
- 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++)
- {
- if (values[i].ToString().Contains(";")){
- string[] parts = values[i].ToString().Split(";");
- // 创建字典来存储解析后的数据
- Dictionary<string, string> data = new Dictionary<string, string>();
- string mon = null;
- // 逐个处理键值对
- foreach (string part in parts)
- {
- if (part.StartsWith("MON"))//0-关闭;1-时间;2-磨损量;3-计件
- {
- mon = part.Substring(3);
- data["mon"] = part.Substring(3);
- }
- if (part.StartsWith("NUM"))//刀具号
- {
- data["number"] = part.Substring(3);
- }
- else if (part.StartsWith("NAM"))//刀具名
- {
- data["name"] = part.Substring(3);
- }
- else if (part.StartsWith("LIF"))//刀具磨损量
- {
- data["wear"] = part.Substring(3);
- }
- else if (part.StartsWith("ALM") && mon == "1")//刀具寿命预警
- {
- data["warnLife"] = part.Substring(3);
- }
- else if (part.StartsWith("TAR"))//刀具寿命目标值
- {
- data["targetLife"] = part.Substring(3);
- }
- else if (part.StartsWith("STR"))//刀具半径
- {
- data["toolRadius"] = part.Substring(3);
- }
- else if (part.StartsWith("EDP")) //刀具位置
- {
- data["toolPosition"] = part.Substring(3);
- }
- else if (part.StartsWith("ALM") && mon == "2")//刀具磨损量预警
- {
- data["wearWarn"] = part.Substring(3);
- }
- }
- // 将字典转换为JSON格式的字符串
- string json = JsonConvert.SerializeObject(data, Formatting.Indented);
- valuesList.Add(json);
- }
- else
- {
- 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;
- }
- }
- }
|