From ad56deb081831090408f97b1c73e61a2eaed8657 Mon Sep 17 00:00:00 2001
From: kazelee <1847801760@qq.com>
Date: 星期二, 24 六月 2025 17:22:48 +0800
Subject: [PATCH] 整合部分Modbus和TCP Socket方式处理设备交互的逻辑

---
 device/ModbusFactory.cs                                                             |  421 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 /dev/null                                                                           |    0 
 .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/f3ed8dde-3902-4d3f-824a-8f00db530827.vsidx |    0 
 .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/140a3f27-91f0-4f76-83cb-8a8da5896f2a.vsidx |    0 
 .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c349b9a0-3c00-4a74-bcf5-5ba46f79eacb.vsidx |    0 
 HH.WCS.Mobox3.DSZSH.csproj                                                          |    1 
 .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/51f15dd6-39b1-4e8d-8d5a-7cdebd5ca9d8.vsidx |    0 
 7 files changed, 422 insertions(+), 0 deletions(-)

diff --git a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c4ac5a83-2808-437a-8fe9-e1ee6fd17012.vsidx b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/140a3f27-91f0-4f76-83cb-8a8da5896f2a.vsidx
similarity index 65%
rename from .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c4ac5a83-2808-437a-8fe9-e1ee6fd17012.vsidx
rename to .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/140a3f27-91f0-4f76-83cb-8a8da5896f2a.vsidx
index 88c69e3..4477ac6 100644
--- a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c4ac5a83-2808-437a-8fe9-e1ee6fd17012.vsidx
+++ b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/140a3f27-91f0-4f76-83cb-8a8da5896f2a.vsidx
Binary files differ
diff --git a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/340c6236-9991-45c1-b640-67c509d34eff.vsidx b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/340c6236-9991-45c1-b640-67c509d34eff.vsidx
deleted file mode 100644
index 4f14c31..0000000
--- a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/340c6236-9991-45c1-b640-67c509d34eff.vsidx
+++ /dev/null
Binary files differ
diff --git a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/43faf91b-3c60-45a1-9be8-c044d303f12b.vsidx b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/51f15dd6-39b1-4e8d-8d5a-7cdebd5ca9d8.vsidx
similarity index 70%
rename from .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/43faf91b-3c60-45a1-9be8-c044d303f12b.vsidx
rename to .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/51f15dd6-39b1-4e8d-8d5a-7cdebd5ca9d8.vsidx
index 6385482..8e0418e 100644
--- a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/43faf91b-3c60-45a1-9be8-c044d303f12b.vsidx
+++ b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/51f15dd6-39b1-4e8d-8d5a-7cdebd5ca9d8.vsidx
Binary files differ
diff --git a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/70dcfb51-010f-40e1-9d83-9060381efdfe.vsidx b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c349b9a0-3c00-4a74-bcf5-5ba46f79eacb.vsidx
similarity index 66%
rename from .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/70dcfb51-010f-40e1-9d83-9060381efdfe.vsidx
rename to .vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c349b9a0-3c00-4a74-bcf5-5ba46f79eacb.vsidx
index 67db0ef..783f818 100644
--- a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/70dcfb51-010f-40e1-9d83-9060381efdfe.vsidx
+++ b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/c349b9a0-3c00-4a74-bcf5-5ba46f79eacb.vsidx
Binary files differ
diff --git a/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/f3ed8dde-3902-4d3f-824a-8f00db530827.vsidx b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/f3ed8dde-3902-4d3f-824a-8f00db530827.vsidx
new file mode 100644
index 0000000..6ceab82
--- /dev/null
+++ b/.vs/HH.WCS.Mobox3.DSZSH/FileContentIndex/f3ed8dde-3902-4d3f-824a-8f00db530827.vsidx
Binary files differ
diff --git a/HH.WCS.Mobox3.DSZSH.csproj b/HH.WCS.Mobox3.DSZSH.csproj
index b59c6b7..4df2d0e 100644
--- a/HH.WCS.Mobox3.DSZSH.csproj
+++ b/HH.WCS.Mobox3.DSZSH.csproj
@@ -235,6 +235,7 @@
     <Compile Include="core\Monitor.cs" />
     <Compile Include="core\WCSCore.cs" />
     <Compile Include="core\WMSCore.cs" />
