From 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0 Mon Sep 17 00:00:00 2001 From: z8018 <1282578289@qq.com> Date: 星期三, 12 三月 2025 14:11:33 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs | 624 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 612 insertions(+), 12 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs index 324bd0a..ba73faa 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs @@ -1,7 +1,10 @@ 锘縰sing HslCommunication; +using HslCommunication.Core; using HslCommunication.LogNet; using HslCommunication.Profinet.Omron; using HslCommunication.Profinet.Siemens; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; @@ -9,13 +12,14 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; +using System.Reflection; using System.Text; using System.Threading.Tasks; namespace WIDESEAWCS_Communicator { /// <summary> - /// 瑗块棬瀛怱7閫氳绫� + /// 娆у榫橢therNet/IP(CIP) /// </summary> [Description("娆у榫橢therNet/IP(CIP)")] public class OmronEtherNetCommunicator : BaseCommunicator @@ -52,11 +56,25 @@ #endregion Private Member #region Public Member + /// <summary> + /// 鏃ュ織璁板綍瀹炰緥瀵硅薄 + /// </summary> public override ILogNet LogNet => _logNet; + /// <summary> + /// 璁惧鍚嶇О + /// </summary> public override string Name => _name; + /// <summary> + /// 鑾峰彇褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆� + /// </summary> public override bool IsConnected => _connected; + + /// <summary> + /// 鏄惁鍦ㄥ啓鍏ユ暟鎹悗璇诲彇鏁版嵁纭銆� + /// </summary> + public override bool IsReadAfterWrite { get; set; } = true; #endregion Public Member @@ -86,6 +104,228 @@ #endregion #region Private Method + /// <summary> + /// 浠嶰perateResult瀵硅薄涓幏鍙栬鍙栫殑鏁版嵁銆� + /// </summary> + /// <typeparam name="T">璇诲彇鐨勬暟鎹被鍨嬨��</typeparam> + /// <param name="operateResult">HSLCommunication璇诲彇鐨凮perateResult<T>瀵硅薄</param> + /// <returns>濡傛灉璇诲彇鎴愬姛锛岃繑鍥炶鍙栫粨鏋滐紝璇诲彇澶辫触锛屾姏鍑鸿嚜瀹氫箟閫氳寮傚父</returns> + /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception> + private object GetContent<T>(OperateResult<T> operateResult, string address) + { + try + { + if (!operateResult.IsSuccess) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); + } + return operateResult.Content ?? throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadDataIsNull, address), CommunicationErrorType.ReadFailed); + } + catch (Exception ex) + { + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.ReadFailed, innerException: ex); + } + } + + /// <summary> + /// + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="operateResult"></param> + /// <param name="address"></param> + /// <param name="value"></param> + /// <returns></returns> + /// <exception cref="CommunicationException"></exception> + private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull + { + StringBuilder stringBuilder = new StringBuilder(); + try + { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, value)); + if (!operateResult.IsSuccess) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); + } + else + { + if (IsReadAfterWrite) + { + object? obj = null; + for (int i = 0; i < 5; i++) + { + T readValue = Read<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value)); + obj = readValue; + if (readValue.Equals(value)) + { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue)); + return true; + } + else if (i < 4) + { + Write(address, value); + } + } + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); + throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); + } + else + { + return true; + } + } + } + catch (Exception ex) + { + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); + } + finally + { + LogNet.WriteInfo(Name, stringBuilder.ToString()); + } + + } + + /// <summary> + /// 鍐欏叆鏁版嵁 + /// </summary> + /// <param name="address"></param> + /// <param name="value"></param> + /// <returns></returns> + /// <exception cref="CommunicationException"></exception> + private OperateResult Write(string address, object value) + { + try + { + Type type = value.GetType(); + + switch (Type.GetTypeCode(type)) + { + case TypeCode.Int32: + return plc.Write(address, Convert.ToInt32(value)); + case TypeCode.UInt32: + return plc.Write(address, Convert.ToUInt32(value)); + case TypeCode.Int16: + return plc.Write(address, Convert.ToInt16(value)); + case TypeCode.UInt16: + return plc.Write(address, Convert.ToUInt16(value)); + case TypeCode.Single: + return plc.Write(address, Convert.ToSingle(value)); + case TypeCode.Boolean: + return plc.Write(address, Convert.ToBoolean(value)); + case TypeCode.Byte: + return plc.Write(address, Convert.ToByte(value)); + case TypeCode.String: + return plc.Write(address, Convert.ToString(value)); + 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); + } + } + catch (CommunicationException ex) + { + throw new CommunicationException(ex.Message, ex.ErrorType); + } + catch (Exception ex) + { + //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� + throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, address, value), CommunicationErrorType.TypeError, innerException: ex); + } + } + + private object Read(string address, TypeCode typeCode, ushort length = 1) + { + try + { + switch (typeCode) + { + case TypeCode.Int32: + if (length > 1) + return (int)GetContent(plc.ReadInt32(address, length), address); + return (int)GetContent(plc.ReadInt32(address), address); + case TypeCode.UInt32: + if (length > 1) + return (uint)GetContent(plc.ReadUInt32(address, length), address); + return (uint)GetContent(plc.ReadUInt32(address), address); + case TypeCode.Int16: + if (length > 1) + return (short)GetContent(plc.ReadInt16(address, length), address); + return (short)GetContent(plc.ReadInt16(address), address); + case TypeCode.UInt16: + if (length > 1) + return (ushort)GetContent(plc.ReadUInt16(address, length), address); + return (ushort)GetContent(plc.ReadUInt16(address), address); + case TypeCode.Single: + if (length > 1) + return (float)GetContent(plc.ReadFloat(address, length), address); + return (float)GetContent(plc.ReadFloat(address), address); + case TypeCode.Boolean: + if (length > 1) + return (bool)GetContent(plc.ReadBool(address, length), address); + 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: + if (length > 1) + return (string)GetContent(plc.ReadString(address, length), address); + 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); + } + } + catch (CommunicationException ex) + { + //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� + throw new CommunicationException(ex.Message, ex.ErrorType); + } + catch (Exception ex) + { + //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� + throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex); + } + } + private void Ping() { Task.Run(() => @@ -111,6 +351,10 @@ #endregion #region Public Method + /// <summary> + /// 杩炴帴鍒癙LC銆� + /// </summary> + /// <returns>濡傛灉杩炴帴鎴愬姛鍒欒繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse銆�</returns> public override bool Connect() { try @@ -139,59 +383,415 @@ } } + /// <summary> + /// 鏂紑涓庡伐涓氳澶囩殑杩炴帴銆� + /// </summary> + /// <returns>濡傛灉鎴愬姛鏂紑杩炴帴鍒欒繑鍥瀟rue锛屽鏋滃凡缁忔槸鏂紑鐘舵�佸垯杩斿洖false銆�</returns> public override bool Disconnect() { - throw new NotImplementedException(); + try + { + if (plc != null) + { + OperateResult operateResult = plc.ConnectClose();//鏂紑涓嶱LC鐨勮繛鎺� + return operateResult.IsSuccess; + } + return false; + } + catch (Exception ex) + { + return false; + } + finally + { + _connected = false; + } } + /// <summary> + /// 閲婃斁瀵硅薄璧勬簮鐨勬帴鍙c�� + /// </summary> public override void Dispose() { - throw new NotImplementedException(); + _isPing = false; + Disconnect(); + plc.Dispose(); + GC.SuppressFinalize(this); } + /// <summary> + /// 浠嶱LC璇诲彇鏁版嵁銆� + /// </summary> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="length">瑕佽鍙栫殑鏁版嵁闀垮害銆�</param> + /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null鎴栫┖鏁扮粍銆�</returns> public override byte[] Read(string address, int length) { - throw new NotImplementedException(); + return (byte[])GetContent(plc.Read(address, (ushort)length), address); } + /// <summary> + /// 浠嶱LC璇诲彇鏁版嵁銆� + /// </summary> + /// <typeparam name="T">璇诲彇鏁版嵁鐨勭被鍨嬫硾鍨嬨��</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null鎴栨姏鍑哄紓甯搞��</returns> public override T Read<T>(string address) { - throw new NotImplementedException(); + Type type = typeof(T); + return (T)Read(address, Type.GetTypeCode(type)); } + /// <summary> + /// 浠嶱LC璇诲彇鏁版嵁銆� + /// </summary> + /// <typeparam name="T">璇诲彇鏁版嵁鐨勭被鍨嬫硾鍨嬨��</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="length">璇诲彇鐨勯暱搴︺��</param> + /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null鎴栨姏鍑哄紓甯搞��</returns> + public override T[] Read<T>(string address, ushort length) + { + Type type = typeof(T); + return (T[])Read(address, Type.GetTypeCode(type), length); + } + + /// <summary> + /// 浠嶱LC璇诲彇鏁版嵁銆� + /// </summary> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="dataType">鏁版嵁绫诲瀷銆�</param> + /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null鎴栨姏鍑哄紓甯搞��</returns> public override object ReadAsObj(string address, string dataType) { - throw new NotImplementedException(); + return Read(address, SiemensDBDataType.GetTypeCode(dataType)); } + /// <summary> + /// 璇诲彇鑷畾涔夌殑鏁版嵁绫诲瀷锛岄渶瑕佺户鎵胯嚜IDataTransfer鎺ュ彛锛岃繑鍥炰竴涓柊鐨勭被鍨嬬殑瀹炰緥瀵硅薄銆� + /// </summary> + /// <typeparam name="T">鑷畾涔夌殑鏁版嵁绫诲瀷娉涘瀷銆�</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <returns>鎴愬姛杩斿洖鑷畾涔夌被鍨嬫暟鎹紝澶辫触鎶涘嚭寮傚父銆�</returns> public override T ReadCustomer<T>(string address) { - throw new NotImplementedException(); + try + { + return plc.ReadCustomer<T>(address).Content; + } + catch (Exception ex) + { + LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); + } } + /// <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) { - throw new NotImplementedException(); + TypeCode typeCode = Type.GetTypeCode(typeof(T)); + switch (typeCode) + { + case TypeCode.Byte: + DateTime start = DateTime.Now; + while (true) + { + OperateResult<byte> read = plc.ReadByte(address); + if (!read.IsSuccess) return OperateResult.CreateFailedResult<TimeSpan>(read); + + if (read.Content == Convert.ToByte(value)) return OperateResult.CreateSuccessResult(DateTime.Now - start); + if (waitTimeout > 0 && (DateTime.Now - start).TotalMilliseconds > waitTimeout) + { + return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout); + } + HslHelper.ThreadSleep(readInterval); + } + case TypeCode.Int16: + OperateResult<TimeSpan> operateResultShort = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout); + return operateResultShort; + case TypeCode.Int32: + OperateResult<TimeSpan> operateResultInt = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout); + return operateResultInt; + case TypeCode.UInt16: + OperateResult<TimeSpan> operateResultUShort = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout); + return operateResultUShort; + case TypeCode.UInt32: + OperateResult<TimeSpan> operateResultUInt = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout); + return operateResultUInt; + default: + throw new NotSupportedException(); + } } + /// <summary> + /// 鍚慞LC鍐欏叆鏁版嵁銆� + /// </summary> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="data">瑕佸啓鍏ョ殑鏁版嵁銆�</param> + /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽惁鍒欐姏鍑哄紓甯搞��</returns> public override bool Write(string address, byte[] data) { - throw new NotImplementedException(); + try + { + OperateResult result = plc.Write(address, data); + if (result.IsSuccess) + { + return result.IsSuccess; + } + else + { + //todo 鍐欏叆澶辫触 + return false; + } + } + catch (Exception ex) + { + //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� + throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex); + } } + /// <summary> + /// 鍚慞LC鍐欏叆鏁版嵁銆� + /// </summary> + /// <typeparam name="T">鍐欏叆鏁版嵁鐨勭被鍨嬫硾鍨嬨��</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="value">瑕佸啓鍏ョ殑鏁版嵁銆�</param> + /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽惁鍒欐姏鍑哄紓甯搞��</returns> public override bool Write<T>(string address, T value) { - throw new NotImplementedException(); + return GetResult(Write(address, value), address, value); } + /// <summary> + /// 鍚慞LC鍐欏叆鏁版嵁銆� + /// </summary> + /// <typeparam name="T">鍐欏叆鏁版嵁鐨勭被鍨嬫硾鍨嬨��</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="values">瑕佸啓鍏ョ殑鏁版嵁銆�</param> + /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽惁鍒欐姏鍑哄紓甯搞��</returns> + public override bool Write<T>(string address, T[] values) + { + return GetResult(Write(address, values), address, values); + } + + /// <summary> + /// 鍐欏叆鑷畾涔夌被鍨嬬殑鏁版嵁锛岃绫诲瀷蹇呴』缁ф壙鑷狪DataTransfer鎺ュ彛銆� + /// </summary> + /// <typeparam name="T">鑷畾涔夌殑鏁版嵁绫诲瀷娉涘瀷銆�</typeparam> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="value">瑕佸啓鍏ユ暟鎹��</param> + /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽け璐ュ垯鎶涘嚭寮傚父銆�</returns> public override bool WriteCustomer<T>(string address, [NotNull] T value) { - throw new NotImplementedException(); + StringBuilder stringBuilder = new StringBuilder(); + try + { + OperateResult operateResult = plc.WriteCustomer(address, value); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value))); + if (operateResult.IsSuccess) + { + if (IsReadAfterWrite) + { + object? obj = null; + for (int i = 0; i < 5; i++) + { + T readValue = ReadCustomer<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, JsonConvert.SerializeObject(readValue))); + obj = readValue; + PropertyInfo[] propertyInfos = typeof(T).GetProperties(); + for (int j = 0; j < propertyInfos.Length; j++) + { + object? writeValueItem = propertyInfos[j].GetValue(value); + object? readValueItem = propertyInfos[j].GetValue(readValue); + if (writeValueItem.Equals(readValueItem)) + { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); + } + else + { + break; + } + if (j == propertyInfos.Length - 1) + return true; + } + + plc.WriteCustomer(address, value); + } + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj))); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj)), CommunicationErrorType.WriteFailed); + } + else + { + return true; + } + } + else + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed); + } + } + catch (Exception ex) + { + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); + } + finally + { + LogNet.WriteInfo(Name, stringBuilder.ToString()); + } } + /// <summary> + /// 鍚慞LC鍐欏叆鏁版嵁銆� + /// </summary> + /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param> + /// <param name="dataType">瑕佸啓鍏ョ殑鏁版嵁绫诲瀷銆�</param> + /// <param name="value">瑕佸啓鍏ョ殑鏁版嵁銆�</param> + /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽け璐ュ垯鎶涘嚭寮傚父銆�</returns> + /// <exception cref="CommunicationException"></exception> public override bool WriteObj(string address, string dataType, object value) { - throw new NotImplementedException(); + bool obj = false; + switch (dataType.ToLower()) + { + case SiemensDBDataType.DataType_DInt: + { + int writeVal; + try + { + writeVal = Convert.ToInt32(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_DW: + { + uint writeVal; + try + { + writeVal = Convert.ToUInt32(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Int: + { + short writeVal; + try + { + writeVal = Convert.ToInt16(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_W: + { + ushort writeVal; + try + { + writeVal = Convert.ToUInt16(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Float: + { + float writeVal; + try + { + writeVal = Convert.ToSingle(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Bool: + { + bool writeVal; + try + { + writeVal = Convert.ToBoolean(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Byte: + { + byte writeVal; + try + { + writeVal = Convert.ToByte(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_String: + { + string writeVal; + try + { + writeVal = value.ToString(); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + + break; + case SiemensDBDataType.DataType_Char: + + break; + default: + throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, dataType, address), CommunicationErrorType.TypeError); + } + return obj; + } + #endregion + + #region Destruction Function + /// <summary> + /// 鏋愭瀯鍑芥暟锛岀‘淇濆湪涓嶅啀闇�瑕佹椂鍏抽棴杩炴帴 + /// </summary> + ~OmronEtherNetCommunicator() + { + Dispose(); } #endregion } -- Gitblit v1.9.3