using HslCommunication; using HslCommunication.LogNet; using HslCommunication.Profinet.Omron; using HslCommunication.Profinet.Siemens; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; namespace WIDESEAWCS_Communicator { /// /// 西门子S7通讯类 /// [Description("欧姆龙EtherNet/IP(CIP)")] public class OmronEtherNetCommunicator : BaseCommunicator { #region Private Member /// /// HSLCommunication的西门子的S7协议的通讯类 /// private OmronCipNet plc; /// /// 设备的IP地址。 /// private string _ipAddress; /// /// 连接使用的端口号。 /// private int _port; /// /// 当前通讯器是否已连接到PLC。 /// private bool _connected; /// /// PLC名称 /// private string _name; private ILogNet _logNet; private bool _isPing = true; #endregion Private Member #region Public Member public override ILogNet LogNet => _logNet; public override string Name => _name; public override bool IsConnected => _connected; #endregion Public Member #region Constructor Function /// /// 构造函数 /// /// 设备的IP地址 /// 连接使用的端口号 /// 设备名称 public OmronEtherNetCommunicator(string ipAddress, int port, string name) { string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}"; _logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100); bool ipCheck = IPAddress.TryParse(ipAddress, out IPAddress? address); if (!ipCheck) { _logNet.WriteError(name, string.Format(CommunicationExceptionMessage.IpAddressErrorException, ipAddress)); throw new CommunicationException(string.Format(CommunicationExceptionMessage.IpAddressErrorException, ipAddress), CommunicationErrorType.IpAddressError); } _ipAddress = ipAddress;//通过构造函数赋值设备的IP地址 _port = port;//通过构造函数赋值连接使用的端口号 _name = name; } #endregion #region Private Method private void Ping() { Task.Run(() => { while (_isPing) { try { IPStatus status = plc.IpAddressPing(); if (status == IPStatus.Success) _connected = true; else _connected = false; } finally { Thread.Sleep(100); } } }); } #endregion #region Public Method public override bool Connect() { try { //实例化一个西门子的S7协议的通讯对象 plc = new OmronCipNet() { IpAddress = _ipAddress, Port = _port }; OperateResult operateResult = plc.ConnectServer();//连接PLC _connected = operateResult.IsSuccess;//将连接是否成功赋值给当前通讯器是否已连接到PLC if (_connected) LogNet.WriteInfo(Name, string.Format(CommunicationInfoMessage.ConnectSuccess, _ipAddress, _port)); else LogNet.WriteError(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, operateResult.Message)); Ping(); return operateResult.IsSuccess; } catch (Exception ex) { LogNet.WriteException(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, ex.Message), ex); //连接异常时抛出自定义异常类 throw new CommunicationException(ex.Message, CommunicationErrorType.ConnectionFailed, innerException: ex); } } public override bool Disconnect() { throw new NotImplementedException(); } public override void Dispose() { throw new NotImplementedException(); } public override byte[] Read(string address, int length) { throw new NotImplementedException(); } public override T Read(string address) { throw new NotImplementedException(); } public override object ReadAsObj(string address, string dataType) { throw new NotImplementedException(); } public override T ReadCustomer(string address) { throw new NotImplementedException(); } public override OperateResult Wait(string address, int readInterval, int waitTimeout, T value) { throw new NotImplementedException(); } public override bool Write(string address, byte[] data) { throw new NotImplementedException(); } public override bool Write(string address, T value) { throw new NotImplementedException(); } public override bool WriteCustomer(string address, [NotNull] T value) { throw new NotImplementedException(); } public override bool WriteObj(string address, string dataType, object value) { throw new NotImplementedException(); } #endregion } }