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; /// /// 是否连接 /// private bool _isConnected = false; /// /// 是否连接 /// public bool IsConnected { get => _isConnected; set { if (_isConnected != value) { _isConnected = value; ConnectionStatusChanged?.Invoke(value); } } } /// /// 连接状态变化事件 /// public event Action ConnectionStatusChanged; /// /// 构造函数 /// /// IP地址 /// 端口号 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); } } /// /// 连接PLC /// /// 是否连接成功 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; } } /// /// 断开连接 /// 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}"); } } /// /// 写入数组数据 /// /// /// /// /// public bool WriteArraysBtye(int Dbno, int start, T[] data) { try { if (!IsConnected && !Connect()) { return false; } ushort[] ushorts = data.Cast().ToArray(); _modbusMaster.WriteMultipleRegisters((byte)Dbno, (ushort)start, ushorts); return true; } catch (Exception ex) { LoggerHelper.Logger.Info($"写入数组失败: {ex.Message}"); return false; } } /// /// 读取数组 /// /// 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; } /// /// 释放资源 /// public void Dispose() { // 释放托管资源 Disconnect(); } public bool WriteSingleData(string address, object value) { throw new NotImplementedException(); } ~ModbusTcpCommunicator() { Dispose(); } } }