using EasyModbus; using HH.WCS.ZhongCeJinTan.util; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using static HH.WCS.ZhongCeJinTan.device.TcpServer; namespace HH.WCS.ZhongCeJinTan.device { internal class ModbusHelper { private static Dictionary clients = new Dictionary(); /// /// 读线圈 /// /// /// /// /// /// internal bool[] ReadCoils(int address, int qty, string ip, int port = 502) { bool[] res = new bool[0]; LogHelper.Info($"ReadCoils:SendMsg:Addr:{address},Qty:{qty},Ip:{ip},Port:{port}", "ModBus"); var client = GetClient(ip, port); if (client != null && client.Connected) { client.ConnectionTimeout = 3500; try { res = client.ReadCoils(address, qty); } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } } LogHelper.Info($"ReadCoils:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Ip:{ip},Port:{port}", "ModBus"); return res; } /// /// 读离散输入 /// /// /// /// /// /// internal 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; } static object ReadHoldingRegistersLock = new object(); /// /// 读保持寄存器 /// /// /// /// /// /// internal static int[] ReadHoldingRegisters(int address, int qty) { int[] res = new int[0]; lock (ReadHoldingRegistersLock) { LogHelper.Info($"ReadHoldingRegisters:SendMsg:Addr:{address},Qty:{qty},Ip:{TmIp},Port:{TmPort}", "ModBus"); var client = GetClient(TmIp, TmPort); if (client != null && client.Connected) { client.ConnectionTimeout = 5000; try { res = client.ReadHoldingRegisters(address, qty); client.Disconnect(); } catch (Exception ex) { //LogHelper.Error(ex.Message, ex); LogHelper.Info($"ReadHoldingRegisters:Error:{ex.Message}"); } } LogHelper.Info($"ReadHoldingRegisters:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Ip:{TmIp},Port:{TmPort}", "ModBus"); } return res; } /// /// 读输入寄存器 /// /// /// /// /// /// internal 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; } /// /// 写单个线圈 /// /// /// /// /// internal 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; } /// /// 写多个线圈 /// /// /// /// /// internal bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502) { var res = false; LogHelper.Info($"WriteMultipleCoils:SendMsg:Addr:{address},Values:{values},Ip:{ip},Port:{port}","ModBus"); var client = GetClient(ip, port); if (client != null && client.Connected) { client.ConnectionTimeout = 3500; try { client.WriteMultipleCoils(address, values); res = true; } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } } LogHelper.Info($"WriteMultipleCoils:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Values:{values},Ip:{ip},Port:{port}", "ModBus"); return res; } static string TmIp = Settings.TmDeviceIp; static int TmPort = Settings.TmDevicePort; /// /// 写单个寄存器 /// /// /// /// /// internal static bool WriteSingleRegister(int address, int value) { var res = false; var client = GetClient(TmIp, TmPort); if (client != null && client.Connected) { try { client.WriteSingleRegister(address, value); res = true; } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } } return res; } /// /// 写多个寄存器 /// /// /// /// /// internal bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 503) { 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; } static ModbusClient GetClient(string ip, int port) { ModbusClient client = null; LogHelper.Info($"GetClient:ip:{ip},port:{port}","ModBus"); if (!clients.ContainsKey(ip)) { LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接不存在,开始链接", "ModBus"); client = new ModbusClient(ip, port); try { client.Connect(); //clients[ip] = client; LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接成功", "ModBus"); } catch (Exception ex) { LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接异常:{ex.Message}", "ModBus"); LogHelper.Error(ex.Message, ex); } } return client; } } }