From 92ad2ae8f7afcb19a30b6d28ed954c0db3f9c0d2 Mon Sep 17 00:00:00 2001 From: iioter <535915157@qq.com> Date: Wed, 10 Aug 2022 16:51:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96AllenBradley=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DriverAllenBradley/AllenBradley.cs | 78 ++++++++----------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs b/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs index 8843e5d..edc7564 100644 --- a/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs +++ b/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs @@ -1,36 +1,29 @@ using IoTClient.Clients.PLC; -using IoTClient.Enums; using PluginInterface; -using System; -using System.Text; using Microsoft.Extensions.Logging; namespace DriverAllenBradley { [DriverSupported("AllenBradley")] - [DriverInfoAttribute("AllenBradley", "V1.0.0", "Copyright IoTGateway-IoTClient© 2022-01-15")] + [DriverInfo("AllenBradley", "V1.0.0", "Copyright IoTGateway-IoTClient© 2022-01-15")] public class AllenBradley : IDriver { - private AllenBradleyClient plc = null; + private AllenBradleyClient? _plc; public ILogger _logger { get; set; } private readonly string _device; + #region 配置参数 - [ConfigParameter("设备Id")] - public Guid DeviceId { get; set; } + [ConfigParameter("设备Id")] public string DeviceId { get; set; } - [ConfigParameter("IP地址")] - public string IpAddress { get; set; } = "127.0.0.1"; + [ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1"; - [ConfigParameter("端口号")] - public int Port { get; set; } = 44818; + [ConfigParameter("端口号")] public int Port { get; set; } = 44818; - [ConfigParameter("超时时间ms")] - public int Timeout { get; set; } = 3000; + [ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000; - [ConfigParameter("最小通讯周期ms")] - public uint MinPeriod { get; set; } = 3000; + [ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000; #endregion @@ -42,26 +35,23 @@ namespace DriverAllenBradley _logger.LogInformation($"Device:[{device}],Create()"); } - public bool IsConnected { - get - { - return plc != null && plc.Connected; - } + get { return _plc != null && _plc.Connected; } } public bool Connect() { try { - plc = new AllenBradleyClient(IpAddress, Port); - plc.Open(); + _plc = new AllenBradleyClient(IpAddress, Port); + _plc.Open(); } catch (Exception) { return false; } + return IsConnected; } @@ -69,12 +59,11 @@ namespace DriverAllenBradley { try { - plc?.Close(); + _plc?.Close(); return !IsConnected; } catch (Exception) { - return false; } } @@ -83,11 +72,10 @@ namespace DriverAllenBradley { try { - plc = null; + _plc = null; } - catch (Exception) + catch (Exception ex) { - } } @@ -96,61 +84,62 @@ namespace DriverAllenBradley { var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good }; - if (plc != null && this.IsConnected) + if (_plc != null && this.IsConnected) { try { switch (ioarg.ValueType) { case PluginInterface.DataTypeEnum.Bit: - ret.Value = plc.ReadBoolean(ioarg.Address).Value == true ? 1 : 0; + ret.Value = _plc.ReadBoolean(ioarg.Address).Value == true ? 1 : 0; break; case PluginInterface.DataTypeEnum.Bool: - ret.Value = plc.ReadBoolean(ioarg.Address).Value; + ret.Value = _plc.ReadBoolean(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.UByte: - ret.Value = plc.ReadByte(ioarg.Address).Value; + ret.Value = _plc.ReadByte(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Byte: - ret.Value = (sbyte)plc.ReadByte(ioarg.Address).Value; + ret.Value = (sbyte)_plc.ReadByte(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Uint16: - ret.Value =plc.ReadUInt16(ioarg.Address).Value; + ret.Value = _plc.ReadUInt16(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Int16: - ret.Value = plc.ReadInt16(ioarg.Address).Value; + ret.Value = _plc.ReadInt16(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Uint32: - ret.Value =plc.ReadUInt32(ioarg.Address).Value; + ret.Value = _plc.ReadUInt32(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Int32: - ret.Value = plc.ReadInt32(ioarg.Address).Value; + ret.Value = _plc.ReadInt32(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Float: - ret.Value = plc.ReadFloat(ioarg.Address).Value; + ret.Value = _plc.ReadFloat(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Double: - ret.Value = plc.ReadDouble(ioarg.Address).Value; + ret.Value = _plc.ReadDouble(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Uint64: - ret.Value = plc.ReadUInt64(ioarg.Address).Value; + ret.Value = _plc.ReadUInt64(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.Int64: - ret.Value = plc.ReadInt64(ioarg.Address).Value; + ret.Value = _plc.ReadInt64(ioarg.Address).Value; break; case PluginInterface.DataTypeEnum.AsciiString: - ret.Value = plc.ReadString(ioarg.Address); + ret.Value = _plc.ReadString(ioarg.Address); break; case PluginInterface.DataTypeEnum.Utf8String: - ret.Value = plc.ReadString(ioarg.Address); + ret.Value = _plc.ReadString(ioarg.Address); break; default: + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"不支持的类型"; break; } } catch (Exception ex) { - ret.StatusType = VaribaleStatusTypeEnum.Bad; ret.Message = $"读取失败,{ex.Message}"; } @@ -160,10 +149,11 @@ namespace DriverAllenBradley ret.StatusType = VaribaleStatusTypeEnum.Bad; ret.Message = "连接失败"; } + return ret; } - public async Task WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg) + public async Task WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg) { RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" }; return rpcResponse;