温湿度传感器更新
现在可以手动更新温湿度了
This commit is contained in:
71
Mods/工业数据采集与分析应用分享/Scripts/温湿度Reader.cs
Normal file
71
Mods/工业数据采集与分析应用分享/Scripts/温湿度Reader.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SharpModbus;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace BITFactory;
|
||||
public partial class 温湿度Reader : Node
|
||||
{
|
||||
[ExportCategory("参数")]
|
||||
[Export(PropertyHint.Range,"100,1000")] private int interval=500;
|
||||
|
||||
[ExportCategory("网络设置")]
|
||||
[Export] private string ip="192.168.3.7";
|
||||
[Export] private int port=502;
|
||||
|
||||
[ExportCategory("运行时")]
|
||||
[Export]public double humidity = 50.0;
|
||||
[Export]public double temperature = 26.0;
|
||||
|
||||
[ExportCategory("UI 绑定")]
|
||||
[Export] private Label temperatureLabel;
|
||||
[Export] private Label humidityLabel;
|
||||
|
||||
private ModbusMaster _modbus;
|
||||
private System.Timers.Timer timer;
|
||||
private CancellationTokenSource _CancellationTokenSource;
|
||||
public override void _Ready()
|
||||
{
|
||||
_CancellationTokenSource = new CancellationTokenSource();
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
|
||||
timer = new Timer();
|
||||
timer.Interval = interval;
|
||||
timer.Elapsed += OnTimerElapsed;
|
||||
timer.AutoReset = true;
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
timer.Stop();
|
||||
}
|
||||
|
||||
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
_CancellationTokenSource.Cancel();
|
||||
await UniTask.SwitchToTaskPool();
|
||||
try
|
||||
{
|
||||
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
var vs = _modbus.ReadInputRegisters(1, 0, 2);
|
||||
|
||||
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
if (vs is not { Length: 2 }) return;
|
||||
temperature = vs[0] / 10.0;
|
||||
humidity = vs[1] / 10.0;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
GD.Print("ex:" + ex);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user