using EasyModbus; using HH.WCS.Mobox3.DoubleCoin; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HH.WCS.JunzhouNongfu.device { /// /// modbus tcp 用第三方的包 /// public class ModbusHelper { public static Dictionary ipPort_ModbusClient = new Dictionary();//内存,连接上的mocbus通讯对象 public ModbusHelper(string ip, int port) { Init(new ModbusConfigModel() { Ip = ip, Port = port }); } /// /// modbus启动初始化 /// public static void Init(ModbusConfigModel modbusConfigModel) { //配置文件读取所有的mocbus进行初始化 try { if (modbusConfigModel == null) { //读取配置信息失败 return; } var modbusClient = new ModbusClient(modbusConfigModel.Ip, modbusConfigModel.Port); ipPort_ModbusClient.Add($"{modbusConfigModel.Ip}:{modbusConfigModel.Port}", modbusClient); Link(modbusClient); } catch (Exception ex) { } } /// /// 连接modbus下位机 /// /// private static void Link(ModbusClient modbusClient) { try { modbusClient.Disconnect(); modbusClient.Connect(); if (modbusClient.Connected) { //连接上 } else { //没连上 } } catch (Exception ex) { } } /// /// 重连 /// internal static void RestLink() { if (ipPort_ModbusClient.Count > 0) { foreach (var item in ipPort_ModbusClient) { if (!item.Value.Connected) { Link(item.Value); } } } } /// /// 读一个或多个线圈,返回一个bit真假数组 /// /// /// /// /// /// public static bool[] ReadCoils(int address, int qty, string ip, int port = 502) { bool[] res = new bool[0]; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { res = client.ReadCoils(address, qty); if (res.Length != 0) { //读取成功 } else { //读取失败 } } catch (Exception ex) { } } else { } return res; } /// /// 写入单个线圈 /// /// /// /// /// public static bool WriteSingleCoil(int address, bool value, string ip, int port = 502) { var res = false; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { client.WriteSingleCoil(address, value); res = value == client.ReadCoils(address, 1)[0]; if (res) { //写入成功 } else { //写入失败 } } catch (Exception ex) { } } else { } return res; } /// /// 写入多个线圈 /// /// /// /// /// public static bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502) { var res = false; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { client.WriteMultipleCoils(address, values); var dataRead = client.ReadCoils(address, values.Length); res = values.SequenceEqual(dataRead); if (res) { //写入成功 } else { //写入失败 } } catch (Exception ex) { } } else { } return res; } /// /// 批量读取或单独读取保持寄存器,返回的是32位int数组 /// /// 读取起始位 /// 读取的数量 /// IP地址 /// 端口号 /// public static int[] ReadHoldingRegisters(int address, int qty, string ip, int port = 502) { int[] res = new int[0]; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { //一个寄存器是16位,返回2个int类型 res = client.ReadHoldingRegisters(address, qty); if (res.Length != 0) { //读取成功 } else { //读取失败 } } catch (Exception ex) { //如果请求数量超出保持寄存器的最大数据行数,会报错 LogHelper.Info($"发生了异常:{ex.Message},IP:{ip},Port:{port}", "Error"); } } else { LogHelper.Info($"未找到Modbus设备实例对象:IP:{ip},Port:{port}"); } return res; } /// /// 写入单个寄存器数据 /// /// /// /// /// public static bool WriteSingleRegister(int address, int value, string ip, int port = 502) { var res = false; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { client.WriteSingleRegister(address, value); res = value == client.ReadHoldingRegisters(address, 1)[0]; if (res) { //写入成功 } else { //写入失败 } } catch (Exception ex) { } } else { } return res; } /// /// 写入多个寄存器数据 /// /// /// /// /// public static bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 502) { var res = false; var client = GetModbusClient(ip, port); var log = string.Join(",", values.Select(x => x.ToString())); if (client != null && client.Connected) { try { client.WriteMultipleRegisters(address, values); var dataRead = client.ReadHoldingRegisters(address, 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; } /// /// 读一个或多个离散输入,返回一个bit真假数组 /// /// /// /// /// /// public static bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502) { bool[] res = new bool[0]; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { res = client.ReadDiscreteInputs(address, qty); if (res.Length != 0) { //读取成功 } else { //读取失败 } } catch (Exception ex) { } } else { } return res; } /// /// 读一个或多个输入寄存器,返回一个int32位数组 /// /// /// /// /// /// public static int[] ReadInputRegisters(int address, int qty, string ip, int port = 502) { int[] res = new int[0]; var client = GetModbusClient(ip, port); if (client != null && client.Connected) { try { res = client.ReadInputRegisters(address, qty); if (res.Length != 0) { //读取成功 } else { //读取失败 } } catch (Exception ex) { } } else { } return res; } /// /// 获取modbus通讯对象 /// /// /// /// private static ModbusClient GetModbusClient(string ip, int port) { if (ipPort_ModbusClient.ContainsKey($"{ip}:{port}")) { return ipPort_ModbusClient[$"{ip}:{port}"]; } else { return null; } } /// /// 通过Modbus协议连接下位机时需要的model,应该放在model层,这里我懒了 /// public class ModbusConfigModel { public string Ip { set; get; }//IP地址 public int Port { set; get; }//端口号 } } }