using HeidenhainDNCLib; using Newtonsoft.Json; using RequestServer.HttpServer; using ResponseServer.HttpServer; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.NetworkInformation; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Fanuc_HttpServer.hedidenain { class HeidenhainServer { private int iChannel = 0; private string RemotePath = "TNC:\\nc_prog\\ATUO";//ConfigurationManager.AppSettings["RemotePath"]; private static string ON_LINE_STATE = "在线"; private Dictionary> jcIpDict = new Dictionary>(); //连接设备列表 public Dictionary deviceList { get; set; } = new Dictionary(); private DNC_STATE m_ControlState; public static Dictionary machineList { get; set; } = new Dictionary(); public HeidenhainServer() { List ip1 = new List(); ip1.Add("\\PLC\\memory\\D\\17600"); ip1.Add("\\PLC\\memory\\D\\4448"); jcIpDict.Add("192.168.10.109", ip1); List ip2 = new List(); ip2.Add("\\PLC\\memory\\D\\368"); ip2.Add("\\PLC\\memory\\D\\5048"); jcIpDict.Add("192.168.10.101", ip2); } public ResponseBody requestHttpServer(RequestBody requestBody) { string ip = requestBody.serverUrl; string port = requestBody.port; string fun = requestBody.type; ResponseBody responseBody = new ResponseBody(); JHMachineInProcess Machine = null; if (fun == ActionTypeEnum.Collect.ToString()) { Ping pingSender = new Ping(); PingReply reply = pingSender.Send(requestBody.serverUrl); if (reply.Status != IPStatus.Success) { //responseBody.result = false; responseBody.msg = "调用失败"; } else { } } return responseBody; } private DNC_STATE connect(string connectName) { DNC_CNC_TYPE CncType; IJHConnectionList connectionList = null; IJHConnection connection = null; try { JHMachineInProcess Machine = new JHMachineInProcess(); Machine.ConnectRequest(connectName); string sCurrentMachine = Machine.currentMachine; // Find out control type connectionList = Machine.ListConnections(); for (int i = 0; i < connectionList.Count; i++) { connection = connectionList[i]; if (connection.name == sCurrentMachine) { CncType = connection.cncType; } if (connection != null) Marshal.ReleaseComObject(connection); } // //DNC连接正常,加入数组 第一次连接加入数组,以支持多台设备 machineList.Add(connectName, Machine); Thread.Sleep(20); return Machine.GetState(); } catch (COMException cex) { YG.Log.Instance.WriteLogAdd($"海德汉响应结果-cex-->> " + cex.Message); return DNC_STATE.DNC_STATE_NOT_INITIALIZED; } catch (Exception ex) { YG.Log.Instance.WriteLogAdd($"海德汉响应结果-ex-->> " + ex.Message); return DNC_STATE.DNC_STATE_NOT_INITIALIZED; } finally { if (connectionList != null) Marshal.ReleaseComObject(connectionList); if (connection != null) Marshal.ReleaseComObject(connection); } } private string GenPath(string part1, string part2) { string sFullPath = part1; switch (part2) { case ".": break; case "..": if (part1.EndsWith(@"\") && part1.Length > 5) part1 = part1.Substring(0, part1.Length - 3); int iLastFolderPos = part1.LastIndexOf(@"\"); if (iLastFolderPos >= 0) sFullPath = part1.Substring(0, iLastFolderPos + 1); break; default: if (part1.EndsWith(@"\")) sFullPath = part1 + part2; else sFullPath = part1 + @"\" + part2; break; } return sFullPath; } } }