Before update to NET 7
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Net.Mime;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
@@ -13,7 +15,7 @@ namespace BITFactory;
|
||||
public partial class 温湿度Reader : Node
|
||||
{
|
||||
[ExportCategory("参数")]
|
||||
[Export(PropertyHint.Range,"100,1000")] private int interval=500;
|
||||
[Export(PropertyHint.Range,"100,2000")] private int interval=500;
|
||||
|
||||
[ExportCategory("网络设置")]
|
||||
[Export] private string ip="192.168.3.7";
|
||||
@@ -33,6 +35,7 @@ public partial class 温湿度Reader : Node
|
||||
private ModbusMaster _modbus;
|
||||
private System.Timers.Timer timer;
|
||||
private CancellationTokenSource _CancellationTokenSource;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_CancellationTokenSource = new CancellationTokenSource();
|
||||
@@ -67,11 +70,13 @@ public partial class 温湿度Reader : Node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UpdatePortAndIP();
|
||||
|
||||
timer = new Timer();
|
||||
timer.Interval = interval;
|
||||
timer.Elapsed += OnTimerElapsed;
|
||||
timer.AutoReset = true;
|
||||
timer.AutoReset = false;
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
@@ -79,29 +84,42 @@ public partial class 温湿度Reader : Node
|
||||
{
|
||||
timer.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部方法,用于更新IP和端口
|
||||
/// </summary>
|
||||
private void UpdatePortAndIP()
|
||||
{
|
||||
_modbus.Dispose();
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
_modbus?.Dispose();
|
||||
try
|
||||
{
|
||||
_modbus = ModbusMaster.TCP(ip, port);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Log<温湿度Reader>(e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部方法,定时器回调用于读取Modbus
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private async void OnTimerElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
SetHints( "正在获取温湿度数据..."+DateTime.Now);
|
||||
_CancellationTokenSource.Cancel();
|
||||
await UniTask.SwitchToTaskPool();
|
||||
try
|
||||
{
|
||||
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
hintsLabel.SetTextAsync( "正在读取温湿度数据..."+DateTime.Now);
|
||||
|
||||
var vs = _modbus.ReadInputRegisters(1, 0, 2);
|
||||
|
||||
_CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
|
||||
hintsLabel.SetTextAsync( "已采集到数据,正在解析..."+DateTime.Now);
|
||||
|
||||
if (vs is not { Length: 2 })
|
||||
{
|
||||
SetHints(hintsLabel.Text = $"获取温湿度数据失败:数据长度为:{vs.Length}"+DateTime.Now);
|
||||
hintsLabel.SetTextAsync(hintsLabel.Text = $"获取温湿度数据失败:数据长度为:{vs.Length}"+DateTime.Now);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -109,25 +127,21 @@ public partial class 温湿度Reader : Node
|
||||
temperature = vs[0] / 10.0;
|
||||
humidity = vs[1] / 10.0;
|
||||
|
||||
SetHints("已获取到温湿度数据:"+DateTime.Now);
|
||||
hintsLabel.SetTextAsync("已获取到温湿度数据:"+DateTime.Now);
|
||||
|
||||
temperatureContaier.Text = temperature.ToString(CultureInfo.InvariantCulture);
|
||||
humidityContainer.Text = humidity.ToString(CultureInfo.InvariantCulture);
|
||||
temperatureContaier.label.SetTextAsync(temperature.ToString(CultureInfo.InvariantCulture)); ;
|
||||
humidityContainer.label.SetTextAsync(humidity.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
//SetHints("连接超时:"+DateTime.Now);
|
||||
hintsLabel.SetTextAsync("连接超时:"+DateTime.Now);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SetHints(ex.Message);
|
||||
GD.Print("ex:" + ex);
|
||||
hintsLabel.SetTextAsync(ex.Message);
|
||||
//GD.Print("ex:" + ex);
|
||||
}
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
private async void SetHints(string hints)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
hintsLabel.Text = hints;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user