diff --git a/IoTGateway.sln b/IoTGateway.sln index 9ba8878..353f924 100644 --- a/IoTGateway.sln +++ b/IoTGateway.sln @@ -65,6 +65,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mock", "Mock", "{87954B15-5 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mock.TcpClient", "Plugins\Drivers\Mock.TcpClient\Mock.TcpClient.csproj", "{1DE34C11-5461-442B-8C67-FCB0247C09D3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLC.LSIS", "Plugins\Drivers\PLC.LS\PLC.LSIS.csproj", "{D3043673-283C-45D7-8F86-6BAAD89AF761}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -241,6 +243,14 @@ Global {1DE34C11-5461-442B-8C67-FCB0247C09D3}.Release|Any CPU.Build.0 = Release|Any CPU {1DE34C11-5461-442B-8C67-FCB0247C09D3}.Release|x86.ActiveCfg = Release|Any CPU {1DE34C11-5461-442B-8C67-FCB0247C09D3}.Release|x86.Build.0 = Release|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Debug|x86.ActiveCfg = Debug|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Debug|x86.Build.0 = Debug|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Release|Any CPU.Build.0 = Release|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Release|x86.ActiveCfg = Release|Any CPU + {D3043673-283C-45D7-8F86-6BAAD89AF761}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -271,6 +281,7 @@ Global {0C82F208-B9B3-485D-A6DD-9ADC59FBF831} = {F8957F78-9D5D-4905-9EFC-EEEBA3A3A13F} {87954B15-5C35-4454-AFC7-9DCE8066D5A0} = {D05CFF72-D58C-418A-8F52-B06848DAC4F1} {1DE34C11-5461-442B-8C67-FCB0247C09D3} = {87954B15-5C35-4454-AFC7-9DCE8066D5A0} + {D3043673-283C-45D7-8F86-6BAAD89AF761} = {1606D516-3865-4F57-9199-93091CF546F5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1F219808-E6E8-4C1D-B846-41F2F7CF20FA} diff --git a/IoTGateway/IoTGateway.csproj.user b/IoTGateway/IoTGateway.csproj.user index 0fe4b33..75a2511 100644 --- a/IoTGateway/IoTGateway.csproj.user +++ b/IoTGateway/IoTGateway.csproj.user @@ -2,7 +2,7 @@ IoTGateway - D:\000000-IIoTGateway\00000-iotgateway\IoTGateway\Properties\PublishProfiles\FolderProfile.pubxml + FolderProfile RazorViewEmptyScaffolder root/Common/MVC/View diff --git a/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user b/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user index 7b6b1c9..666b147 100644 --- a/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/IoTGateway/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - <_PublishTargetUrl>D:\000000-IIoTGateway\00000-iotgateway\IoTGateway\bin\Release\net6.0\publish\ - True|2023-05-06T08:43:37.2780869Z;True|2023-05-06T16:16:53.5825444+08:00;True|2023-05-06T15:23:48.5579380+08:00;True|2023-02-27T11:39:28.6812515+08:00;True|2023-02-20T16:42:44.7526644+08:00;True|2023-02-20T16:12:12.9091659+08:00;True|2023-02-20T09:51:20.5504880+08:00;True|2023-02-20T09:35:31.1757073+08:00;True|2023-01-13T15:48:56.7869969+08:00;True|2022-11-21T15:59:03.8927175+08:00;True|2022-11-21T15:58:06.9464735+08:00;True|2022-11-21T15:57:24.8426888+08:00;True|2022-11-21T15:55:06.1675530+08:00;True|2022-10-20T22:40:01.3534004+08:00;True|2022-08-30T20:02:03.8144013+08:00;True|2022-08-30T19:59:49.6042620+08:00;True|2022-08-30T19:52:02.8823029+08:00;True|2022-08-30T19:36:54.3646619+08:00;True|2022-07-15T22:49:16.0563176+08:00;True|2022-07-15T22:48:07.3189129+08:00;True|2022-07-15T22:44:35.7670375+08:00;True|2022-07-15T22:40:17.0184184+08:00;True|2022-06-09T15:43:47.6180879+08:00;True|2022-06-09T15:42:06.6021355+08:00;False|2022-06-09T15:41:10.3444145+08:00;False|2022-06-09T15:39:22.0048230+08:00;False|2022-06-09T15:37:35.5049088+08:00;True|2022-06-09T15:34:27.8095097+08:00;True|2022-06-09T15:33:30.9577399+08:00;False|2022-06-09T15:29:58.9432537+08:00;False|2022-06-09T15:29:15.9735490+08:00;True|2022-06-09T15:27:02.3333134+08:00;True|2022-06-09T15:18:02.0623963+08:00;True|2022-01-24T23:31:28.3620602+08:00;True|2022-01-24T23:30:03.8991942+08:00;True|2022-01-24T23:23:16.4288882+08:00;True|2021-12-24T19:18:19.0736393+08:00;True|2021-12-24T16:46:49.1192015+08:00;True|2021-12-24T16:23:28.9214784+08:00;True|2021-12-24T15:20:08.6401847+08:00;True|2021-12-17T19:11:07.1655146+08:00;True|2021-12-12T14:11:08.8380502+08:00; + <_PublishTargetUrl>C:\Users\UserX\RiderProjects\iotgateway1\IoTGateway\bin\Release\net6.0\publish\ + True|2023-12-19T20:21:26.6090666Z;True|2023-05-06T10:43:37.2780869+02:00;True|2023-05-06T10:16:53.5825444+02:00;True|2023-05-06T09:23:48.5579380+02:00;True|2023-02-27T05:39:28.6812515+02:00;True|2023-02-20T10:42:44.7526644+02:00;True|2023-02-20T10:12:12.9091659+02:00;True|2023-02-20T03:51:20.5504880+02:00;True|2023-02-20T03:35:31.1757073+02:00;True|2023-01-13T09:48:56.7869969+02:00;True|2022-11-21T09:59:03.8927175+02:00;True|2022-11-21T09:58:06.9464735+02:00;True|2022-11-21T09:57:24.8426888+02:00;True|2022-11-21T09:55:06.1675530+02:00;True|2022-10-20T16:40:01.3534004+02:00;True|2022-08-30T14:02:03.8144013+02:00;True|2022-08-30T13:59:49.6042620+02:00;True|2022-08-30T13:52:02.8823029+02:00;True|2022-08-30T13:36:54.3646619+02:00;True|2022-07-15T16:49:16.0563176+02:00;True|2022-07-15T16:48:07.3189129+02:00;True|2022-07-15T16:44:35.7670375+02:00;True|2022-07-15T16:40:17.0184184+02:00;True|2022-06-09T09:43:47.6180879+02:00;True|2022-06-09T09:42:06.6021355+02:00;False|2022-06-09T09:41:10.3444145+02:00;False|2022-06-09T09:39:22.0048230+02:00;False|2022-06-09T09:37:35.5049088+02:00;True|2022-06-09T09:34:27.8095097+02:00;True|2022-06-09T09:33:30.9577399+02:00;False|2022-06-09T09:29:58.9432537+02:00;False|2022-06-09T09:29:15.9735490+02:00;True|2022-06-09T09:27:02.3333134+02:00;True|2022-06-09T09:18:02.0623963+02:00;True|2022-01-24T17:31:28.3620602+02:00;True|2022-01-24T17:30:03.8991942+02:00;True|2022-01-24T17:23:16.4288882+02:00;True|2021-12-24T13:18:19.0736393+02:00;True|2021-12-24T10:46:49.1192015+02:00;True|2021-12-24T10:23:28.9214784+02:00;True|2021-12-24T09:20:08.6401847+02:00;True|2021-12-17T13:11:07.1655146+02:00;True|2021-12-12T08:11:08.8380502+02:00; \ No newline at end of file diff --git a/IoTGateway/iotgateway.db b/IoTGateway/iotgateway.db index ef5a5b9..104db51 100644 Binary files a/IoTGateway/iotgateway.db and b/IoTGateway/iotgateway.db differ diff --git a/Plugins/Drivers/CNC.Fanuc.H/CNC.Fanuc.H.csproj b/Plugins/Drivers/CNC.Fanuc.H/CNC.Fanuc.H.csproj index 1c37d26..3536637 100644 --- a/Plugins/Drivers/CNC.Fanuc.H/CNC.Fanuc.H.csproj +++ b/Plugins/Drivers/CNC.Fanuc.H/CNC.Fanuc.H.csproj @@ -9,7 +9,7 @@ - + diff --git a/Plugins/Drivers/PLC.LS/DeviceXGB.cs b/Plugins/Drivers/PLC.LS/DeviceXGB.cs new file mode 100644 index 0000000..6ea2b86 --- /dev/null +++ b/Plugins/Drivers/PLC.LS/DeviceXGB.cs @@ -0,0 +1,741 @@ +using HslCommunication; +using HslCommunication.Profinet.LSIS; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Linq; +using PluginInterface; +using System.IO.Ports; +using System.Net; + +namespace PLC.LSIS +{ + [DriverSupported("FENet")] + [DriverSupported("CNet")] + [DriverInfo("XGB", "V1.0.0", "Copyright IoTGateway.net 20230220")] + public class DeviceXGB : IDriver + { + public ILogger _logger { get; set; } + private readonly string _device; + + private XGBCnet xGBCnet = null; + private XGBFastEnet fastEnet = null; + + + #region 配置参数 + + [ConfigParameter("设备Id")] public string DeviceId { get; set; } + + [ConfigParameter("PLC类型")] public LSCpuInfo lSCpuInfo { get; set; } = LSCpuInfo.XGB; + + [ConfigParameter("PLC类型")] public LSTypeConnect lSTypeConnect { get; set; } = LSTypeConnect.FENet; + + [ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1"; + + [ConfigParameter("端口号")] public int Port { get; set; } = 2004; + + [ConfigParameter("Slot")] public short Slot { get; set; } = 0; + + [ConfigParameter("串口名")] public string PortName { get; set; } = "COM1"; + + [ConfigParameter("波特率")] public int BaudRate { get; set; } = 9600; + + [ConfigParameter("数据位")] public int DataBits { get; set; } = 8; + + [ConfigParameter("校验位")] public Parity Parity { get; set; } = Parity.None; + + [ConfigParameter("停止位")] public StopBits StopBits { get; set; } = StopBits.One; + + [ConfigParameter("从站号")] public byte Station { get; set; } = 1; + + [ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000; + + [ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000; + + #endregion + + #region + /// + /// 反射构造函数 + /// + /// + /// + public DeviceXGB(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 XGBFastEnet(IpAddress, Port); + IsConnected = fastEnet.ConnectServer().IsSuccess; + } + break; + case LSTypeConnect.CNet: + if (xGBCnet == null || !IsConnected) + { + xGBCnet = new XGBCnet + { + 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("XGB", 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + return ret; + } + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.UnKnow; + ret.Message = "类型未定义"; + _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("XGB", 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]; + var length = ushort.Parse(args[1]); + + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + 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 = $"读取失败"; + } + return ret; + } + else + { + IsConnected = false; + ret.StatusType = VaribaleStatusTypeEnum.UnKnow; + ret.Message = "类型未定义"; + _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("XGB", description: "Write")] + public async Task WriteAsync(string RequestId, string Method, DriverAddressIoArgModel ioArg) + { + RpcResponse rpcResponse = new() { IsSuccess = false }; + try + { + OperateResult writeResult; + if (!IsConnected) + rpcResponse.Description = "设备连接已断开"; + 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 = $"不支持写入:{Method}"; + } + } + catch (Exception ex) + { + IsConnected = false; + rpcResponse.Description = $"写入失败,[method]:{Method},[ioArg]:{ioArg},[ex]:{ex}"; + _logger.LogError(ex, $"Device:[{_device}],WriteAsync(),Error"); + } + + return rpcResponse; + } + #endregion + + } + + public enum LSTypeConnect + { + CNet = 0, + FENet = 1, + + } +} diff --git a/Plugins/Drivers/PLC.LS/PLC.LSIS.csproj b/Plugins/Drivers/PLC.LS/PLC.LSIS.csproj new file mode 100644 index 0000000..09e2dee --- /dev/null +++ b/Plugins/Drivers/PLC.LS/PLC.LSIS.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + ../../../IoTGateway/bin/Debug/net6.0/drivers + enable + enable + + + + + + + + + + + diff --git a/Plugins/Plugin/MyMqttClient.cs b/Plugins/Plugin/MyMqttClient.cs index 75a4ccf..9aabde0 100644 --- a/Plugins/Plugin/MyMqttClient.cs +++ b/Plugins/Plugin/MyMqttClient.cs @@ -32,7 +32,7 @@ namespace Plugin _logger = logger; //_uaNodeManager = uaService.server.m_server.nodeManagers[0] as ReferenceNodeManager; - StartClientAsync().Wait(); + //StartClientAsync().Wait(); } public async Task StartClientAsync() @@ -76,6 +76,10 @@ namespace Plugin private async Task Client_ConnectedAsync(MqttClientConnectedEventArgs arg) { + if (Client == null) + { + StartClientAsync().Wait(); + } _logger.LogInformation($"MQTT CONNECTED WITH SERVER "); #region Topics try @@ -123,6 +127,10 @@ namespace Plugin private async Task Client_DisconnectedAsync(MqttClientDisconnectedEventArgs arg) { + if (Client == null) + { + StartClientAsync().Wait(); + } try { _logger.LogError($"MQTT DISCONNECTED WITH SERVER "); @@ -256,6 +264,10 @@ namespace Plugin private async Task ResponseTbRpcAsync(TBRpcResponse tBRpcResponse) { + if (Client == null) + { + StartClientAsync().Wait(); + } await Client.PublishAsync(new MqttApplicationMessageBuilder() .WithTopic(_tbRpcTopic) .WithPayload(JsonConvert.SerializeObject(tBRpcResponse)) @@ -264,6 +276,10 @@ namespace Plugin private async Task ResponseTcRpcAsync(TCRpcRequest tCRpcResponse) { + if (Client == null) + { + StartClientAsync().Wait(); + } var topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}"; await Client.PublishAsync(new MqttApplicationMessageBuilder() .WithTopic(topic) @@ -273,6 +289,10 @@ namespace Plugin private async Task ResponseIsRpcAsync(ISRpcResponse rpcResult) { + if (Client == null) + { + StartClientAsync().Wait(); + } //var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}"; var topic = $"devices/{rpcResult.DeviceId}/rpc/response/{rpcResult.Method}/{rpcResult.ResponseId}"; await Client.PublishAsync(new MqttApplicationMessageBuilder() @@ -313,6 +333,10 @@ namespace Plugin //Message: {"Device A":{"attribute1":"value1", "attribute2": 42}, "Device B":{"attribute1":"value1", "attribute2": 42} try { + if (Client == null) + { + StartClientAsync().Wait(); + } return Client.PublishAsync(new MqttApplicationMessageBuilder() .WithTopic($"devices/{deviceName}/attributes").WithPayload(JsonConvert.SerializeObject(obj)) .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce) @@ -328,12 +352,20 @@ namespace Plugin public async Task UploadIsTelemetryDataAsync(string deviceName, object obj) { + if (Client == null) + { + StartClientAsync().Wait(); + } await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{deviceName}/telemetry") .WithPayload(JsonConvert.SerializeObject(obj)).Build()); } public async Task UploadTcTelemetryDataAsync(string deviceName, object obj) { + if (Client == null) + { + StartClientAsync().Wait(); + } var toSend = new Dictionary { { deviceName, obj } }; await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"gateway/attributes") .WithPayload(JsonConvert.SerializeObject(toSend)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); @@ -361,6 +393,10 @@ namespace Plugin { Devices = hwTelemetry }; + if (Client == null) + { + StartClientAsync().Wait(); + } await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"/v1/devices/{_systemConfig.GatewayName}/datas") .WithPayload(JsonConvert.SerializeObject(hwTelemetrys)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); @@ -418,6 +454,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } string id = Guid.NewGuid().ToString(); switch (_systemConfig.IoTPlatformType) { @@ -512,6 +552,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } if (CanPubTelemetry(deviceName, device, sendModel)) { switch (_systemConfig.IoTPlatformType) @@ -583,6 +627,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } switch (_systemConfig.IoTPlatformType) { case IoTPlatformType.ThingsBoard: @@ -638,6 +686,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } switch (_systemConfig.IoTPlatformType) { case IoTPlatformType.ThingsBoard: @@ -693,6 +745,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } switch (_systemConfig.IoTPlatformType) { case IoTPlatformType.HuaWei: @@ -728,6 +784,10 @@ namespace Plugin { try { + if (Client == null) + { + StartClientAsync().Wait(); + } switch (_systemConfig.IoTPlatformType) { case IoTPlatformType.HuaWei: