using EasyModbus;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace HH.WCS.NongFuChaYuan.DeviceService
|
{
|
internal class ModbusHelper
|
{
|
private 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];
|
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 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 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 {
|
res = client.ReadHoldingRegisters(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 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;
|
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 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;
|
}
|
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 = 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 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);
|
}
|
}
|
else {
|
if (clients[ip].Connected) {
|
try {
|
clients[ip].Connect();
|
client = clients[ip];
|
}
|
catch (Exception ex) {
|
LogHelper.Error(ex.Message, ex);
|
}
|
}
|
else {
|
client = clients[ip];
|
}
|
}
|
return client;
|
}
|
}
|
|
public class EasyModbusHelper
|
{
|
private static ModbusHelper ModbusHelper = new ModbusHelper();
|
public static void WriteOrRead()
|
{
|
//ModbusClient tcp = new ModbusClient("", 502);//TCP模式连接
|
//ModbusClient rtu = new ModbusClient("COM1");//RTU模式连接
|
//rtu.UnitIdentifier = 1;//设备ID
|
//rtu.Baudrate = 9600;//波特率
|
//rtu.Parity = System.IO.Ports.Parity.None;//奇偶校验位
|
////rtu.StopBits = System.IO.Ports.StopBits.One;//停止位
|
//rtu.ConnectionTimeout = 500;
|
//rtu.Connect();
|
|
int[] readHoldingRegisters = ModbusHelper.ReadHoldingRegisters(0, 2, "127.0.0.1");
|
//Console.WriteLine($"read:{JsonConvert.SerializeObject(readHoldingRegisters)}");
|
ModbusHelper.WriteMultipleRegisters(2, new int[1] { 9 }, "127.0.0.1");
|
}
|
}
|
}
|