using EasyModbus;
using HH.WCS.Mobox3.pinggao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HH.WCS.JunzhouNongfu.device
{
///
/// modbus tcp 用第三方的包
///
internal static class ModbusHelper
{
private static Dictionary clients = new Dictionary();
///
/// 读线圈
///
///
///
///
///
///
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 {
res = client.ReadCoils(address, qty);
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
///
/// 读离散输入
///
///
///
///
///
///
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 {
res = client.ReadDiscreteInputs(address, qty);
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
///
/// 读保持寄存器
///
///
///
///
///
///
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个int类型
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;
}
///
/// 读输入寄存器
///
///
///
///
///
///
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 {
res = client.ReadInputRegisters(address, qty);
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
///
/// 写单个线圈
///
///
///
///
///
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 {
client.WriteSingleCoil(address, value);
res = true;
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
///
/// 写多个线圈
///
///
///
///
///
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 {
client.WriteMultipleCoils(address, values);
res = true;
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
///
/// 写单个寄存器
///
///
///
///
///
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 {
client.WriteSingleRegister(address, value);
res = true;
Console.WriteLine($"写寄存器{ip},address={address},value={value},成功");
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.WriteLine($"写寄存器{ip},address={address},value={value},失败");
}
}
return res;
}
///
/// 写多个寄存器
///
///
///
///
///
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 {
client.WriteMultipleRegisters(address, values);
res = true;
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
}
}
return res;
}
private static ModbusClient GetClient(string ip, int port) {
ModbusClient client = null;
if (!clients.ContainsKey(ip)) {
client = new ModbusClient(ip, port);
try {
client.Connect();
clients[ip] = client;
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
Console.WriteLine($"连接plc失败{ip},err=" + ex.Message);
}
}
else {
client = clients[ip];
if (!clients[ip].Connected) {
try {
clients[ip].Connect();
}
catch (Exception ex) {
//LogHelper.Error(ex.Message, ex);
Console.WriteLine($"连接plc失败{ip},err=" + ex.Message);
}
}
}
return client;
}
}
}