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/ModbusHelper.cs |  234 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 234 insertions(+), 0 deletions(-)

diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/ModbusHelper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/ModbusHelper.cs
new file mode 100644
index 0000000..30b1d16
--- /dev/null
+++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/ModbusHelper.cs
@@ -0,0 +1,234 @@
+锘縰sing EasyModbus;
+using HH.WCS.Mobox3.FJJT;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HH.WCS.JunzhouNongfu.device
+{
+    /// <summary>
+    /// modbus tcp 鐢ㄧ涓夋柟鐨勫寘
+    /// </summary>
+    internal static class ModbusHelper
+    {
+        private static Dictionary<string, ModbusClient> clients = new Dictionary<string, ModbusClient>();
+
+        /// <summary>
+        /// 璇荤嚎鍦�+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="qty"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        /// <returns></returns>
+        internal static bool[] ReadCoils(int address, int qty, string ip, int port = 502) {
+            bool[] res = new bool[0];
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    res = client.ReadCoils(address, qty);
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+
+        }
+        /// <summary>
+        /// 璇荤鏁h緭鍏�+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="qty"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        /// <returns></returns>
+        internal static bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502) {
+            bool[] res = new bool[0];
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    res = client.ReadDiscreteInputs(address, qty);
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+
+        }
+        /// <summary>
+        /// 璇讳繚鎸佸瘎瀛樺櫒
+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="qty"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        /// <returns></returns>
+        internal static int[] ReadHoldingRegisters(int address, int qty, string ip, int port = 502) {
+            int[] res = new int[0];
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    //涓�釜瀵勫瓨鍣ㄦ槸16浣嶏紝杩斿洖2涓猧nt绫诲瀷
+                    res = client.ReadHoldingRegisters(address, qty);
+                    if (res != null) {
+                        Console.WriteLine($"璇诲瘎瀛樺櫒{ip},address={address},qty={qty},length={res.Length},res={string.Join(",", res)}");
+                    }
+                    else {
+                        Console.WriteLine($"璇诲瘎瀛樺櫒{ip},address={address},qty={qty},澶辫触");
+                    }
+                }
+                catch (Exception ex) {
+                    Console.WriteLine("ReadHoldingRegisters:err=" + ex.Message);
+                }
+            }
+            else {
+                Console.WriteLine($"{ip}杩炴帴宸茬粡鏂紑");
+            }
+            return res;
+        }
+        /// <summary>
+        /// 璇昏緭鍏ュ瘎瀛樺櫒
+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="qty"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        /// <returns></returns>
+        internal static int[] ReadInputRegisters(int address, int qty, string ip, int port = 502) {
+            int[] res = new int[0];
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    res = client.ReadInputRegisters(address, qty);
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+
+        }
+        /// <summary>
+        /// 鍐欏崟涓嚎鍦�+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="value"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        internal static bool WriteSingleCoil(int address, bool value, string ip, int port = 502) {
+            var res = false;
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteSingleCoil(address, value);
+                    res = true;
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+
+        }
+        /// <summary>
+        /// 鍐欏涓嚎鍦�+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="values"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        internal static bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502) {
+            var res = false;
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteMultipleCoils(address, values);
+                    res = true;
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+
+        }
+        /// <summary>
+        /// 鍐欏崟涓瘎瀛樺櫒
+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="value"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        internal static bool WriteSingleRegister(int address, int value, string ip, int port = 502) {
+            var res = false;
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteSingleRegister(address, value);
+                    res = true;
+                    Console.WriteLine($"鍐欏瘎瀛樺櫒{ip},address={address},value={value},鎴愬姛");
+
+                }
+                catch (Exception ex) {
+                    Console.WriteLine(ex.Message);
+                    Console.WriteLine($"鍐欏瘎瀛樺櫒{ip},address={address},value={value},澶辫触");
+                }
+            }
+            return res;
+        }
+        /// <summary>
+        /// 鍐欏涓瘎瀛樺櫒
+        /// </summary>
+        /// <param name="address"></param>
+        /// <param name="values"></param>
+        /// <param name="ip"></param>
+        /// <param name="port"></param>
+        internal static bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 502) {
+            var res = false;
+            var client = GetClient(ip, port);
+            if (client != null && client.Connected) {
+                try {
+                    client.WriteMultipleRegisters(address, values);
+                    res = true;
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                }
+            }
+            return res;
+        }
+        private static ModbusClient GetClient(string ip, int port) {
+            ModbusClient client = null;
+            if (!clients.ContainsKey(ip)) {
+                client = new ModbusClient(ip, port);
+                try {
+                    client.Connect();
+                    clients[ip] = client;
+                }
+                catch (Exception ex) {
+                    //LogHelper.Error(ex.Message, ex);
+                    Console.WriteLine($"杩炴帴plc澶辫触{ip},err=" + ex.Message);
+                }
+            }
+            else {
+                client = clients[ip];
+                if (!clients[ip].Connected) {
+                    try {
+                        clients[ip].Connect();
+
+                    }
+                    catch (Exception ex) {
+                        //LogHelper.Error(ex.Message, ex);
+                        Console.WriteLine($"杩炴帴plc澶辫触{ip},err=" + ex.Message);
+                    }
+                }
+
+            }
+            return client;
+        }
+    }
+}

--
Gitblit v1.9.1