test
This commit is contained in:
@@ -31,11 +31,20 @@ namespace 头罩视野.Views
|
||||
DispatcherTimer _timer;
|
||||
DataChange c = new DataChange();
|
||||
Function ma;
|
||||
//// 定时采集用
|
||||
private DispatcherTimer testTimer;
|
||||
// 保存上一条数据(用于去重)
|
||||
private TestDataStore.TestRecord _lastRecord;
|
||||
public PageTest()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitializeModbusTcp();
|
||||
|
||||
_timer = InitDispatcherTimer();
|
||||
// 2. 初始化定时器:500毫秒 执行一次
|
||||
testTimer = new DispatcherTimer();
|
||||
testTimer.Interval = TimeSpan.FromMilliseconds(500); // 500ms = 0.5秒
|
||||
testTimer.Tick += Timer_Tick;
|
||||
|
||||
}
|
||||
|
||||
//复位btn
|
||||
@@ -66,14 +75,58 @@ namespace 头罩视野.Views
|
||||
ma.BtnClickFunction(Function.ButtonType.复归型, 11);
|
||||
}
|
||||
|
||||
//测试btn
|
||||
//测试btn 测试按钮:读取数据,存入共享列表
|
||||
private void Button_Click_Test(object sender, RoutedEventArgs e)
|
||||
|
||||
{
|
||||
|
||||
testTimer.Start();
|
||||
ma.BtnClickFunction(Function.ButtonType.复归型, 101);
|
||||
}
|
||||
|
||||
private async void Timer_Tick(object sender, EventArgs e)
|
||||
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("定时器触发了!" + DateTime.Now);
|
||||
// 调用你原来的读取方
|
||||
//await ReadAddr262DataAsync();
|
||||
|
||||
// 组装当前数据
|
||||
var data = new TestDataStore.TestRecord
|
||||
{
|
||||
Date = DateTime.Now.ToString("yyyy-MM-dd"),
|
||||
Time = DateTime.Now.ToString("HH:mm:ss"),
|
||||
LeftEyeArea = double.TryParse(zmsyarea.Text, out var l) ? l : 0,
|
||||
RightEyeArea = double.TryParse(ymsyarea.Text, out var r) ? r : 0,
|
||||
BinocularArea = double.TryParse(smsyarea.Text, out var b) ? b : 0,
|
||||
LowerVision = double.TryParse(kbsyarea.Text, out var lv) ? lv : 0,
|
||||
VisionRetentionRate = double.TryParse(sybhl.Text, out var vr) ? vr : 0
|
||||
};
|
||||
|
||||
// ==================== 去重判断 ====================
|
||||
if (_lastRecord != null &&
|
||||
data.LeftEyeArea == _lastRecord.LeftEyeArea &&
|
||||
data.RightEyeArea == _lastRecord.RightEyeArea &&
|
||||
data.BinocularArea == _lastRecord.BinocularArea &&
|
||||
data.LowerVision == _lastRecord.LowerVision &&
|
||||
data.VisionRetentionRate == _lastRecord.VisionRetentionRate)
|
||||
{
|
||||
return; // 一样就不添加
|
||||
}
|
||||
// 不一样 → 插入表格
|
||||
TestDataStore.AddNewRecord(data);
|
||||
_lastRecord = data;
|
||||
}
|
||||
private void Page_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
testTimer?.Stop();
|
||||
}
|
||||
|
||||
//停止btn
|
||||
private void Button_Click_Stop(object sender, RoutedEventArgs e)
|
||||
{
|
||||
testTimer.Stop();
|
||||
MessageBox.Show("已停止");
|
||||
ma.BtnClickFunction(Function.ButtonType.复归型, 103);
|
||||
}
|
||||
//打印
|
||||
@@ -256,40 +309,9 @@ namespace 头罩视野.Views
|
||||
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
|
||||
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
||||
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
|
||||
private void InitializeModbusTcp()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
string plcIp = "192.168.1.10";
|
||||
bool initSuccess = ModbusResourceManager.Instance.Init(plcIp, 502);
|
||||
if (!initSuccess)
|
||||
{
|
||||
MessageBox.Show("连接Modbus服务器失败!", "错误");
|
||||
//this.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查连接状态
|
||||
if (_tcpClient == null || !_tcpClient.Connected)
|
||||
{
|
||||
MessageBox.Show("Modbus连接异常!", "错误");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ma = new Function(_modbusMaster);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowError($"Modbus初始化失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void Page_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
//InitializeModbusTcp();
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user