HeidenhainServer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using HeidenhainDNCLib;
  2. using Newtonsoft.Json;
  3. using RequestServer.HttpServer;
  4. using ResponseServer.HttpServer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.NetworkInformation;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace Fanuc_HttpServer.hedidenain
  15. {
  16. class HeidenhainServer
  17. {
  18. private int iChannel = 0;
  19. private string RemotePath = "TNC:\\nc_prog\\ATUO";//ConfigurationManager.AppSettings["RemotePath"];
  20. private static string ON_LINE_STATE = "在线";
  21. private Dictionary<String, List<string>> jcIpDict = new Dictionary<String, List<string>>();
  22. //连接设备列表
  23. public Dictionary<string, DNC_STATE> deviceList { get; set; } = new Dictionary<string, DNC_STATE>();
  24. private DNC_STATE m_ControlState;
  25. public static Dictionary<string, JHMachineInProcess> machineList { get; set; } = new Dictionary<string, JHMachineInProcess>();
  26. public HeidenhainServer()
  27. {
  28. List<string> ip1 = new List<string>();
  29. ip1.Add("\\PLC\\memory\\D\\17600");
  30. ip1.Add("\\PLC\\memory\\D\\4448");
  31. jcIpDict.Add("192.168.10.109", ip1);
  32. List<string> ip2 = new List<string>();
  33. ip2.Add("\\PLC\\memory\\D\\368");
  34. ip2.Add("\\PLC\\memory\\D\\5048");
  35. jcIpDict.Add("192.168.10.101", ip2);
  36. }
  37. public ResponseBody requestHttpServer(RequestBody<ToolData> requestBody)
  38. {
  39. string ip = requestBody.serverUrl;
  40. string port = requestBody.port;
  41. string fun = requestBody.type;
  42. ResponseBody responseBody = new ResponseBody();
  43. JHMachineInProcess Machine = null;
  44. if (fun == ActionTypeEnum.Collect.ToString())
  45. {
  46. Ping pingSender = new Ping();
  47. PingReply reply = pingSender.Send(requestBody.serverUrl);
  48. if (reply.Status != IPStatus.Success)
  49. {
  50. //responseBody.result = false;
  51. responseBody.msg = "调用失败";
  52. }
  53. else
  54. {
  55. }
  56. }
  57. return responseBody;
  58. }
  59. private DNC_STATE connect(string connectName)
  60. {
  61. DNC_CNC_TYPE CncType;
  62. IJHConnectionList connectionList = null;
  63. IJHConnection connection = null;
  64. try
  65. {
  66. JHMachineInProcess Machine = new JHMachineInProcess();
  67. Machine.ConnectRequest(connectName);
  68. string sCurrentMachine = Machine.currentMachine;
  69. // Find out control type
  70. connectionList = Machine.ListConnections();
  71. for (int i = 0; i < connectionList.Count; i++)
  72. {
  73. connection = connectionList[i];
  74. if (connection.name == sCurrentMachine)
  75. {
  76. CncType = connection.cncType;
  77. }
  78. if (connection != null)
  79. Marshal.ReleaseComObject(connection);
  80. }
  81. // //DNC连接正常,加入数组 第一次连接加入数组,以支持多台设备
  82. machineList.Add(connectName, Machine);
  83. Thread.Sleep(20);
  84. return Machine.GetState();
  85. }
  86. catch (COMException cex)
  87. {
  88. YG.Log.Instance.WriteLogAdd($"海德汉响应结果-cex-->> " + cex.Message);
  89. return DNC_STATE.DNC_STATE_NOT_INITIALIZED;
  90. }
  91. catch (Exception ex)
  92. {
  93. YG.Log.Instance.WriteLogAdd($"海德汉响应结果-ex-->> " + ex.Message);
  94. return DNC_STATE.DNC_STATE_NOT_INITIALIZED;
  95. }
  96. finally
  97. {
  98. if (connectionList != null)
  99. Marshal.ReleaseComObject(connectionList);
  100. if (connection != null)
  101. Marshal.ReleaseComObject(connection);
  102. }
  103. }
  104. private string GenPath(string part1, string part2)
  105. {
  106. string sFullPath = part1;
  107. switch (part2)
  108. {
  109. case ".":
  110. break;
  111. case "..":
  112. if (part1.EndsWith(@"\") && part1.Length > 5)
  113. part1 = part1.Substring(0, part1.Length - 3);
  114. int iLastFolderPos = part1.LastIndexOf(@"\");
  115. if (iLastFolderPos >= 0)
  116. sFullPath = part1.Substring(0, iLastFolderPos + 1);
  117. break;
  118. default:
  119. if (part1.EndsWith(@"\"))
  120. sFullPath = part1 + part2;
  121. else
  122. sFullPath = part1 + @"\" + part2;
  123. break;
  124. }
  125. return sFullPath;
  126. }
  127. }
  128. }