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
|
{
|
/// <summary>
|
/// 西门子S7通讯类
|
/// </summary>
|
[Description("欧姆龙EtherNet/IP(CIP)")]
|
public class OmronEtherNetCommunicator : BaseCommunicator
|
{
|
#region Private Member
|
/// <summary>
|
/// HSLCommunication的西门子的S7协议的通讯类
|
/// </summary>
|
private OmronCipNet plc;
|
|
/// <summary>
|
/// 设备的IP地址。
|
/// </summary>
|
private string _ipAddress;
|
|
/// <summary>
|
/// 连接使用的端口号。
|
/// </summary>
|
private int _port;
|
|
/// <summary>
|
/// 当前通讯器是否已连接到PLC。
|
/// </summary>
|
private bool _connected;
|
|
/// <summary>
|
/// PLC名称
|
/// </summary>
|
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
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="ipAddress">设备的IP地址</param>
|
/// <param name="port">连接使用的端口号</param>
|
/// <param name="name">设备名称</param>
|
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<T>(string address)
|
{
|
throw new NotImplementedException();
|
}
|
|
public override object ReadAsObj(string address, string dataType)
|
{
|
throw new NotImplementedException();
|
}
|
|
public override T ReadCustomer<T>(string address)
|
{
|
throw new NotImplementedException();
|
}
|
|
public override OperateResult<TimeSpan> Wait<T>(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<T>(string address, T value)
|
{
|
throw new NotImplementedException();
|
}
|
|
public override bool WriteCustomer<T>(string address, [NotNull] T value)
|
{
|
throw new NotImplementedException();
|
}
|
|
public override bool WriteObj(string address, string dataType, object value)
|
{
|
throw new NotImplementedException();
|
}
|
#endregion
|
}
|
}
|