| | |
| | | using System.Diagnostics.CodeAnalysis; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Net.NetworkInformation; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | |
| | | /// 西门子S7通讯类 |
| | | /// </summary> |
| | | [Description("西门子S7")] |
| | | public class SiemensS7 : BaseCommunicator, IDisposable |
| | | public class SiemensS7 : BaseCommunicator |
| | | { |
| | | #region Private Member |
| | | /// <summary> |
| | |
| | | private string _name; |
| | | |
| | | private ILogNet _logNet; |
| | | |
| | | private bool _isPing = true; |
| | | #endregion Private Member |
| | | |
| | | #region Public Member |
| | |
| | | throw new CommunicationException($"读取数据异常,错误信息:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex); |
| | | } |
| | | } |
| | | |
| | | 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 |
| | |
| | | { |
| | | try |
| | | { |
| | | |
| | | //实例化一个西门子的S7协议的通讯对象 |
| | | plc = new SiemensS7Net(SiemensPLCS.S1500) |
| | | { |
| | |
| | | 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) |
| | |
| | | #endregion |
| | | |
| | | // 显式实现IDisposable接口以提供垃圾回收时的清理 |
| | | public void Dispose() |
| | | public override void Dispose() |
| | | { |
| | | _isPing = false; |
| | | Disconnect(); |
| | | plc.Dispose(); |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | |