using EasyModbus;
|
using HH.WCS.Mobox3.FJJT;
|
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>
|
internal static 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 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;
|
|
}
|
/// <summary>
|
/// 读离散输入
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
|
}
|
/// <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, 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;
|
}
|
/// <summary>
|
/// 读输入寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
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;
|
|
}
|
/// <summary>
|
/// 写单个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="value"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
|
}
|
/// <summary>
|
/// 写多个线圈
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
|
}
|
/// <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, 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;
|
}
|
/// <summary>
|
/// 写多个寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="values"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
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;
|
}
|
}
|
}
|