添加通过Modbus读取温湿度的设置
可通过用户界面调整地址
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SharpModbus;
|
||||
using Timer = System.Timers.Timer;
|
||||
@@ -21,8 +24,11 @@ public partial class 温湿度Reader : Node
|
||||
[Export]public double temperature = 26.0;
|
||||
|
||||
[ExportCategory("UI 绑定")]
|
||||
[Export] private Label temperatureLabel;
|
||||
[Export] private Label humidityLabel;
|
||||
[Export] private UXContainer temperatureContaier;
|
||||
[Export] private UXContainer humidityContainer;
|
||||
[Export] private LineEdit ipEdit;
|
||||
[Export] private LineEdit portEdit;
|
||||
[Export] private RichTextLabel hintsLabel;
|
||||
|
||||
private ModbusMaster _modbus;
|
||||
private System.Timers.Timer timer;
|
||||
@@ -30,7 +36,37 @@ public partial class 温湿度Reader : Node
|
||||
public override void _Ready()
|
||||
{
|
||||
_CancellationTokenSource = new CancellationTokenSource();
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
try
|
||||
{
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
hintsLabel.Text = e.Message;
|
||||
}
|
||||
|
||||
|
||||
if (ipEdit is not null)
|
||||
{
|
||||
ipEdit.TextChanged += s => ip = s;
|
||||
ipEdit.TextSubmitted +=s=> UpdatePortAndIP();
|
||||
}
|
||||
if(portEdit is not null)
|
||||
{
|
||||
portEdit.TextSubmitted += OnPortChanged;
|
||||
void OnPortChanged(string s)
|
||||
{
|
||||
UpdatePortAndIP();
|
||||
if (int.TryParse(s,out var newPort))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
portEdit.Text=string.Empty;
|
||||
portEdit.PlaceholderText = "请输入正确的端口号";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
timer = new Timer();
|
||||
timer.Interval = interval;
|
||||
@@ -43,9 +79,16 @@ public partial class 温湿度Reader : Node
|
||||
{
|
||||
timer.Stop();
|
||||
}
|
||||
|
||||
private void UpdatePortAndIP()
|
||||
{
|
||||
_modbus.Dispose();
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
}
|
||||
|
||||
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
SetHints( "正在获取温湿度数据..."+DateTime.Now);
|
||||
_CancellationTokenSource.Cancel();
|
||||
await UniTask.SwitchToTaskPool();
|
||||
try
|
||||
@@ -56,16 +99,35 @@ public partial class 温湿度Reader : Node
|
||||
|
||||
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
if (vs is not { Length: 2 }) return;
|
||||
if (vs is not { Length: 2 })
|
||||
{
|
||||
SetHints(hintsLabel.Text = $"获取温湿度数据失败:数据长度为:{vs.Length}"+DateTime.Now);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
temperature = vs[0] / 10.0;
|
||||
humidity = vs[1] / 10.0;
|
||||
|
||||
SetHints("已获取到温湿度数据:"+DateTime.Now);
|
||||
|
||||
temperatureContaier.Text = temperature.ToString(CultureInfo.InvariantCulture);
|
||||
humidityContainer.Text = humidity.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
//SetHints("连接超时:"+DateTime.Now);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SetHints(ex.Message);
|
||||
GD.Print("ex:" + ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async void SetHints(string hints)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
hintsLabel.Text = hints;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user