From b7308bba3d7ffad271ce7fc7a93c8c45d76be87d Mon Sep 17 00:00:00 2001 From: 杨前锦 <1010338399@qq.com> Date: 星期五, 13 六月 2025 17:21:03 +0800 Subject: [PATCH] 优化印尼佳通-硫化胚胎出入库逻辑策略优化 --- HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/TcpClient.cs | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 85 insertions(+), 0 deletions(-) diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/TcpClient.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/TcpClient.cs new file mode 100644 index 0000000..6529f6b --- /dev/null +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/TcpClient.cs @@ -0,0 +1,85 @@ +锘縰sing HH.WCS.Mobox3.FJJT; +using HH.WCS.Mobox3.FJJT.device; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace HH.WCS.Mobox3.FJJT.device { + internal class TcpClient + { + /// <summary> + /// + /// </summary> + /// <param name="ip">127.0.0.1</param> + /// <param name="port">8888</param> + /// <param name="hex">01 02 00 00 00 0C 78 0F</param> + /// <returns></returns> + public static string SendHexOnce(string ip, int port, string hex) { + var res = string.Empty; + Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + client.Connect(ip, port); + client.ReceiveTimeout = 2000; + if (client.Connected) { + client.Send(PlcHelper.Hex2Bytes(hex)); + byte[] buffer = new byte[1024]; + try { + var length = client.Receive(buffer, SocketFlags.None); + byte[] data = new byte[length]; + Array.Copy(buffer, data, length); + res = BitConverter.ToString(data).Replace("-", ""); + } + catch (Exception ex) { + LogHelper.Error(ex.Message, ex); + } + client.Disconnect(true); + client.Dispose(); + } + client = null; + return res; + } + + /// <summary> + /// 璇讳繚鎸佸瘎瀛樺櫒锛宮odbus rtu鐨勫皝瑁�+ /// </summary> + /// <param name="address"></param> + /// <param name="qty"></param> + /// <param name="ip"></param> + /// <param name="port"></param> + /// <returns></returns> + internal int[] ReadInputRegistersRtu(int address, int qty, string ip, int port = 502) { + List<int> res = new List<int>(); + var hex = $"0103{address.ToString("X4")}{qty.ToString("X4")}"; + hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", ""); + var data = SendHexOnce(ip, port, hex); + if (!string.IsNullOrEmpty(data)) { + if (PlcHelper.CheckCRC(data)) { + var lenght = Convert.ToInt16(data.Substring(4, 2), 16) / 2; + for (int i = 0; i < lenght; i++) { + res[i] = Convert.ToInt16(data.Substring(6 + 4 * 1, 4), 16); + } + } + } + return res.ToArray(); + } + /// <summary> + /// 鍐欏崟涓瘎瀛樺櫒 + /// </summary> + /// <param name="address"></param> + /// <param name="value"></param> + /// <param name="ip"></param> + /// <param name="port"></param> + internal bool WriteSingleRegisterRtu(int address, int value, string ip, int port = 502) { + var res = false; + var hex = $"0106{address.ToString("X4")}{value.ToString("X4")}"; + hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", ""); + var data = SendHexOnce(ip, port, hex); + if (!string.IsNullOrEmpty(data)) { + res = true; + } + return res; + } + } +} -- Gitblit v1.9.1