¶Ô±ÈÐÂÎļþ |
| | |
| | | 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 |
| | | } |
| | | } |