页面数据

This commit is contained in:
2026-04-24 15:43:33 +08:00
parent e59fed9b91
commit eaefadc2b1
2 changed files with 40 additions and 7 deletions

View File

@@ -24,6 +24,7 @@ namespace 头罩视野.Views
{
public partial class PageTest : Page
{
/// 只加这一个变量
private CancellationTokenSource? _cts;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
@@ -40,6 +41,7 @@ namespace 头罩视野.Views
public PageTest()
{
InitializeComponent();
System.Diagnostics.Debug.WriteLine("页面加载了111111111");
_timer = InitDispatcherTimer();
// 2. 初始化定时器500毫秒 执行一次
@@ -161,16 +163,28 @@ namespace 头罩视野.Views
await _modbusMaster.WriteSingleCoilAsync(1, 11, false);
//System.Diagnostics.Debug.WriteLine("正传end");
}
// 在 PageTest 类里面
private RecordDate? GetRecordDatePage()
{
// 拿到主窗口
var mainWin = Application.Current.MainWindow;
// 假设你全局切换页面的 Frame 名字叫 MainFrame
var frame = mainWin.FindName("MainFrame") as Frame;
// 返回 RecordDate 实例
return frame?.Content as RecordDate;
}
//测试btn 测试按钮:读取数据,存入共享列表
private void Button_Click_Test(object sender, RoutedEventArgs e)
private void Button_Click_Test(object sender, RoutedEventArgs e)
{
ma.BtnClickFunction(Function.ButtonType., 100);
ButtonTest.Content = "测试中....";
testTimer.Start();
var recordPage = GetRecordDatePage();
recordPage?.StartPlcTimer();
}
//停止btn
private void Button_Click_Stop(object sender, RoutedEventArgs e)
@@ -178,6 +192,8 @@ namespace 头罩视野.Views
ma.BtnClickFunction(Function.ButtonType., 103);
ButtonTest.Content = "测试";
testTimer.Stop();
var recordPage = GetRecordDatePage();
recordPage?.StopPlcTimer();
}
private async void Timer_Tick(object sender, EventArgs e)

View File

@@ -28,7 +28,7 @@ namespace 头罩视野.Views
{
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
private System.Timers.Timer _plcReadTimer;
private System.Timers.Timer? _plcReadTimer;
// 表跟数据存储列表
private List<dynamic> LeftEyeDataList = new List<dynamic>();
private List<dynamic> RightEyeDataList = new List<dynamic>();
@@ -52,7 +52,7 @@ namespace 头罩视野.Views
DynamicHeader();
// 2. 调用(名字和上面的变量一致)
// 2. 启动定时器定时读取数据每100ms读一次
StartPlcReadTimer(100);
//StartPlcReadTimer(100);
//// 判断连接
if (!ModbusHelper.IsConnected)
@@ -79,10 +79,22 @@ namespace 头罩视野.Views
});
}
}
//定时读取 PLC 数据
private void StartPlcReadTimer(int intervalMs)
public void StopPlcTimer()
{
if (_plcReadTimer != null)
{
_plcReadTimer.Stop();
_plcReadTimer.Dispose();
_plcReadTimer = null;
}
}
//定时读取 PLC 数据
public void StartPlcReadTimer(int intervalMs)
{
// 防止重复创建
if (_plcReadTimer != null && _plcReadTimer.Enabled)
return;
_plcReadTimer = new System.Timers.Timer(intervalMs);
_plcReadTimer.Elapsed += ReadPlcData;
_plcReadTimer.Start();
@@ -310,6 +322,11 @@ namespace 头罩视野.Views
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
internal void StartPlcTimer()
{
throw new NotImplementedException();
}
//NavigationService.Navigate(new Views.RecordDate()); 页面相互跳转
}