OpcUaUtils.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Newtonsoft.Json;
  2. using Opc.Ua;
  3. using OpcUaHelper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Kede;
  11. using Newtonsoft.Json.Linq;
  12. using System.IO;
  13. namespace TestAgreement.utils
  14. {
  15. internal class OpcUaUtils
  16. {
  17. public static ResponseBody OpcCaiji(string url,string userName,string password,List<string> list)
  18. {
  19. Console.WriteLine("===============科德机床OPCUa数据采集start================");
  20. ResponseBody responseBody = new ResponseBody();
  21. responseBody.code = 1;
  22. try
  23. {
  24. /*string url = "opc.tcp://192.168.11.63:12686";
  25. string userName = "";
  26. string password = "";*/
  27. OpcUaClient opcUaClient = new OpcUaClient();
  28. // 创建连接
  29. if (!string.IsNullOrEmpty(url) && string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password))
  30. {
  31. opcUaClient.ConnectServer(url);
  32. }
  33. else if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
  34. {
  35. opcUaClient.UserIdentity = new UserIdentity(userName, password);
  36. opcUaClient.ConnectServer(url);
  37. }
  38. // 批量获取值
  39. List<NodeId> nodeIdList = new List<NodeId>();
  40. list.ForEach(vo =>
  41. {
  42. nodeIdList.Add(vo);
  43. });
  44. //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//主轴负载
  45. //nodeIdList.Add("ns=2;s=cncVariablesInfo/variables-var1");//进给倍率
  46. List<DataValue> values = opcUaClient.ReadNodes(nodeIdList.ToArray());
  47. List<string> valuesList = new List<string>();
  48. for (int i = 0; i < values.Count; i++)
  49. {
  50. if (values[i].ToString().Contains(";")){
  51. string[] parts = values[i].ToString().Split(";");
  52. // 创建字典来存储解析后的数据
  53. Dictionary<string, string> data = new Dictionary<string, string>();
  54. string mon = null;
  55. // 逐个处理键值对
  56. foreach (string part in parts)
  57. {
  58. if (part.StartsWith("MON"))//0-关闭;1-时间;2-磨损量;3-计件
  59. {
  60. mon = part.Substring(3);
  61. data["mon"] = part.Substring(3);
  62. }
  63. if (part.StartsWith("NUM"))//刀具号
  64. {
  65. data["number"] = part.Substring(3);
  66. }
  67. else if (part.StartsWith("NAM"))//刀具名
  68. {
  69. data["name"] = part.Substring(3);
  70. }
  71. else if (part.StartsWith("LIF"))//刀具磨损量
  72. {
  73. data["wear"] = part.Substring(3);
  74. }
  75. else if (part.StartsWith("ALM") && mon == "1")//刀具寿命预警
  76. {
  77. data["warnLife"] = part.Substring(3);
  78. }
  79. else if (part.StartsWith("TAR"))//刀具寿命目标值
  80. {
  81. data["targetLife"] = part.Substring(3);
  82. }
  83. else if (part.StartsWith("STR"))//刀具半径
  84. {
  85. data["toolRadius"] = part.Substring(3);
  86. }
  87. else if (part.StartsWith("EDP")) //刀具位置
  88. {
  89. data["toolPosition"] = part.Substring(3);
  90. }
  91. else if (part.StartsWith("ALM") && mon == "2")//刀具磨损量预警
  92. {
  93. data["wearWarn"] = part.Substring(3);
  94. }
  95. }
  96. // 将字典转换为JSON格式的字符串
  97. string json = JsonConvert.SerializeObject(data, Formatting.Indented);
  98. valuesList.Add(json);
  99. }
  100. else
  101. {
  102. valuesList.Add(values[i].ToString());
  103. }
  104. }
  105. responseBody.values = valuesList;
  106. // 关闭连接
  107. if (opcUaClient != null)
  108. {
  109. opcUaClient.Disconnect();
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. Console.WriteLine("连接失败报错:" + ex);
  115. responseBody.code=0;
  116. responseBody.msg = ex.Message;
  117. }
  118. return responseBody;
  119. }
  120. }
  121. }