+    <Compile Include="device\ModbusFactory.cs" />
     <Compile Include="device\TcpClientHelper.cs" />
     <Compile Include="models\TN_Outbound_Plan.cs" />
     <Compile Include="models\TN_Record_Table.cs" />
diff --git a/device/ModbusFactory.cs b/device/ModbusFactory.cs
new file mode 100644
index 0000000..28578f8
--- /dev/null
+++ b/device/ModbusFactory.cs
@@ -0,0 +1,421 @@
+锘縰sing System;
+using System.Net.Sockets;
+using System.Net;
+using Microsoft.Owin.BuilderProperties;
+using System.Web.Services.Description;
+using Org.BouncyCastle.Utilities.Net;
+using System.Web.UI.WebControls.WebParts;
+using System.Linq;
+
+using TcpClient = System.Net.Sockets.TcpClient;
+
+namespace HH.WCS.Mobox3.DSZSH.device {
+    // 瀹氫箟Modbus閫氳鎺ュ彛
+    public interface IModbusCommunicator : IDisposable {
+        bool IsConnected { get; }
+
+        void Link(string ipAddress, int port);
+        void Disconnect();
+        void ReconnectAll();
+
+        // 甯哥敤鐨凪odbus鍔熻兘鐮佹柟娉�+        /// <summary>
+        /// 璇讳竴涓垨澶氫釜绾垮湀锛岃繑鍥炰竴涓猙it鐪熷亣鏁扮粍
+        /// </summary>
+        /// <param name="startingAddress"></param>
+        /// <param name="quantity"></param>
+        /// <returns></returns>
+        bool[] ReadCoils(int startingAddress, int quantity);
+        /// <summary>
+        /// 璇讳竴涓垨澶氫釜绂绘暎杈撳叆锛岃繑鍥炰竴涓猙it鐪熷亣鏁扮粍
+        /// </summary>
+        /// <param name="startingAddress"></param>
+        /// <param name="quantity"></param>
+        /// <returns></returns>
+        bool[] ReadDiscreteInputs(int startingAddress, int quantity);
+        /// <summary>
+        /// 鎵归噺璇诲彇鎴栧崟鐙鍙栦繚鎸佸瘎瀛樺櫒锛岃繑鍥炵殑鏄�2浣峣nt鏁扮粍
+        /// </summary>
+        /// <param name="startingAddress"></param>
+        /// <param name="quantity"></param>
+        /// <returns></returns>
+        int[] ReadHoldingRegisters(int startingAddress, int quantity);
+        /// <summary>
+        /// 璇讳竴涓垨澶氫釜杈撳叆瀵勫瓨鍣紝杩斿洖涓�釜int32浣嶆暟缁�+        /// </summary>
+        /// <param name="startingAddress"></param>
+        /// <param name="quantity"></param>
+        /// <returns></returns>
+        int[] ReadInputRegisters(int startingAddress, int quantity);
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="coilAddress"></param>
+        /// <param name="value"></param>
+        bool WriteSingleCoil(int coilAddress, bool value);
+        bool WriteSingleRegister(int registerAddress, ushort value);
+        bool WriteMultipleCoils(int startingAddress, bool[] values);
+        bool WriteMultipleRegisters(int startingAddress, int[] values);
+    }
+
+    // Modbus閫氳鏂瑰紡鏋氫妇
+    public enum ModbusCommunicationType {
+        EasyModbus,
+        TcpSocket
+    }
+
+    // Modbus閫氳宸ュ巶绫�+    public static class ModbusFactory {
+        public static IModbusCommunicator CreateCommunicator(ModbusCommunicationType type) {
+            switch (type) {
+                case ModbusCommunicationType.EasyModbus:
+                    return new EasyModbusCommunicator();
+                case ModbusCommunicationType.TcpSocket:
+                    return new TcpSocketCommunicator();
+                default:
+                    //throw new ArgumentException("涓嶆敮鎸佺殑Modbus閫氫俊鏂瑰紡锛氳閫夋嫨EasyModbus鎴朤cpSocket");
+                    LogHelper.Info("涓嶆敮鎸佺殑Modbus閫氫俊鏂瑰紡锛氳閫夋嫨EasyModbus鎴朤cpSocket");
+                    return null;
+            }
+        }
+
+        static void Test() {
+            var communicator = ModbusFactory.CreateCommunicator(ModbusCommunicationType.TcpSocket);
+        }
+    }
+
+    // EasyModbus瀹炵幇
+    public class EasyModbusCommunicator : IModbusCommunicator {
+        private EasyModbus.ModbusClient _modbusClient;
+
+        public bool IsConnected => _modbusClient?.Connected ?? false;
+
+        public void Link(string ipAddress, int port) {
+            if (IsConnected) {
+                LogHelper.Info($"(ip:{ipAddress}, port:{port})宸茬粡杩炴帴");
+                return;
+            }
+
+            _modbusClient = new EasyModbus.ModbusClient(ipAddress, port);
+            _modbusClient.Connect();
+            
+            if (IsConnected) {
+                LogHelper.Info($"杩炴帴鎴愬姛锛歿ipAddress}:{port}");
+            }
+            else {
+                LogHelper.Info($"杩炴帴澶辫触锛歿ipAddress}:{port}");
+            }
+        }
+
+        public void Disconnect() {
+            if (!IsConnected) {
+                LogHelper.Info($"褰撳墠娌℃湁瑕佹柇寮�殑杩炴帴");
+                return;
+            }
+
+            _modbusClient?.Disconnect();
+
+            if (IsConnected) {
+                LogHelper.Info($"杩炴帴鎴愬姛锛歿_modbusClient.IPAddress}:{_modbusClient.Port}");
+            }
+            else {
+                LogHelper.Info($"杩炴帴澶辫触锛歿_modbusClient.IPAddress} : {_modbusClient.Port}");
+            }
+        }
+
+        public void ReconnectAll() {
+            if (_modbusClient != null) {
+                var ip = _modbusClient.IPAddress;
+                var port = _modbusClient.Port;
+                Disconnect();
+                Link(ip, port);
+            }
+        }
+
+        public bool[] ReadCoils(int startingAddress, int quantity) {
+            bool[] res = new bool[0];
+            if (_modbusClient != null && IsConnected) {
+                try {
+                    res = _modbusClient.ReadCoils(startingAddress, quantity);
+                    if (res.Length != 0) {
+                        //璇诲彇鎴愬姛
+                        LogHelper.Info($"璇诲彇鎴愬姛锛歊eadCoils");
+                    }
+                    else {
+                        //璇诲彇澶辫触
+                        LogHelper.Info($"璇诲彇澶辫触锛歊eadCoils");
+                    }
+                }
+                catch (Exception ex) {
+                    LogHelper.InfoEx(ex);
+                }
+            }
+            else {
+                LogHelper.Info($"璁惧鏈繛鎺ワ紝璇诲彇澶辫触锛歊eadCoils");
+            }
+            return res;
+        }
+
+        public bool[] ReadDiscreteInputs(int startingAddress, int quantity) {
+            bool[] res = new bool[0];
+            if (_modbusClient != null && IsConnected) {
+                try {
+                    res = _modbusClient.ReadDiscreteInputs(startingAddress, quantity);
+                    if (res.Length != 0) {
+                        //璇诲彇鎴愬姛
+                        LogHelper.Info($"璇诲彇鎴愬姛锛歊eadDiscreteInputs");
+                    }
+                    else {
+                        //璇诲彇澶辫触
+                        LogHelper.Info($"璇诲彇澶辫触锛歊eadDiscreteInputs");
+                    }
+                }
+                catch (Exception ex) {
+                    LogHelper.InfoEx(ex);
+                }
+            }
+            else {
+                LogHelper.Info($"璁惧鏈繛鎺ワ紝璇诲彇澶辫触锛歊eadDiscreteInputs");
+            }
+            return res;
+        }
+
+        public int[] ReadHoldingRegisters(int startingAddress, int quantity) {
+            int[] res = new int[0];
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            if (client != null && client.Connected) {
+                try {
+                    //涓�釜瀵勫瓨鍣ㄦ槸16浣嶏紝杩斿洖2涓猧nt绫诲瀷
+                    res = client.ReadHoldingRegisters(startingAddress, quantity);
+                    if (res.Length != 0) {
+                        //璇诲彇鎴愬姛
+                    }
+                    else {
+                        //璇诲彇澶辫触
+                    }
+                }
+                catch (Exception ex) {
+                    //濡傛灉璇锋眰鏁伴噺瓒呭嚭淇濇寔瀵勫瓨鍣ㄧ殑鏈�ぇ鏁版嵁琛屾暟锛屼細鎶ラ敊
+                    LogHelper.Info($"鍙戠敓浜嗗紓甯�{ex.Message},IP:{ip},Port:{port}", "Error");
+                }
+            }
+            else {
+                LogHelper.Info($"鏈壘鍒癕odbus璁惧瀹炰緥瀵硅薄:IP:{ip},Port:{port}");
+            }
+            return res;
+        }
+
+        public int[] ReadInputRegisters(int startingAddress, int quantity) {
+            int[] res = new int[0];
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            if (client != null && client.Connected) {
+                try {
+                    res = client.ReadInputRegisters(startingAddress, quantity);
+                    if (res.Length != 0) {
+                        //璇诲彇鎴愬姛
+                    }
+                    else {
+                        //璇诲彇澶辫触
+                    }
+                }
+                catch (Exception ex) {
+
+                }
+            }
+            else {
+
+            }
+            return res;
+        }
+
+        public bool WriteSingleCoil(int coilAddress, bool value) {
+            var res = false;
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteSingleCoil(coilAddress, value);
+                    res = value == client.ReadCoils(coilAddress, 1)[0];
+                    if (res) {
+                        //鍐欏叆鎴愬姛
+                    }
+                    else {
+                        //鍐欏叆澶辫触
+                    }
+                }
+                catch (Exception ex) {
+
+                }
+            }
+            else {
+
+            }
+            return res;
+        }
+
+        public bool WriteSingleRegister(int registerAddress, ushort value) {
+            var res = false;
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteSingleRegister(registerAddress, value);
+                    res = value == client.ReadHoldingRegisters(registerAddress, 1)[0];
+                    if (res) {
+                        //鍐欏叆鎴愬姛
+                    }
+                    else {
+                        //鍐欏叆澶辫触
+                    }
+                }
+                catch (Exception ex) {
+
+                }
+            }
+            else {
+
+            }
+            return res;
+        }
+
+        public bool WriteMultipleCoils(int startingAddress, bool[] values) {
+            var res = false;
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteMultipleCoils(startingAddress, values);
+                    var dataRead = client.ReadCoils(startingAddress, values.Length);
+                    res = values.SequenceEqual(dataRead);
+                    if (res) {
+                        //鍐欏叆鎴愬姛
+                    }
+                    else {
+                        //鍐欏叆澶辫触
+                    }
+                }
+                catch (Exception ex) {
+
+                }
+            }
+            else {
+
+            }
+            return res;
+        }
+
+        public bool WriteMultipleRegisters(int startingAddress, int[] values) {
+            var res = false;
+            var client = _modbusClient;
+            var ip = client.IPAddress;
+            var port = client.Port;
+            var log = string.Join(",", values.Select(x => x.ToString()));
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteMultipleRegisters(startingAddress, values);
+                    var dataRead = client.ReadHoldingRegisters(startingAddress, values.Length);
+                    res = values.SequenceEqual(dataRead);
+                    if (res) {
+                        LogHelper.Info($"鍐欏叆鎴愬姛,IP:{ip},Port:{port},{log}");
+                    }
+                    else {
+                        LogHelper.Info($"鍐欏叆澶辫触,IP:{ip},Port:{port},{log}");
+                    }
+                }
+                catch (Exception ex) {
+                    LogHelper.Info($"鍙戠敓浜嗗紓甯�{ex.Message},IP:{ip},Port:{port},{log}", "Error");
+                }
+            }
+            else {
+                LogHelper.Info($"鏈厤缃殑璁惧淇℃伅,IP:{ip},Port:{port},{log}");
+            }
+            return res;
+        }
+
+        public void Dispose() {
+            //_modbusClient?.Dispose();
+        }
+    }
+
+    // TCPSocket瀹炵幇
+    public class TcpSocketCommunicator : IModbusCommunicator {
+        private System.Net.Sockets.TcpClient _tcpClient;
+        private NetworkStream _networkStream;
+
+        //public bool IsConnected => _tcpClient?.Connected ?? false;
+        public bool IsConnected { get { if (_tcpClient != null) { return true; } else { return false; } } }
+
+        public void Link(string ipAddress, int port) {
+            _tcpClient = new System.Net.Sockets.TcpClient();
+            _tcpClient.Connect(ipAddress, port);
+            _networkStream = _tcpClient.GetStream();
+        }
+
+        public void Disconnect() {
+            _networkStream?.Close();
+            _tcpClient?.Close();
+        }
+
+        public void ReconnectAll() {
+            if (_tcpClient != null) {
+                var ip = ((IPEndPoint)_tcpClient.Client.RemoteEndPoint).Address.ToString();
+                var port = ((IPEndPoint)_tcpClient.Client.RemoteEndPoint).Port;
+                Disconnect();
+                Link(ip, port);
+            }
+        }
+
+        // 浠ヤ笅鏂规硶闇�鏍规嵁浣犵殑Modbus TCP鍗忚鍏蜂綋瀹炵幇
+        public bool[] ReadCoils(int startingAddress, int quantity) {
+            // 瀹炵幇TCP鏂瑰紡璇诲彇绾垮湀
+            throw new NotImplementedException();
+        }
+
+        public bool[] ReadDiscreteInputs(int startingAddress, int quantity) {
+            // 瀹炵幇TCP鏂瑰紡璇诲彇绂绘暎杈撳叆
+            throw new NotImplementedException();
+        }
+
+        public int[] ReadHoldingRegisters(int startingAddress, int quantity) {
+            // 瀹炵幇TCP鏂瑰紡璇诲彇淇濇寔瀵勫瓨鍣�+            throw new NotImplementedException();
+        }
+
+        public int[] ReadInputRegisters(int startingAddress, int quantity) {
+            // 瀹炵幇TCP鏂瑰紡璇诲彇杈撳叆瀵勫瓨鍣�+            throw new NotImplementedException();
+        }
+
+        public bool WriteSingleCoil(int coilAddress, bool value) {
+            // 瀹炵幇TCP鏂瑰紡鍐欏叆鍗曚釜绾垮湀
+            throw new NotImplementedException();
+        }
+
+        public bool WriteSingleRegister(int registerAddress, ushort value) {
+            // 瀹炵幇TCP鏂瑰紡鍐欏叆鍗曚釜瀵勫瓨鍣�+            throw new NotImplementedException();
+        }
+
+        public bool WriteMultipleCoils(int startingAddress, bool[] values) {
+            // 瀹炵幇TCP鏂瑰紡鍐欏叆澶氫釜绾垮湀
+            throw new NotImplementedException();
+        }
+
+        public bool WriteMultipleRegisters(int startingAddress, int[] values) {
+            // 瀹炵幇TCP鏂瑰紡鍐欏叆澶氫釜瀵勫瓨鍣�+            throw new NotImplementedException();
+        }
+
+        public void Dispose() {
+            _networkStream?.Dispose();
+            //_tcpClient?.Dispose();
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1