diff --git a/IoTGateway/iotgateway.db b/IoTGateway/iotgateway.db index 2664769..aee9041 100644 Binary files a/IoTGateway/iotgateway.db and b/IoTGateway/iotgateway.db differ diff --git a/Plugins/Drivers/PLC.LS/DeviceXGB.cs b/Plugins/Drivers/PLC.LS/DeviceXGB.cs index 6ea2b86..47d3069 100644 --- a/Plugins/Drivers/PLC.LS/DeviceXGB.cs +++ b/Plugins/Drivers/PLC.LS/DeviceXGB.cs @@ -10,7 +10,7 @@ namespace PLC.LSIS { [DriverSupported("FENet")] [DriverSupported("CNet")] - [DriverInfo("XGB", "V1.0.0", "Copyright IoTGateway.net 20230220")] + [DriverInfo("XGB", "V1.0.0", "Copyright IoTGateway.net 20231223")] public class DeviceXGB : IDriver { public ILogger _logger { get; set; } @@ -20,35 +20,35 @@ namespace PLC.LSIS private XGBFastEnet fastEnet = null; - #region 配置参数 + #region ConfigParameter - [ConfigParameter("设备Id")] public string DeviceId { get; set; } + [ConfigParameter("Device Id")] public string DeviceId { get; set; } - [ConfigParameter("PLC类型")] public LSCpuInfo lSCpuInfo { get; set; } = LSCpuInfo.XGB; + [ConfigParameter("PLC LSCpuInfo")] public LSCpuInfo lSCpuInfo { get; set; } = LSCpuInfo.XGB; - [ConfigParameter("PLC类型")] public LSTypeConnect lSTypeConnect { get; set; } = LSTypeConnect.FENet; + [ConfigParameter("PLC TypeConnect")] public LSTypeConnect lSTypeConnect { get; set; } = LSTypeConnect.FENet; - [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; } = 2004; + [ConfigParameter("Port")] public int Port { get; set; } = 2004; [ConfigParameter("Slot")] public short Slot { get; set; } = 0; - [ConfigParameter("串口名")] public string PortName { get; set; } = "COM1"; + [ConfigParameter("PortName")] public string PortName { get; set; } = "COM1"; - [ConfigParameter("波特率")] public int BaudRate { get; set; } = 9600; + [ConfigParameter("BaudRate")] public int BaudRate { get; set; } = 9600; - [ConfigParameter("数据位")] public int DataBits { get; set; } = 8; + [ConfigParameter("DataBits")] public int DataBits { get; set; } = 8; - [ConfigParameter("校验位")] public Parity Parity { get; set; } = Parity.None; + [ConfigParameter("Parity")] public Parity Parity { get; set; } = Parity.None; - [ConfigParameter("停止位")] public StopBits StopBits { get; set; } = StopBits.One; + [ConfigParameter("StopBits")] public StopBits StopBits { get; set; } = StopBits.One; - [ConfigParameter("从站号")] public byte Station { get; set; } = 1; + [ConfigParameter("Station")] public byte Station { get; set; } = 1; - [ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000; + [ConfigParameter("Overtime time ms")] public int Timeout { get; set; } = 3000; - [ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000; + [ConfigParameter("Minimum communication Cycle ms")] public uint MinPeriod { get; set; } = 3000; #endregion @@ -81,6 +81,8 @@ namespace PLC.LSIS if (fastEnet == null || !IsConnected) { fastEnet = new XGBFastEnet(IpAddress, Port); + fastEnet.SlotNo = (byte)Slot; + fastEnet.SetCpuType = $"{lSCpuInfo}"; IsConnected = fastEnet.ConnectServer().IsSuccess; } break; @@ -219,7 +221,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -232,7 +234,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -245,7 +247,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -258,7 +260,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; @@ -273,7 +275,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -287,7 +289,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -301,7 +303,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -315,7 +317,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -329,7 +331,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -343,7 +345,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -357,7 +359,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -371,7 +373,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -379,7 +381,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.UnKnow; - ret.Message = "类型未定义"; + ret.Message = "Read failed"; _logger.LogError($"Device:[{_device}],[{ioArg.ValueType}]类型未定义"); return ret; } @@ -421,7 +423,7 @@ namespace PLC.LSIS var args = ioArg.Address.Split(','); startAddress = args[0]; - var length = ushort.Parse(args[1]); + ushort length = (ushort)(ushort.Parse(args[1]) * 2); if (ioArg.ValueType == DataTypeEnum.Bool) { @@ -433,7 +435,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -447,7 +449,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -460,7 +462,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -473,7 +475,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; @@ -488,7 +490,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -502,7 +504,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -516,7 +518,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -530,7 +532,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -544,7 +546,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -558,7 +560,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -572,7 +574,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -586,7 +588,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.Bad; - ret.Message = $"读取失败"; + ret.Message = $"Read failed"; } return ret; } @@ -594,7 +596,7 @@ namespace PLC.LSIS { IsConnected = false; ret.StatusType = VaribaleStatusTypeEnum.UnKnow; - ret.Message = "类型未定义"; + ret.Message = "Read failed"; _logger.LogError($"Device:[{_device}],[{ioArg.ValueType}]类型未定义"); return ret; } @@ -620,7 +622,7 @@ namespace PLC.LSIS { OperateResult writeResult; if (!IsConnected) - rpcResponse.Description = "设备连接已断开"; + rpcResponse.Description = "Device connection has been disconnected"; else { @@ -716,13 +718,13 @@ namespace PLC.LSIS - rpcResponse.Description = $"不支持写入:{Method}"; + rpcResponse.Description = $"Writing is not supported:{Method}"; } } catch (Exception ex) { IsConnected = false; - rpcResponse.Description = $"写入失败,[method]:{Method},[ioArg]:{ioArg},[ex]:{ex}"; + rpcResponse.Description = $"Write failed,[method]:{Method},[ioArg]:{ioArg},[ex]:{ex}"; _logger.LogError(ex, $"Device:[{_device}],WriteAsync(),Error"); } diff --git a/Plugins/Drivers/PLC.LS/DeviceXGK.cs b/Plugins/Drivers/PLC.LS/DeviceXGK.cs new file mode 100644 index 0000000..534fa10 --- /dev/null +++ b/Plugins/Drivers/PLC.LS/DeviceXGK.cs @@ -0,0 +1,735 @@ +using HslCommunication; +using HslCommunication.Profinet.LSIS; +using Microsoft.Extensions.Logging; +using PluginInterface; +using System.IO.Ports; + +namespace PLC.LSIS +{ + [DriverSupported("FENet")] + [DriverSupported("CNet")] + [DriverInfo("XGK", "V1.0.0", "Copyright IoTGateway.net 20231223")] + public class DeviceXGK : IDriver + { + public ILogger _logger { get; set; } + private readonly string _device; + + private XGKCnet xGBCnet = null; + private XGKFastEnet fastEnet = null; + + + #region ConfigParameter + + [ConfigParameter("Device Id")] public string DeviceId { get; set; } + + [ConfigParameter("PLC LSCpuInfo")] public LSCpuInfo lSCpuInfo { get; set; } = LSCpuInfo.XGK; + + [ConfigParameter("PLC TypeConnect")] public LSTypeConnect lSTypeConnect { get; set; } = LSTypeConnect.FENet; + + [ConfigParameter("IP")] public string IpAddress { get; set; } = "127.0.0.1"; + + [ConfigParameter("Port")] public int Port { get; set; } = 2004; + + [ConfigParameter("Slot")] public short Slot { get; set; } = 0; + + [ConfigParameter("PortName")] public string PortName { get; set; } = "COM1"; + + [ConfigParameter("BaudRate")] public int BaudRate { get; set; } = 9600; + + [ConfigParameter("DataBits")] public int DataBits { get; set; } = 8; + + [ConfigParameter("Parity")] public Parity Parity { get; set; } = Parity.None; + + [ConfigParameter("StopBits")] public StopBits StopBits { get; set; } = StopBits.One; + + [ConfigParameter("Station")] public byte Station { get; set; } = 1; + + [ConfigParameter("Overtime time ms")] public int Timeout { get; set; } = 3000; + + [ConfigParameter("Minimum communication Cycle ms")] public uint MinPeriod { get; set; } = 3000; + + #endregion + + #region + /// + /// 反射构造函数 + /// + /// + /// + public DeviceXGK(string device, ILogger logger) + { + _device = device; + _logger = logger; + + _logger.LogInformation($"Device:[{_device}],Create()"); + } + + public bool IsConnected { get; set; } + + public bool Connect() + { + try + { + _logger.LogInformation($"Device:[{_device}],Connect()"); + if (!IsConnected) + { + switch (lSTypeConnect) + { + case LSTypeConnect.FENet: + if (fastEnet == null || !IsConnected) + { + fastEnet = new XGKFastEnet(IpAddress, Port); + + fastEnet.SlotNo = (byte)Slot; + fastEnet.SetCpuType = $"{lSCpuInfo}"; + IsConnected = fastEnet.ConnectServer().IsSuccess; + } + break; + case LSTypeConnect.CNet: + if (xGBCnet == null || !IsConnected) + { + xGBCnet = new XGKCnet + { + Station = Station + }; + xGBCnet.SerialPortInni(sp => + { + sp.PortName = PortName; + sp.BaudRate = BaudRate; + sp.DataBits = DataBits; + sp.StopBits = StopBits; + sp.Parity = Parity; + }); + xGBCnet.Open(); + IsConnected = xGBCnet.IsOpen(); + } + break; + + default: + return IsConnected; + } + + } + else + { + return IsConnected; + } + return IsConnected; + } + catch (Exception ex) + { + _logger.LogError(ex, $"Device:[{_device}],Connect()"); + return IsConnected; + } + } + + public bool Close() + { + try + { + _logger.LogInformation($"Device:[{_device}],Close()"); + switch (lSTypeConnect) + { + case LSTypeConnect.FENet: + if (fastEnet != null) + { + + fastEnet.ConnectClose(); + return !IsConnected; + } + break; + case LSTypeConnect.CNet: + if (xGBCnet != null) + { + xGBCnet.Close(); + + return !IsConnected; + } + break; + + default: + return false; + } + return !IsConnected; + } + catch (Exception ex) + { + _logger.LogError(ex, $"Device:[{_device}],Close()"); + return false; + } + } + + public void Dispose() + { + try + { + _logger?.LogInformation($"Device:[{_device}],Dispose()"); + switch (lSTypeConnect) + { + case LSTypeConnect.FENet: + if (fastEnet != null) + { + + fastEnet.Dispose(); + + } + break; + case LSTypeConnect.CNet: + if (xGBCnet != null) + { + xGBCnet.Dispose(); + + + } + break; + + + + } + + // Suppress finalization. + GC.SuppressFinalize(this); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Device:[{_device}],Dispose()"); + } + + } + [Method("XGK", description: "Read")] + public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg) + { + DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good }; + if (!IsConnected) + ret.StatusType = VaribaleStatusTypeEnum.Bad; + else + { + if (ret.StatusType != VaribaleStatusTypeEnum.Good) + return ret; + try + { + + + if (ioArg.ValueType == DataTypeEnum.Bool) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadBool(ioArg.Address, 1) : xGBCnet.ReadBool(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Bit) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadBool(ioArg.Address, 1) : xGBCnet.ReadBool(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Byte) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadByte(ioArg.Address) : xGBCnet.ReadByte(ioArg.Address); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Uint16) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt16(ioArg.Address, 1) : xGBCnet.ReadUInt16(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + + } + else if (ioArg.ValueType == DataTypeEnum.Int16) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt16(ioArg.Address, 1) : xGBCnet.ReadInt16(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Int32) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt32(ioArg.Address, 1) : xGBCnet.ReadInt32(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Uint32) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt32(ioArg.Address, 1) : xGBCnet.ReadUInt32(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Int64) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt64(ioArg.Address, 1) : xGBCnet.ReadInt64(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Uint64) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt64(ioArg.Address, 1) : xGBCnet.ReadUInt64(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Double) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadDouble(ioArg.Address, 1) : xGBCnet.ReadDouble(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Float) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadFloat(ioArg.Address, 1) : xGBCnet.ReadFloat(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content[0]); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.AsciiString) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadString(ioArg.Address, 1) : xGBCnet.ReadString(ioArg.Address, 1); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.UnKnow; + ret.Message = "Read failed"; + _logger.LogError($"Device:[{_device}],[{ioArg.ValueType}]类型未定义"); + return ret; + } + } + catch (Exception ex) + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = ex.Message; + _logger.LogError(ex, $"Device:[{_device}],ReadRegistersBuffers(),Error"); + } + } + + + return ret; + + } + [Method("XGK", description: "ReadMultiple")] + public DriverReturnValueModel ReadMultiple(DriverAddressIoArgModel ioArg) + { + DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good }; + + + + if (!IsConnected) + ret.StatusType = VaribaleStatusTypeEnum.Bad; + else + { + if (ret.StatusType != VaribaleStatusTypeEnum.Good) + return ret; + try + { + string startAddress; + if (ioArg.Address.Contains('|')) + { + startAddress = ioArg.Address.Split('|')[0]; + ioArg.Address = ioArg.Address.Split('|')[1]; + } + var args = ioArg.Address.Split(','); + + startAddress = args[0]; + ushort length = (ushort)(ushort.Parse(args[1]) * 2); + + if (ioArg.ValueType == DataTypeEnum.Bool) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadBool(startAddress, length) : xGBCnet.ReadBool(startAddress, length); + + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Bit) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadBool(startAddress, length) : xGBCnet.ReadBool(startAddress, length); + + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Byte) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Read(startAddress, length) : xGBCnet.Read(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + if (ioArg.ValueType == DataTypeEnum.Uint16) + { + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt16(startAddress, length) : xGBCnet.ReadUInt16(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + + } + else if (ioArg.ValueType == DataTypeEnum.Int16) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt16(startAddress, length) : xGBCnet.ReadInt16(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Int32) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt32(startAddress, length) : xGBCnet.ReadInt32(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Uint32) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt32(startAddress, length) : xGBCnet.ReadUInt32(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Int64) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadInt64(startAddress, length) : xGBCnet.ReadInt64(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Uint64) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadUInt64(startAddress, length) : xGBCnet.ReadUInt64(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Double) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadDouble(startAddress, length) : xGBCnet.ReadDouble(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.Float) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadFloat(startAddress, length) : xGBCnet.ReadFloat(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else if (ioArg.ValueType == DataTypeEnum.AsciiString) + { + + OperateResult read = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.ReadString(startAddress, length) : xGBCnet.ReadString(startAddress, length); + if (read.IsSuccess) + ret.Value = Newtonsoft.Json.JsonConvert.SerializeObject(read.Content); + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = $"Read failed"; + } + return ret; + } + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.UnKnow; + ret.Message = "Read failed"; + _logger.LogError($"Device:[{_device}],[{ioArg.ValueType}]类型未定义"); + return ret; + } + } + catch (Exception ex) + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.Bad; + ret.Message = ex.Message; + _logger.LogError(ex, $"Device:[{_device}],ReadRegistersBuffers(),Error"); + } + } + + + return ret; + + } + [Method("XGK", description: "Write")] + public async Task WriteAsync(string RequestId, string Method, DriverAddressIoArgModel ioArg) + { + RpcResponse rpcResponse = new() { IsSuccess = false }; + try + { + OperateResult writeResult; + if (!IsConnected) + rpcResponse.Description = "Device connection has been disconnected"; + else + { + + + if (ioArg.ValueType == DataTypeEnum.Bool) + { + var value = ioArg.Value.ToString() == "1" || ioArg.Value.ToString().ToLower() == "true"; + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.WriteCoil(ioArg.Address, value) : xGBCnet.WriteCoil(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + + + } + if (ioArg.ValueType == DataTypeEnum.Bit) + { + var value = ioArg.Value.ToString() == "1" || ioArg.Value.ToString().ToLower() == "true"; + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.WriteCoil(ioArg.Address, value) : xGBCnet.WriteCoil(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + if (ioArg.ValueType == DataTypeEnum.Byte) + { + var value = ioArg.Value.ToString(); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + + rpcResponse.IsSuccess = true; + return rpcResponse; + } + if (ioArg.ValueType == DataTypeEnum.Uint16) + { + var value = ushort.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + + } + else if (ioArg.ValueType == DataTypeEnum.Int16) + { + var value = short.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Int32) + { + + var value = int.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Uint32) + { + + var value = uint.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Int64) + { + + var value = long.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Uint64) + { + + var value = ulong.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Double) + { + + var value = double.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + else if (ioArg.ValueType == DataTypeEnum.Float) + { + + var value = float.Parse(ioArg.Value.ToString()); + writeResult = lSTypeConnect == LSTypeConnect.FENet ? fastEnet.Write(ioArg.Address, value) : xGBCnet.Write(ioArg.Address, value); + rpcResponse.IsSuccess = true; + return rpcResponse; + } + + + + + rpcResponse.Description = $"Writing is not supported:{Method}"; + } + } + catch (Exception ex) + { + IsConnected = false; + rpcResponse.Description = $"Write failed,[method]:{Method},[ioArg]:{ioArg},[ex]:{ex}"; + _logger.LogError(ex, $"Device:[{_device}],WriteAsync(),Error"); + } + + return rpcResponse; + } + #endregion + + } +} diff --git a/Plugins/Plugin/MyMqttClient.cs b/Plugins/Plugin/MyMqttClient.cs index 75a4ccf..5db5cda 100644 --- a/Plugins/Plugin/MyMqttClient.cs +++ b/Plugins/Plugin/MyMqttClient.cs @@ -64,9 +64,12 @@ namespace Plugin Client.DisconnectedAsync += Client_DisconnectedAsync; Client.ApplicationMessageReceivedAsync += Client_ApplicationMessageReceivedAsync; - await Client.ConnectAsync(_options); + if(Client.ConnectAsync(_options).IsCompletedSuccessfully) ; + { + _logger.LogInformation("MQTT WAITING FOR APPLICATION MESSAGES"); + } - _logger.LogInformation("MQTT WAITING FOR APPLICATION MESSAGES"); + } catch (Exception ex) { @@ -126,7 +129,7 @@ namespace Plugin try { _logger.LogError($"MQTT DISCONNECTED WITH SERVER "); - await Client.ConnectAsync(_options); + await Client.DisconnectAsync(); } catch (Exception ex) {