From f142e86b68f5480f342442b1a4ae4c6a4fc3d912 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期二, 17 十二月 2024 09:08:12 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 134 +++++++++++++++++++++++++++++++++++++++----- 1 files changed, 117 insertions(+), 17 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs index e504bdc..2f1436a 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs @@ -20,6 +20,7 @@ using HslCommunication.LogNet; using HslCommunication.Profinet.Siemens; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; @@ -27,6 +28,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net; +using System.Net.NetworkInformation; using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -38,7 +40,7 @@ /// 瑗块棬瀛怱7閫氳绫� /// </summary> [Description("瑗块棬瀛怱7")] - public class SiemensS7 : BaseCommunicator, IDisposable + public class SiemensS7 : BaseCommunicator { #region Private Member /// <summary> @@ -67,6 +69,8 @@ private string _name; private ILogNet _logNet; + + private bool _isPing = true; #endregion Private Member #region Public Member @@ -89,10 +93,11 @@ /// </summary> /// <param name="ipAddress">璁惧鐨処P鍦板潃</param> /// <param name="port">杩炴帴浣跨敤鐨勭鍙e彿</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) @@ -143,6 +148,10 @@ /// <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 { @@ -157,7 +166,7 @@ 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)) { @@ -182,7 +191,6 @@ { LogNet.WriteInfo(Name, stringBuilder.ToString()); } - } /// <summary> @@ -219,6 +227,38 @@ 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); } } @@ -233,29 +273,54 @@ } } - 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); @@ -271,6 +336,29 @@ //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� 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 @@ -297,7 +385,7 @@ 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) @@ -360,6 +448,12 @@ 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> /// 浠嶱LC璇诲彇鏁版嵁杩斿洖object銆� /// </summary> @@ -399,7 +493,7 @@ catch (Exception ex) { //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex); + throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.WriteFailed, innerException: ex); } } @@ -414,6 +508,12 @@ 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> @@ -564,7 +664,6 @@ LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); } - } #endregion @@ -620,14 +719,15 @@ { LogNet.WriteInfo(Name, stringBuilder.ToString()); } - } #endregion // 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞� - public void Dispose() + public override void Dispose() { + _isPing = false; Disconnect(); + plc.Dispose(); GC.SuppressFinalize(this); } -- Gitblit v1.9.3