Files
SamplePreSystem-CS/SamplePre.Communication/Communication/ModbusTcpCommunicator.cs
2026-04-30 11:34:41 +08:00

237 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Modbus.Device;
using S7.Net;
using SamplePre.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace SamplePre.Communication.Communication
{
public class ModbusTcpCommunicator : ICommunication, IDisposable
{
IModbusMaster _modbusMaster = null;
TcpClient _tcpClient = null;
private readonly string _ipAddress;
private readonly int _port;
/// <summary>
/// 是否连接
/// </summary>
private bool _isConnected = false;
/// <summary>
/// 是否连接
/// </summary>
public bool IsConnected
{
get => _isConnected;
set
{
if (_isConnected != value)
{
_isConnected = value;
ConnectionStatusChanged?.Invoke(value);
}
}
}
/// <summary>
/// 连接状态变化事件
/// </summary>
public event Action<bool> ConnectionStatusChanged;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ipAddress">IP地址</param>
/// <param name="port">端口号</param>
public ModbusTcpCommunicator(string ipAddress, int port=502)
{
try
{
_ipAddress = ipAddress;
_port = port;
_tcpClient = new TcpClient(ipAddress, port);//Modbus主设备
_modbusMaster = ModbusIpMaster.CreateIp(_tcpClient);
_modbusMaster.Transport.ReadTimeout = 2000;
_modbusMaster.Transport.Retries = 3;
IsConnected = true;
}
catch (Exception ex)
{
LoggerHelper.Logger.Error("初始化ModbusTcpCommunicator失败" + ex.Message);
}
}
/// <summary>
/// 连接PLC
/// </summary>
/// <returns>是否连接成功</returns>
public bool Connect()
{
try
{
if (IsConnected)
{
LoggerHelper.Logger.Info("已经处于连接状态!");
return true;
}
// 如果之前的连接对象已释放,创建新的
if (_modbusMaster == null)
{
_tcpClient = new TcpClient(_ipAddress, _port);//Modbus主设备
_modbusMaster = ModbusIpMaster.CreateIp(_tcpClient);
_modbusMaster.Transport.ReadTimeout = 2000; //
_modbusMaster.Transport.Retries = 3;
}
LoggerHelper.Logger.Info("PLC连接成功");
IsConnected = true;
return IsConnected;
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"连接PLC失败: {ex.Message}");
IsConnected = false;
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
public void Disconnect()
{
try
{
if (IsConnected)
{
_modbusMaster.Dispose();
_tcpClient.Close();
IsConnected = false;
LoggerHelper.Logger.Info("PLC连接已断开");
}
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"断开PLC连接失败: {ex.Message}");
}
}
/// <summary>
/// 写入数组数据
/// </summary>
/// <param name="Dbno"></param>
/// <param name="start"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public bool WriteArraysBtye<T>(int Dbno, int start, T[] data)
{
try
{
if (!IsConnected && !Connect())
{
return false;
}
ushort[] ushorts = data.Cast<ushort>().ToArray();
_modbusMaster.WriteMultipleRegisters((byte)Dbno, (ushort)start, ushorts);
return true;
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"写入数组失败: {ex.Message}");
return false;
}
}
/// <summary>
/// 读取数组
/// </summary>
/// <returns></returns>
public ushort[] ReadArraysShort(int Dbno, int start, int lenth)
{
//try
//{
// if (!IsConnected && !Connect())
// {
// return null;
// }
// //public object? Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount, byte bitAdr = 0)
// ushort[] datas = _plc.Read(DataType.DataBlock, Dbno, start, VarType.Word, lenth) as ushort[];
// return datas;
//}
//catch (Exception ex)
//{
// LoggerHelper.Logger.Info($"读取数组失败: {ex.Message}");
// return null;
//}
return null;
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 释放托管资源
Disconnect();
}
public bool WriteSingleData(string address, object value)
{
throw new NotImplementedException();
}
~ModbusTcpCommunicator()
{
Dispose();
}
}
}