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<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 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;
|
}
|
/// <summary>
|
/// 读离散输入
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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();
|
/// <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) {
|
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;
|
}
|
/// <summary>
|
/// 读输入寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
|
}
|
/// <summary>
|
/// 写单个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="value"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
|
}
|
/// <summary>
|
/// 写多个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
|
|
/// <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) {
|
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;
|
}
|
/// <summary>
|
/// 写多个寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
}
|
}
|
|
|
}
|