From e1a97fc8b29f063e96e3ebbae2f07ee95b276069 Mon Sep 17 00:00:00 2001 From: lss <Lss@HanInfo> Date: 星期四, 05 六月 2025 17:25:31 +0800 Subject: [PATCH] 合肥佳通优化 --- HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs | 181 ++++++++++++++++++++++++++++---------------- 1 files changed, 114 insertions(+), 67 deletions(-) diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs index bbeb583..408f263 100644 --- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/ModbusHelper.cs @@ -1,5 +1,6 @@ 锘縰sing EasyModbus; using HH.WCS.JiaTong; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; @@ -23,14 +24,18 @@ /// <param name="ip"></param> /// <param name="port"></param> /// <returns></returns> - internal static bool[] ReadCoils(int address, int qty, string ip, int port = 502) { + 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 { + if (client != null && client.Connected) + { + try + { res = client.ReadCoils(address, qty); } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); } } @@ -45,51 +50,25 @@ /// <param name="ip"></param> /// <param name="port"></param> /// <returns></returns> - internal static bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502) { + 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 { + if (client != null && client.Connected) + { + try + { res = client.ReadDiscreteInputs(address, qty); } - catch (Exception ex) { + 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> @@ -98,14 +77,18 @@ /// <param name="ip"></param> /// <param name="port"></param> /// <returns></returns> - internal static int[] ReadInputRegisters(int address, int qty, string ip, int port = 502) { + 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 { + if (client != null && client.Connected) + { + try + { res = client.ReadInputRegisters(address, qty); } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); } } @@ -119,15 +102,19 @@ /// <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) { + 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 { + if (client != null && client.Connected) + { + try + { client.WriteSingleCoil(address, value); res = true; } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); } } @@ -141,21 +128,26 @@ /// <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) { + 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 { + if (client != null && client.Connected) + { + try + { client.WriteMultipleCoils(address, values); res = true; } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); } } return res; } + /// <summary> /// 鍐欏崟涓瘎瀛樺櫒 /// </summary> @@ -163,20 +155,63 @@ /// <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) { + 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 { + if (client != null && client.Connected) + { + try + { client.WriteSingleRegister(address, value); res = true; Console.WriteLine($"鍐欏瘎瀛樺櫒{ip},address={address},value={value},鎴愬姛"); } - catch (Exception ex) { + catch (Exception ex) + { Console.WriteLine(ex.Message); Console.WriteLine($"鍐欏瘎瀛樺櫒{ip},address={address},value={value},澶辫触"); } + } + 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; } @@ -187,41 +222,53 @@ /// <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) { + 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 { + if (client != null && client.Connected) + { + try + { client.WriteMultipleRegisters(address, values); res = true; } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); } } return res; } - private static ModbusClient GetClient(string ip, int port) { + private static ModbusClient GetClient(string ip, int port) + { ModbusClient client = null; - if (!clients.ContainsKey(ip)) { + if (!clients.ContainsKey(ip)) + { client = new ModbusClient(ip, port); - try { + try + { client.Connect(); clients[ip] = client; } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); Console.WriteLine($"杩炴帴plc澶辫触{ip},err=" + ex.Message); } } - else { + else + { client = clients[ip]; - if (!clients[ip].Connected) { - try { + if (!clients[ip].Connected) + { + try + { clients[ip].Connect(); } - catch (Exception ex) { + catch (Exception ex) + { //LogHelper.Error(ex.Message, ex); Console.WriteLine($"杩炴帴plc澶辫触{ip},err=" + ex.Message); } -- Gitblit v1.9.1