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
|
{
|
/// <summary>
|
/// modbus tcp 用第三方的包
|
/// </summary>
|
public class ModbusHelper
|
{
|
public static Dictionary<string, ModbusClient> ipPort_ModbusClient = new Dictionary<string, ModbusClient>();//内存,连接上的mocbus通讯对象
|
|
public ModbusHelper(string ip, int port)
|
{
|
Init(new ModbusConfigModel() { Ip = ip, Port = port });
|
}
|
|
/// <summary>
|
/// modbus启动初始化
|
/// </summary>
|
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)
|
{
|
|
}
|
}
|
|
/// <summary>
|
/// 连接modbus下位机
|
/// </summary>
|
/// <param name="modbusClient"></param>
|
private static void Link(ModbusClient modbusClient)
|
{
|
try
|
{
|
modbusClient.Disconnect();
|
modbusClient.Connect();
|
if (modbusClient.Connected)
|
{
|
//连接上
|
}
|
else
|
{
|
//没连上
|
}
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
|
/// <summary>
|
/// 重连
|
/// </summary>
|
internal static void RestLink()
|
{
|
if (ipPort_ModbusClient.Count > 0)
|
{
|
foreach (var item in ipPort_ModbusClient)
|
{
|
if (!item.Value.Connected)
|
{
|
Link(item.Value);
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 读一个或多个线圈,返回一个bit真假数组
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 写入单个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="value"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
}
|
/// <summary>
|
/// 写入多个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
|
}
|
|
/// <summary>
|
/// 批量读取或单独读取保持寄存器,返回的是32位int数组
|
/// </summary>
|
/// <param name="address">读取起始位</param>
|
/// <param name="qty">读取的数量</param>
|
/// <param name="ip">IP地址</param>
|
/// <param name="port">端口号</param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 写入单个寄存器数据
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="value"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
}
|
|
/// <summary>
|
/// 写入多个寄存器数据
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
}
|
|
/// <summary>
|
/// 读一个或多个离散输入,返回一个bit真假数组
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
|
}
|
|
/// <summary>
|
/// 读一个或多个输入寄存器,返回一个int32位数组
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 获取modbus通讯对象
|
/// </summary>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
private static ModbusClient GetModbusClient(string ip, int port)
|
{
|
if (ipPort_ModbusClient.ContainsKey($"{ip}:{port}"))
|
{
|
return ipPort_ModbusClient[$"{ip}:{port}"];
|
}
|
else
|
{
|
return null;
|
}
|
}
|
|
/// <summary>
|
/// 通过Modbus协议连接下位机时需要的model,应该放在model层,这里我懒了
|
/// </summary>
|
public class ModbusConfigModel
|
{
|
public string Ip { set; get; }//IP地址
|
public int Port { set; get; }//端口号
|
}
|
}
|
}
|