using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System; using System.Collections.Generic; using System.IO; using System.Net.Sockets; using System.Text; using System.Xml; using Microsoft.Extensions.Logging; namespace Kede.Utils { public class SocketUtil { public static void socket(string deviceIp, string ncName,ResponseBody response) { // 机床 IP //string deviceIp = "192.168.11.41"; // 机床端口 int devicePort = 62937; // 通过 SFTP 上传的 NC 名称 //string ncName = "X1.nc"; // 通过 SFTP 上传到机床 NC 的地址,注意必须使用"/" 斜杠,不能使用"\", 末尾必须加上"/" string ncPath = "D:/NCprog/testNc/"; StringBuilder xmlBuffer = new StringBuilder(); xmlBuffer.Append(""); YG.Log.Instance.WriteLogAdd($"选中nc程序请求id{deviceIp},port{devicePort},par{xmlBuffer}"); string xmlResult = SocketUtil.SendSocket(deviceIp, devicePort, xmlBuffer.ToString()); // YG.Log.Instance.WriteLogAdd($"选中nc程序请求返回结果{xmlResult}"); StringBuilder checkBuffer = new StringBuilder(); checkBuffer.Append("prg"); string checkResult = SocketUtil.SendSocket(deviceIp, devicePort, checkBuffer.ToString()); YG.Log.Instance.WriteLogAdd($"检查机床当前使用nc程序{checkResult}"); //D:/NCprog/testNc/X.nc string xmlFileName = ExtractFileNameFromXml(xmlResult, "name"); string checkFileName = ExtractFileNameFromXml(checkResult, "prg"); if (xmlFileName != checkFileName) { response.msg = $"设置机床主程序nc程序{xmlFileName}失败"; response.code = 0; } // 若需要对返回的 XML 结果进行解析 // List paramList = new List { "AUTO IWO3" }; // Dictionary resultMap = SocketUtil.ResultMap(xmlResult, paramList); // log.Info($"请求结果解析{resultMap}"); } public static string SendSocket(string hostName, int portNumber, string xmlBuilder) { string responseLine = null; try { using (TcpClient client = new TcpClient(hostName, portNumber)) using (NetworkStream stream = client.GetStream()) using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true }) using (StreamReader reader = new StreamReader(stream, Encoding.ASCII)) { // 发送 XML 到服务器 writer.WriteLine(xmlBuilder); writer.WriteLine(); // 空行表示 XML 的结束 // 接收服务器的响应 responseLine = reader.ReadLine(); YG.Log.Instance.WriteLogAdd("sendSocket - 服务器的回应XML::\n" + responseLine); } } catch (SocketException e) { YG.Log.Instance.WriteLogAdd("sendSocket - 无法连接到主机:" + hostName + " 错误原因:" + e.Message); Environment.Exit(1); } catch (IOException e) { YG.Log.Instance.WriteLogAdd("I/O 错误 " + hostName + " 错误原因:" + e.Message); } return responseLine; } public static Dictionary ResultMap(string xmlString, List paramList) { Dictionary dataMap = new Dictionary(); try { // 加载 XML 字符串 XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlString); // 获取根元素 XmlElement rootElement = doc.DocumentElement; foreach (string param in paramList) { // 获取元素 XmlNode valueElement = rootElement.GetElementsByTagName(param).Item(0); if (valueElement != null) { // 提取值 string title = valueElement.InnerText; // 输出结果 dataMap[param] = title; } } } catch (Exception e) { YG.Log.Instance.WriteLogAdd("XML 解析错误:" + e.Message); } return dataMap; } private static string ExtractFileNameFromXml(string xml, string tagName) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNode node = doc.GetElementsByTagName(tagName).Item(0); if (node != null) { if (tagName == "prg") { // 从完整路径中提取文件名 return System.IO.Path.GetFileName(node.InnerText); } else { return node.InnerText; } } return null; } } }