| | |
| | | using HslCommunication.LogNet; |
| | | using HslCommunication.Profinet.Siemens; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | |
| | | 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 |
| | |
| | | /// </summary> |
| | | public override string Name => _name; |
| | | |
| | | /// <summary> |
| | | /// PLC读写日志记录 |
| | | /// </summary> |
| | | public override ILogNet LogNet => _logNet; |
| | | #endregion Public Member |
| | | |
| | |
| | | /// </summary> |
| | | /// <param name="ipAddress">设备的IP地址</param> |
| | | /// <param name="port">连接使用的端口号</param> |
| | | /// <param name="name">设备名称</param> |
| | | public SiemensS7(string ipAddress, int port, string name) |
| | | { |
| | | string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}"; |
| | | _logNet = new LogNetFileSize(path, 3 * 1024 * 1024, 100); |
| | | _logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100); |
| | | |
| | | bool ipCheck = IPAddress.TryParse(ipAddress, out IPAddress? address); |
| | | if (!ipCheck) |
| | |
| | | /// <exception cref="CommunicationException"></exception> |
| | | private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull |
| | | { |
| | | if (value is Array) |
| | | { |
| | | return operateResult.IsSuccess; |
| | | } |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | try |
| | | { |
| | |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | T readValue = Read<T>(address); |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value)); |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value)); |
| | | obj = readValue; |
| | | if (readValue.Equals(value)) |
| | | { |
| | |
| | | { |
| | | LogNet.WriteInfo(Name, stringBuilder.ToString()); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | case TypeCode.Char: |
| | | return plc.Write(address, Convert.ToChar(value)); |
| | | default: |
| | | if (value is int[]) |
| | | { |
| | | return plc.Write(address, value as int[]); |
| | | } |
| | | else if (value is uint[]) |
| | | { |
| | | return plc.Write(address, value as uint[]); |
| | | } |
| | | else if (value is short[]) |
| | | { |
| | | return plc.Write(address, value as short[]); |
| | | } |
| | | else if (value is ushort[]) |
| | | { |
| | | return plc.Write(address, value as ushort[]); |
| | | } |
| | | else if (value is bool[]) |
| | | { |
| | | return plc.Write(address, value as bool[]); |
| | | } |
| | | else if (value is float[]) |
| | | { |
| | | return plc.Write(address, value as float[]); |
| | | } |
| | | else if (value is double[]) |
| | | { |
| | | return plc.Write(address, value as double[]); |
| | | } |
| | | else if (value is byte[]) |
| | | { |
| | | return plc.Write(address, value as byte[]); |
| | | } |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private object Read(string address, TypeCode typeCode) |
| | | private object Read(string address, TypeCode typeCode, ushort length = 1) |
| | | { |
| | | try |
| | | { |
| | | switch (typeCode) |
| | | { |
| | | case TypeCode.Int32: |
| | | return (int)GetContent(plc.ReadInt32(address), address); |
| | | if (length > 1) |
| | | return (int[])GetContent(plc.ReadInt32(address, length), address); |
| | | else |
| | | return (int)GetContent(plc.ReadInt32(address), address); |
| | | case TypeCode.UInt32: |
| | | return (uint)GetContent(plc.ReadUInt32(address), address); |
| | | if (length > 1) |
| | | return (uint[])GetContent(plc.ReadUInt32(address, length), address); |
| | | else |
| | | return (uint)GetContent(plc.ReadUInt32(address), address); |
| | | case TypeCode.Int16: |
| | | return (short)GetContent(plc.ReadInt16(address), address); |
| | | if (length > 1) |
| | | return (short[])GetContent(plc.ReadInt16(address, length), address); |
| | | else |
| | | return (short)GetContent(plc.ReadInt16(address), address); |
| | | case TypeCode.UInt16: |
| | | return (ushort)GetContent(plc.ReadUInt16(address), address); |
| | | if (length > 1) |
| | | return (ushort[])GetContent(plc.ReadUInt16(address, length), address); |
| | | else |
| | | return (ushort)GetContent(plc.ReadUInt16(address), address); |
| | | case TypeCode.Single: |
| | | return (float)GetContent(plc.ReadFloat(address), address); |
| | | if (length > 1) |
| | | return (float[])GetContent(plc.ReadFloat(address, length), address); |
| | | else |
| | | return (float)GetContent(plc.ReadFloat(address), address); |
| | | case TypeCode.Boolean: |
| | | return (bool)GetContent(plc.ReadBool(address), address); |
| | | if (length > 1) |
| | | return (bool[])GetContent(plc.ReadBool(address, length), address); |
| | | else |
| | | return (bool)GetContent(plc.ReadBool(address), address); |
| | | case TypeCode.Byte: |
| | | if (length > 1) |
| | | return (byte)GetContent(plc.Read(address, length), address); |
| | | return (byte)GetContent(plc.ReadByte(address), address); |
| | | case TypeCode.String: |
| | | return (string)GetContent(plc.ReadString(address), address); |
| | | if (length > 1) |
| | | return (string)GetContent(plc.ReadString(address, length), address); |
| | | else |
| | | return (string)GetContent(plc.ReadString(address), address); |
| | | case TypeCode.Char: |
| | | if (length > 1) |
| | | return (char)GetContent(plc.Read(address, length), address); |
| | | return (char)GetContent(plc.ReadByte(address), address); |
| | | default: |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError); |
| | |
| | | //读取异常时抛出自定义通讯异常类 |
| | | 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 |
| | | |
| | |
| | | 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) |
| | |
| | | return (T)Read(address, Type.GetTypeCode(type)); |
| | | } |
| | | |
| | | public override T[] Read<T>(string address, ushort length) |
| | | { |
| | | Type type = typeof(T); |
| | | return (T[])Read(address, Type.GetTypeCode(type), length); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 从PLC读取数据返回object。 |
| | | /// </summary> |
| | |
| | | catch (Exception ex) |
| | | { |
| | | //写入异常时抛出自定义通讯异常类 |
| | | throw new CommunicationException($"写入数据异常,地址:【{address}】,错误信息: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex); |
| | | throw new CommunicationException($"写入数据异常,地址:【{address}】,错误信息: {ex.Message}", CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | } |
| | | |
| | |
| | | public override bool Write<T>(string address, T value) |
| | | { |
| | | return GetResult(Write(address, value), address, value); |
| | | } |
| | | |
| | | |
| | | public override bool Write<T>(string address, T[] values) |
| | | { |
| | | return GetResult(Write(address, values), address, values); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | LogNet.WriteException(Name, $"【{Name}】PLC读取异常,地址:【{address}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |
| | |
| | | { |
| | | LogNet.WriteInfo(Name, stringBuilder.ToString()); |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |
| | | // 显式实现IDisposable接口以提供垃圾回收时的清理 |
| | | public void Dispose() |
| | | /// <summary> |
| | | /// 显式实现IDisposable接口以提供垃圾回收时的清理 |
| | | /// </summary> |
| | | public override void Dispose() |
| | | { |
| | | _isPing = false; |
| | | Disconnect(); |
| | | plc.Dispose(); |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 等待指定地址的泛型类型值为指定的值 |
| | | /// </summary> |
| | | /// <typeparam name="T">指定的值的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="readInterval">读取的频率。</param> |
| | | /// <param name="waitTimeout">等待的超时时间,如果超时时间为-1的话,则是无期限等待。</param> |
| | | /// <param name="value">等待检测的值</param> |
| | | /// <returns>是否等待成功的结果对象,一旦通信失败,或是等待超时就返回失败。否则返回成功,并告知调用方等待了多久。</returns> |
| | | public override OperateResult<TimeSpan> Wait<T>(string address, int readInterval, int waitTimeout, T value) |
| | | { |
| | | TypeCode typeCode = Type.GetTypeCode(typeof(T)); |