From d426e280d906f44a5bd5f2efb6bdc465bcc568d7 Mon Sep 17 00:00:00 2001
From: "2248356998 qq.com" <2248356998@qq.com>
Date: Mon, 22 May 2023 18:41:09 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=B7=BB=E5=8A=A0=E6=8F=92?=
=?UTF-8?q?=E4=BB=B6=E9=A9=B1=E5=8A=A8=E8=B0=83=E8=AF=95=E9=A1=B5=E9=9D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus/ModbusTest.cs | 11 +--
src/Plugins/ThingsGateway.Modbus/ModbusRtu.cs | 2 +
.../ModbusRtuDebugDriverPage.razor | 32 +++++++
.../SerialClientPage.razor | 63 ++++++++++++++
.../ThingsGateway.Modbus.csproj | 10 ++-
.../Serial/Serial/SerialProperty.cs | 6 ++
.../Plugin/CollectBase.cs | 5 +-
.../Plugin/DriverBase.cs | 6 ++
.../Wokers/Collect/CollectDeviceWorker.cs | 25 ++++++
.../Page/PluginDebugPage.razor | 86 +++++++++++++++++++
.../Page/PluginDebugPage.razor.cs | 27 ++++++
11 files changed, 263 insertions(+), 10 deletions(-)
create mode 100644 src/Plugins/ThingsGateway.Modbus/ModbusRtuDebugDriverPage.razor
create mode 100644 src/Plugins/ThingsGateway.Modbus/SerialClientPage.razor
create mode 100644 src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor
create mode 100644 src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor.cs
diff --git a/src/Plugins/ThingsGateway.Foundation.Tests/Modbus/ModbusTest.cs b/src/Plugins/ThingsGateway.Foundation.Tests/Modbus/ModbusTest.cs
index 5afcd5a32..74e3688fa 100644
--- a/src/Plugins/ThingsGateway.Foundation.Tests/Modbus/ModbusTest.cs
+++ b/src/Plugins/ThingsGateway.Foundation.Tests/Modbus/ModbusTest.cs
@@ -123,21 +123,22 @@ namespace ThingsGateway.Foundation.Tests
stopwatch.Stop();
_output.WriteLine(address + "ʱ" + stopwatch.Elapsed.TotalSeconds);
}
- [Theory(DisplayName = "127.0.0.1:503")]
+ [Theory(DisplayName = "127.0.0.1:502")]
//[MemberData(nameof(RangeData), 3, 1, 10)]
[MemberData(nameof(RangeData), 4, 1, 10)]
public async Task ModbusRtuOverTcpReadTest(string address)
{
- ModbusRtuOverTcpClient("127.0.0.1:503");
+ ModbusRtuOverTcpClient("127.0.0.1:502");
+ await ModbusRtuOverTcp.ConnectAsync();
Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
var byteConverter = ByteConverterHelper.GetTransByAddress(ref address, ModbusRtuOverTcp.ThingsGatewayBitConverter, out int length, out BcdFormat bcdFormat);
+ stopwatch.Start();
var test = await ModbusRtuOverTcp.ReadAsync(address, 1);
+ stopwatch.Stop();
+ _output.WriteLine(address + "ʱ" + stopwatch.Elapsed.TotalSeconds);
Assert.True(test.IsSuccess, test.Message);
var data = byteConverter.ToInt16(test.Content, 0);
_output.WriteLine(data.ToJson());
- stopwatch.Stop();
- _output.WriteLine(address + "ʱ" + stopwatch.Elapsed.TotalSeconds);
}
[Theory(DisplayName = "127.0.0.1:512")]
diff --git a/src/Plugins/ThingsGateway.Modbus/ModbusRtu.cs b/src/Plugins/ThingsGateway.Modbus/ModbusRtu.cs
index e7557acd2..97e454dcd 100644
--- a/src/Plugins/ThingsGateway.Modbus/ModbusRtu.cs
+++ b/src/Plugins/ThingsGateway.Modbus/ModbusRtu.cs
@@ -13,6 +13,8 @@ public class ModbusRtu : CollectBase, IDisposable
private ThingsGateway.Foundation.Adapter.Modbus.ModbusRtu _plc;
private ModbusRtuProperty driverPropertys = new ModbusRtuProperty();
+ public override Type DriverDebugUIType => typeof(ModbusRtuDebugDriverPage);
+
public ModbusRtu(IServiceScopeFactory scopeFactory) : base(scopeFactory)
{
diff --git a/src/Plugins/ThingsGateway.Modbus/ModbusRtuDebugDriverPage.razor b/src/Plugins/ThingsGateway.Modbus/ModbusRtuDebugDriverPage.razor
new file mode 100644
index 000000000..33eea2191
--- /dev/null
+++ b/src/Plugins/ThingsGateway.Modbus/ModbusRtuDebugDriverPage.razor
@@ -0,0 +1,32 @@
+@using BlazorComponent;
+@using Microsoft.AspNetCore.Components.Web;
+@using ThingsGateway.Foundation.Adapter.Modbus;
+@using ThingsGateway.Foundation;
+@using ThingsGateway.Web.Foundation;
+@using Masa.Blazor
+@inherits DriverDebugUIBase
+
+
+ @nameof(ModbusRtu)
+
+
+
+
+
+
+
+
+
+
+@code
+{
+ private SerialClientPage serialClientPage;
+ protected override void OnInitialized()
+ {
+
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/src/Plugins/ThingsGateway.Modbus/SerialClientPage.razor b/src/Plugins/ThingsGateway.Modbus/SerialClientPage.razor
new file mode 100644
index 000000000..064a86b45
--- /dev/null
+++ b/src/Plugins/ThingsGateway.Modbus/SerialClientPage.razor
@@ -0,0 +1,63 @@
+@using BlazorComponent;
+@using Microsoft.AspNetCore.Components.Web;
+@using System.IO.Ports;
+@using ThingsGateway.Core;
+@using ThingsGateway.Foundation.Adapter.Modbus;
+@using ThingsGateway.Foundation;
+@using ThingsGateway.Foundation.Serial;
+@using ThingsGateway.Web.Foundation;
+@using Masa.Blazor
+
+
+ x.PortName)) Dense Outlined HideDetails="@("auto")" @bind-Value=@serialProperty.PortName />
+
+ x.DataBits)) Dense Outlined HideDetails="@("auto")" @bind-Value=@serialProperty.DataBits />
+ x.BaudRate)) Dense Outlined HideDetails="@("auto")" @bind-Value=@serialProperty.BaudRate />
+ u.des)
+ ItemValue=@(u =>(Parity)u.value)
+ HideDetails=@("auto") Height="30"
+ Dense>
+
+ u.des)
+ ItemValue=@(u =>(StopBits)u.value)
+ HideDetails=@("auto") Height="30"
+ Dense>
+
+
+ {
+ serialClient.Close();
+ await GetSerialClient().OpenAsync();
+ }
+ ) Color="primary">
+ 连接
+
+
+
+
+
+
+@code
+{
+ private SerialProperty serialProperty = new SerialProperty();
+ private SerialClient serialClient { get; set; } = new();
+ private TouchSocketConfig config;
+ protected override void OnInitialized()
+ {
+ config = new TouchSocketConfig();
+ }
+ public SerialClient GetSerialClient()
+ {
+ config.SetSerialProperty(serialProperty).SetBufferLength(300);
+ //载入配置
+ serialClient.Setup(config);
+ return serialClient;
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/Plugins/ThingsGateway.Modbus/ThingsGateway.Modbus.csproj b/src/Plugins/ThingsGateway.Modbus/ThingsGateway.Modbus.csproj
index d6c14371e..8bcd7f764 100644
--- a/src/Plugins/ThingsGateway.Modbus/ThingsGateway.Modbus.csproj
+++ b/src/Plugins/ThingsGateway.Modbus/ThingsGateway.Modbus.csproj
@@ -1,4 +1,4 @@
-
+
latestMajor
@@ -17,6 +17,14 @@
+
+
+
+ Compile
+
+
+
+
Compile
diff --git a/src/ThingsGateway.Foundation/Serial/Serial/SerialProperty.cs b/src/ThingsGateway.Foundation/Serial/Serial/SerialProperty.cs
index 744f1b490..733d8509a 100644
--- a/src/ThingsGateway.Foundation/Serial/Serial/SerialProperty.cs
+++ b/src/ThingsGateway.Foundation/Serial/Serial/SerialProperty.cs
@@ -1,3 +1,4 @@
+using System.ComponentModel;
using System.IO.Ports;
namespace ThingsGateway.Foundation.Serial
@@ -10,22 +11,27 @@ namespace ThingsGateway.Foundation.Serial
///
/// COM
///
+ [Description("COM")]
public string PortName { get; set; } = "COM1";
///
///
///
+ [Description("")]
public int BaudRate { get; set; } = 9600;
///
/// λ
///
+ [Description("λ")]
public int DataBits { get; set; } = 8;
///
/// Уλ
///
+ [Description("Уλ")]
public Parity Parity { get; set; } = Parity.None;
///
/// ֹͣλ
///
+ [Description("ֹͣλ")]
public StopBits StopBits { get; set; } = StopBits.One;
///
/// ַתʵ࣬-ָ谴˳
diff --git a/src/ThingsGateway.Web.Foundation/Plugin/CollectBase.cs b/src/ThingsGateway.Web.Foundation/Plugin/CollectBase.cs
index e17d39267..0923480c2 100644
--- a/src/ThingsGateway.Web.Foundation/Plugin/CollectBase.cs
+++ b/src/ThingsGateway.Web.Foundation/Plugin/CollectBase.cs
@@ -28,10 +28,7 @@ public abstract class CollectBase : DriverBase
/// 导入变量UI Type,继承实现后,返回继承类的Type,如果不存在,返回null
///
public virtual Type DriverImportUIType { get; }
- ///
- /// 调试UI Type,继承实现后,返回继承类的Type,如果不存在,返回null
- ///
- public virtual Type DriverDebugUIType { get; }
+
///
/// 共享通道类型
///
diff --git a/src/ThingsGateway.Web.Foundation/Plugin/DriverBase.cs b/src/ThingsGateway.Web.Foundation/Plugin/DriverBase.cs
index 188e8e792..2733a82ef 100644
--- a/src/ThingsGateway.Web.Foundation/Plugin/DriverBase.cs
+++ b/src/ThingsGateway.Web.Foundation/Plugin/DriverBase.cs
@@ -42,6 +42,11 @@ namespace ThingsGateway.Web.Foundation
///
public abstract DriverPropertyBase DriverPropertys { get; }
+ ///
+ /// 调试UI Type,继承实现后,返回继承类的Type,如果不存在,返回null
+ ///
+ public virtual Type DriverDebugUIType { get; }
+
///
/// 是否输出日志
///
@@ -54,6 +59,7 @@ namespace ThingsGateway.Web.Foundation
/// 报文信息
///
public ConcurrentList<(long id, string message)> Messages { get; set; } = new();
+
///
/// 是否连接成功
///
diff --git a/src/ThingsGateway.Web.Foundation/Wokers/Collect/CollectDeviceWorker.cs b/src/ThingsGateway.Web.Foundation/Wokers/Collect/CollectDeviceWorker.cs
index 2d8cec4a6..dfb6f753b 100644
--- a/src/ThingsGateway.Web.Foundation/Wokers/Collect/CollectDeviceWorker.cs
+++ b/src/ThingsGateway.Web.Foundation/Wokers/Collect/CollectDeviceWorker.cs
@@ -358,6 +358,31 @@ public class CollectDeviceWorker : BackgroundService
}
}
+
+ ///
+ /// 获取导入变量UI
+ ///
+ ///
+ ///
+ public Type GetDebugUI(long driverId)
+ {
+ using var serviceScope = _scopeFactory.CreateScope();
+ var driverPluginService = serviceScope.ServiceProvider.GetService();
+ var driverPlugin = driverPluginService.GetDriverPluginById(driverId);
+ var id = YitIdHelper.NextId();
+ try
+ {
+ var driver = (DriverBase)_pluginService.GetDriver(id, driverPlugin);
+ driver?.Dispose();
+ return driver.DriverDebugUIType;
+ }
+ finally
+ {
+ _pluginService.DeleteDriver(id, driverId);
+ }
+
+ }
+
#endregion
#region worker服务
diff --git a/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor b/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor
new file mode 100644
index 000000000..860bc7496
--- /dev/null
+++ b/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor
@@ -0,0 +1,86 @@
+@page "/tgconfig/driverdebug"
+@namespace ThingsGateway.Web.Page
+@using System.Linq.Expressions;
+@using BlazorComponent;
+@using Furion.DataValidation;
+@using Mapster;
+@using Masa.Blazor.Presets;
+@using System.IO;
+@using TouchSocket.Core;
+@inject IVariableService VariableService
+@attribute [Authorize]
+@inject MasaBlazor MasaBlazor
+@attribute [RolePermission]
+@inherits CultureComponentBase
+@inject UserResoures UserResoures
+@inject ICollectDeviceService CollectDeviceService
+@layout MainLayout
+
+
+
+
+
+
+
+
+ {
+ if(this._searchModel.PluginId!= a.FirstOrDefault())
+ {
+ this._searchModel.PluginId= a.FirstOrDefault();
+ await ImportVaiable(this._searchModel.PluginId);
+ }
+ }) )
+ Items="DriverPlugins" ItemText="r=>r.Name" ItemChildren="r=>r.Children"
+ Search="@_searchName"
+ Activatable ItemKey=@(r=>r.Id)>
+
+
+
+
+
+ @if (_importRender != null)
+ {
+ @_importRender
+ }
+
+
+
+
+@code {
+ class PluginDebugUIInput
+ {
+ public string PluginName { get; set; }
+ public long PluginId { get; set; }
+ }
+}
+
+@code {
+ string _searchName;
+ List _deviceGroups = new();
+ PluginDebugUIInput _searchModel { get; set; } = new();
+ private BootstrapDynamicComponent _importComponent;
+ private RenderFragment _importRender;
+ private DriverDebugUIBase _importRef;
+ List DriverPlugins;
+ [Inject]
+ IDriverPluginService DriverPluginService { get; set; }
+
+ protected override void OnInitialized()
+ {
+ DriverPlugins = DriverPluginService.GetDriverPluginChildrenList(DriverEnum.Collect);
+
+ base.OnInitialized();
+ }
+ async Task ImportVaiable(long driverId)
+ {
+ var driver = this.GetBackgroundService().GetDebugUI(driverId);
+ if (driver == null)
+ {
+ await PopupService.EnqueueSnackbarAsync("插件未实现调试页面", AlertTypes.Warning);
+ return;
+ }
+ _importComponent = new BootstrapDynamicComponent(driver);
+ _importRender = _importComponent.Render(a => _importRef = (DriverDebugUIBase)a);
+ }
+}
\ No newline at end of file
diff --git a/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor.cs b/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor.cs
new file mode 100644
index 000000000..b5cfb8250
--- /dev/null
+++ b/src/ThingsGateway.Web.Page/Page/PluginDebugPage.razor.cs
@@ -0,0 +1,27 @@
+using SqlSugar;
+
+namespace ThingsGateway.Web.Page
+{
+ public partial class PluginDebugPage
+ {
+
+ [CascadingParameter]
+ MainLayout MainLayout { get; set; }
+
+
+ [Inject]
+ ResourceService ResourceService { get; set; }
+
+ protected override async Task OnInitializedAsync()
+ {
+ await base.OnInitializedAsync();
+ }
+ [Inject]
+ IUploadDeviceService UploadDeviceService { get; set; }
+ protected override async Task OnParametersSetAsync()
+ {
+ await base.OnParametersSetAsync();
+ }
+
+ }
+}
\ No newline at end of file