public static string LeerBulkPLC(IReadWriteNet readWrite, string Variable, ushort Cantidad) { try { OperateResult <byte[]> operateResult = readWrite.Read(Variable, Cantidad); if (!operateResult.IsSuccess) { return(""); } char[] chArray = Encoding.ASCII.GetString(operateResult.Content).ToCharArray(); string str = ""; int index = 2; while (index < chArray.Length) { str = index != 0 ? str + chArray[index + 1].ToString() + chArray[index].ToString() : chArray[index].ToString(); index += 2; } return(str); } catch (Exception ex) { return(""); } }
/// <summary> /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="HslDeviceAddressAttribute"/>,详细参考论坛的操作说明。 /// </summary> /// <typeparam name="T">自定义的数据类型对象</typeparam> /// <param name="readWrite">读写接口的实现</param> /// <returns>包含是否成功的结果对象</returns> public static OperateResult <T> Read <T>(IReadWriteNet readWrite) where T : class, new() { var type = typeof(T); // var constrcuor = type.GetConstructors( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic ); var obj = type.Assembly.CreateInstance(type.FullName); var properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var property in properties) { var attribute = property.GetCustomAttributes(typeof(HslDeviceAddressAttribute), false); if (attribute == null) { continue; } HslDeviceAddressAttribute hslAttribute = null; for (int i = 0; i < attribute.Length; i++) { HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i]; if (tmp.deviceType != null && tmp.deviceType == readWrite.GetType( )) { hslAttribute = tmp; break; } } if (hslAttribute == null) { for (int i = 0; i < attribute.Length; i++) { HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i]; if (tmp.deviceType == null) { hslAttribute = tmp; break; } } } if (hslAttribute == null) { continue; } Type propertyType = property.PropertyType; if (propertyType == typeof(short)) { OperateResult <short> valueResult = readWrite.ReadInt16(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(short[])) { OperateResult <short[]> valueResult = readWrite.ReadInt16(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(ushort)) { OperateResult <ushort> valueResult = readWrite.ReadUInt16(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(ushort[])) { OperateResult <ushort[]> valueResult = readWrite.ReadUInt16(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(int)) { OperateResult <int> valueResult = readWrite.ReadInt32(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(int[])) { OperateResult <int[]> valueResult = readWrite.ReadInt32(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(uint)) { OperateResult <uint> valueResult = readWrite.ReadUInt32(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(uint[])) { OperateResult <uint[]> valueResult = readWrite.ReadUInt32(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(long)) { OperateResult <long> valueResult = readWrite.ReadInt64(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(long[])) { OperateResult <long[]> valueResult = readWrite.ReadInt64(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(ulong)) { OperateResult <ulong> valueResult = readWrite.ReadUInt64(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(ulong[])) { OperateResult <ulong[]> valueResult = readWrite.ReadUInt64(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(float)) { OperateResult <float> valueResult = readWrite.ReadFloat(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(float[])) { OperateResult <float[]> valueResult = readWrite.ReadFloat(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(double)) { OperateResult <double> valueResult = readWrite.ReadDouble(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(double[])) { OperateResult <double[]> valueResult = readWrite.ReadDouble(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(string)) { OperateResult <string> valueResult = readWrite.ReadString(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(byte[])) { OperateResult <byte[]> valueResult = readWrite.Read(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(bool)) { OperateResult <bool> valueResult = readWrite.ReadBool(hslAttribute.address); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } else if (propertyType == typeof(bool[])) { OperateResult <bool[]> valueResult = readWrite.ReadBool(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1)); if (!valueResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(valueResult)); } property.SetValue(obj, valueResult.Content, null); } } return(OperateResult.CreateSuccessResult((T)obj)); }
/// <summary> /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="T:ProcessControlService.CommunicationStandard.HslDeviceAddressAttribute" />,详细参考论坛的操作说明。 /// </summary> /// <typeparam name="T">自定义的数据类型对象</typeparam> /// <param name="readWrite">读写接口的实现</param> /// <returns>包含是否成功的结果对象</returns> public static OperateResult <T> Read <T>(IReadWriteNet readWrite) where T : class, new() { Type typeFromHandle = typeof(T); object obj = typeFromHandle.Assembly.CreateInstance(typeFromHandle.FullName); PropertyInfo[] properties = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public); PropertyInfo[] array = properties; foreach (PropertyInfo propertyInfo in array) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(DeviceAddressAttribute), inherit: false); if (customAttributes == null) { continue; } DeviceAddressAttribute hslDeviceAddressAttribute = null; for (int j = 0; j < customAttributes.Length; j++) { DeviceAddressAttribute hslDeviceAddressAttribute2 = (DeviceAddressAttribute)customAttributes[j]; if (hslDeviceAddressAttribute2.DeviceType != null && hslDeviceAddressAttribute2.DeviceType == readWrite.GetType()) { hslDeviceAddressAttribute = hslDeviceAddressAttribute2; break; } } if (hslDeviceAddressAttribute == null) { for (int k = 0; k < customAttributes.Length; k++) { DeviceAddressAttribute hslDeviceAddressAttribute3 = (DeviceAddressAttribute)customAttributes[k]; if (hslDeviceAddressAttribute3.DeviceType == null) { hslDeviceAddressAttribute = hslDeviceAddressAttribute3; break; } } } if (hslDeviceAddressAttribute == null) { continue; } Type propertyType = propertyInfo.PropertyType; if (propertyType == typeof(short)) { OperateResult <short> operateResult = readWrite.ReadInt16(hslDeviceAddressAttribute.Address); if (!operateResult.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult)); } propertyInfo.SetValue(obj, operateResult.Content, null); } else if (propertyType == typeof(short[])) { OperateResult <short[]> operateResult2 = readWrite.ReadInt16(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult2.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult2)); } propertyInfo.SetValue(obj, operateResult2.Content, null); } else if (propertyType == typeof(ushort)) { OperateResult <ushort> operateResult3 = readWrite.ReadUInt16(hslDeviceAddressAttribute.Address); if (!operateResult3.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult3)); } propertyInfo.SetValue(obj, operateResult3.Content, null); } else if (propertyType == typeof(ushort[])) { OperateResult <ushort[]> operateResult4 = readWrite.ReadUInt16(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult4.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult4)); } propertyInfo.SetValue(obj, operateResult4.Content, null); } else if (propertyType == typeof(int)) { OperateResult <int> operateResult5 = readWrite.ReadInt32(hslDeviceAddressAttribute.Address); if (!operateResult5.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult5)); } propertyInfo.SetValue(obj, operateResult5.Content, null); } else if (propertyType == typeof(int[])) { OperateResult <int[]> operateResult6 = readWrite.ReadInt32(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult6.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult6)); } propertyInfo.SetValue(obj, operateResult6.Content, null); } else if (propertyType == typeof(uint)) { OperateResult <uint> operateResult7 = readWrite.ReadUInt32(hslDeviceAddressAttribute.Address); if (!operateResult7.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult7)); } propertyInfo.SetValue(obj, operateResult7.Content, null); } else if (propertyType == typeof(uint[])) { OperateResult <uint[]> operateResult8 = readWrite.ReadUInt32(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult8.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult8)); } propertyInfo.SetValue(obj, operateResult8.Content, null); } else if (propertyType == typeof(long)) { OperateResult <long> operateResult9 = readWrite.ReadInt64(hslDeviceAddressAttribute.Address); if (!operateResult9.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult9)); } propertyInfo.SetValue(obj, operateResult9.Content, null); } else if (propertyType == typeof(long[])) { OperateResult <long[]> operateResult10 = readWrite.ReadInt64(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult10.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult10)); } propertyInfo.SetValue(obj, operateResult10.Content, null); } else if (propertyType == typeof(ulong)) { OperateResult <ulong> operateResult11 = readWrite.ReadUInt64(hslDeviceAddressAttribute.Address); if (!operateResult11.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult11)); } propertyInfo.SetValue(obj, operateResult11.Content, null); } else if (propertyType == typeof(ulong[])) { OperateResult <ulong[]> operateResult12 = readWrite.ReadUInt64(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult12.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult12)); } propertyInfo.SetValue(obj, operateResult12.Content, null); } else if (propertyType == typeof(float)) { OperateResult <float> operateResult13 = readWrite.ReadFloat(hslDeviceAddressAttribute.Address); if (!operateResult13.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult13)); } propertyInfo.SetValue(obj, operateResult13.Content, null); } else if (propertyType == typeof(float[])) { OperateResult <float[]> operateResult14 = readWrite.ReadFloat(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult14.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult14)); } propertyInfo.SetValue(obj, operateResult14.Content, null); } else if (propertyType == typeof(double)) { OperateResult <double> operateResult15 = readWrite.ReadDouble(hslDeviceAddressAttribute.Address); if (!operateResult15.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult15)); } propertyInfo.SetValue(obj, operateResult15.Content, null); } else if (propertyType == typeof(double[])) { OperateResult <double[]> operateResult16 = readWrite.ReadDouble(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult16.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult16)); } propertyInfo.SetValue(obj, operateResult16.Content, null); } else if (propertyType == typeof(string)) { OperateResult <string> operateResult17 = readWrite.ReadString(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult17.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult17)); } propertyInfo.SetValue(obj, operateResult17.Content, null); } else if (propertyType == typeof(byte[])) { OperateResult <byte[]> operateResult18 = readWrite.Read(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult18.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult18)); } propertyInfo.SetValue(obj, operateResult18.Content, null); } else if (propertyType == typeof(bool)) { OperateResult <bool> operateResult19 = readWrite.ReadBool(hslDeviceAddressAttribute.Address); if (!operateResult19.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult19)); } propertyInfo.SetValue(obj, operateResult19.Content, null); } else if (propertyType == typeof(bool[])) { OperateResult <bool[]> operateResult20 = readWrite.ReadBool(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length)); if (!operateResult20.IsSuccess) { return(OperateResult.CreateFailedResult <T>(operateResult20)); } propertyInfo.SetValue(obj, operateResult20.Content, null); } } return(OperateResult.CreateSuccessResult((T)obj)); }