页面逻辑调整
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Modbus.Device;
|
||||
using Microsoft.Win32;
|
||||
using Modbus.Device;
|
||||
using Sunny.UI;
|
||||
|
||||
//using RecordDateView;
|
||||
@@ -37,7 +38,8 @@ namespace 头罩视野.Views
|
||||
// 保存上一条数据(用于去重)
|
||||
private TestDataStore.TestRecord? _lastRecord;
|
||||
private object btnLeftEye;
|
||||
|
||||
// 表跟数据存储列表
|
||||
public List<dynamic> DataList = new List<dynamic>();
|
||||
|
||||
public PageTest()
|
||||
{
|
||||
@@ -178,8 +180,7 @@ namespace 头罩视野.Views
|
||||
ma.BtnClickFunction(Function.ButtonType.复归型, 100);
|
||||
ButtonTest.Content = "测试中....";
|
||||
testTimer.Start();
|
||||
//var recordPage = GetRecordDatePage();
|
||||
//recordPage?.StartPlcReadTimer(100);
|
||||
|
||||
|
||||
}
|
||||
//停止btn
|
||||
@@ -188,8 +189,7 @@ 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)
|
||||
@@ -285,7 +285,7 @@ namespace 头罩视野.Views
|
||||
};
|
||||
return timer;
|
||||
}
|
||||
|
||||
private List<(int m, int n)> _lightPositions;
|
||||
private async System.Threading.Tasks.Task ReadAddr262DataAsync()
|
||||
{
|
||||
try
|
||||
@@ -305,23 +305,45 @@ namespace 头罩视野.Views
|
||||
//ReadAndUpdateFloatAsync(430 ,2, sybhl, "F2", " "),
|
||||
|
||||
//前1从站地址,后1是长度
|
||||
|
||||
ReadLightBarData()
|
||||
|
||||
};
|
||||
isFinished = _modbusMaster.ReadCoils(1, 102, 1)[0];
|
||||
isFinished = _modbusMaster.ReadCoils(1, 102, 1)[0];
|
||||
if (isFinished)
|
||||
{
|
||||
int.TryParse(fbspeed.Text, out int speedValue);
|
||||
// 还不确定?
|
||||
var recordPage = GetRecordDatePage();
|
||||
recordPage?.StopPlcTimer();
|
||||
recordPage?.getAllData(recordPage.LeftEyeDataList, recordPage.RightEyeDataList, int.Parse(fbspeed.Text));
|
||||
Button_Click_Stop(null, null);
|
||||
//开始计算视野面积
|
||||
// 1. 把DataList(List<dynamic>)转成方法需要的int[]
|
||||
int[] lightData = DataList.Cast<int>().ToArray();
|
||||
|
||||
//2. 调用CalculateEllipseArea,传入两个参数
|
||||
double area = GetArea.CalculateEllipseArea(lightData, _lightPositions);
|
||||
|
||||
//值显示在页面,
|
||||
zmsyarea.Text = GlobalData.LeftEyeArea.ToString("0.00"); // 左目
|
||||
smsyarea.Text = GlobalData.BinocularArea.ToString("0.00"); // 双目
|
||||
kbsyarea.Text = GlobalData.BlankArea.ToString("0.00"); // 空白
|
||||
ymsyarea.Text = GlobalData.RightEyeArea.ToString("0.00"); // 右目
|
||||
xfsyarea.Text = GlobalData.LowerVision.ToString("0.00"); // 下方
|
||||
sybhl.Text = GlobalData.VisionRetentionRate.ToString("0.00"); // 视野保存率
|
||||
if (btnRight.Content.ToString() == "右眼开"&& btnLeft.Content.ToString() == "左眼关")
|
||||
{
|
||||
zmsyarea.Text = area.ToString("0.00");//左目
|
||||
}
|
||||
else if (btnRight.Content.ToString() == "右眼关" && btnLeft.Content.ToString() == "左眼开")
|
||||
{
|
||||
ymsyarea.Text = area.ToString("0.00"); //右目
|
||||
}
|
||||
else if(btnRight.Content.ToString() == "右眼关" && btnLeft.Content.ToString() == "左眼关")
|
||||
{
|
||||
smsyarea.Text = area.ToString("0.00"); // 双目
|
||||
}
|
||||
// 空白
|
||||
double binocularTotalArea = double.Parse(smsyarea.Text);
|
||||
double blankArea = GetArea.GetBlankViewArea(binocularTotalArea);
|
||||
kbsyarea.Text = blankArea.ToString("0.00"); // 保留2位小数
|
||||
|
||||
|
||||
double BottomViewAngle = GetArea.CalculateEllipseArea(lightData, _lightPositions);
|
||||
xfsyarea.Text = BottomViewAngle.ToString("0.00"); // 下方
|
||||
|
||||
|
||||
double binocularRate = GetArea.CalcVisionRate(binocularTotalArea);
|
||||
sybhl.Text = binocularRate.ToString("0.00"); // 视野保存率
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
@@ -400,56 +422,7 @@ namespace 头罩视野.Views
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBlock control, string format, string unit)
|
||||
{
|
||||
try
|
||||
{
|
||||
ushort[] registers = await Task.Run(async () =>
|
||||
await _modbusMaster!.ReadHoldingRegistersAsync(1, (ushort)address, (ushort)length)
|
||||
);
|
||||
|
||||
if (registers != null && registers.Length >= 2)
|
||||
{
|
||||
float value = c.UshortToFloat(registers[1], registers[0]);
|
||||
Dispatcher.Invoke(() => control.Text = value.ToString(format) + unit);
|
||||
}
|
||||
else if (registers != null && registers.Length >= 1)
|
||||
{
|
||||
int value = registers[0];
|
||||
Dispatcher.Invoke(() => control.Text = value.ToString(format) + unit);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"读取地址{address}失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReadAndUpdateIntAsync(int address, int length, System.Windows.Controls.Control control, string format)
|
||||
{
|
||||
try
|
||||
{
|
||||
ushort[] registers = await Task.Run(() =>
|
||||
_modbusMaster.ReadHoldingRegisters(1, (ushort)address, (ushort)length)
|
||||
);
|
||||
|
||||
if (registers != null && registers.Length >= 1)
|
||||
{
|
||||
int value = registers[0];
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
if (control is System.Windows.Controls.ContentControl contentControl)
|
||||
contentControl.Content = value.ToString(format);
|
||||
else if (control is System.Windows.Controls.TextBox textBox)
|
||||
textBox.Text = value.ToString(format);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"读取地址{address}失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//写入
|
||||
private void fbspeed_GotFocus(object sender, RoutedEventArgs e)
|
||||
@@ -473,7 +446,6 @@ namespace 头罩视野.Views
|
||||
}
|
||||
|
||||
|
||||
|
||||
//错误信息提示
|
||||
private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
||||
@@ -509,6 +481,46 @@ namespace 头罩视野.Views
|
||||
_cts = null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//读取灯泡的数据
|
||||
|
||||
/// <summary>
|
||||
/// 读取灯条PLC数据 → 自动存入 DataList
|
||||
/// 可直接放入 Task 列表等待
|
||||
/// </summary>
|
||||
private async Task ReadLightBarData()
|
||||
{
|
||||
// 无连接直接返回
|
||||
if (_modbusMaster == null || !ModbusHelper.TcpClient.Connected)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
// 读取灯条寄存器(地址350,长度15)
|
||||
ushort[] registers = await _modbusMaster.ReadHoldingRegistersAsync(1, 350, 15);
|
||||
|
||||
// 每个 ushort 是16位,低位在前
|
||||
foreach (ushort reg in registers)
|
||||
{
|
||||
for (int bit = 0; bit < 16; bit++)
|
||||
{
|
||||
// 取第bit位的值:0或1
|
||||
int lightBit = (reg & (1 << bit)) != 0 ? 1 : 0;
|
||||
DataList.Add(lightBit);
|
||||
}
|
||||
}
|
||||
|
||||
// 调试:打印转换后的数据长度
|
||||
System.Diagnostics.Debug.WriteLine($"灯条二进制数据总长度:{DataList.Count}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"灯条数据读取失败:{ex.Message}");
|
||||
DataList.Clear(); // 出错时清空数据
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,52 +231,52 @@ namespace 头罩视野.Views
|
||||
}
|
||||
|
||||
//面积的计算方法
|
||||
public void getAllData(List<dynamic> leftEyeDataList, List<dynamic> RightEyeDataList, int perAngle)
|
||||
//public void getAllData(List<dynamic> leftEyeDataList, List<dynamic> RightEyeDataList, int perAngle)
|
||||
|
||||
{
|
||||
//{
|
||||
|
||||
// 1. 先去除异常值,生成新列表(不修改原列表)
|
||||
var filteredLeft = GetArea.RemoveOutliers(leftEyeDataList);
|
||||
var filteredRight = GetArea.RemoveOutliers(RightEyeDataList);
|
||||
// // 1. 先去除异常值,生成新列表(不修改原列表)
|
||||
// var filteredLeft = GetArea.RemoveOutliers(leftEyeDataList);
|
||||
// var filteredRight = GetArea.RemoveOutliers(RightEyeDataList);
|
||||
|
||||
//左目视野面积
|
||||
GlobalData.LeftEyeArea = GetArea.CalculateEyeArea(filteredLeft);
|
||||
//右目视野面积
|
||||
GlobalData.RightEyeArea = GetArea.CalculateEyeArea(filteredRight);
|
||||
//双目视野面积
|
||||
GlobalData.BinocularArea = GetArea.CalcBinocularArea(filteredLeft, filteredRight);
|
||||
// //左目视野面积
|
||||
// GlobalData.LeftEyeArea = GetArea.CalculateEyeArea(filteredLeft);
|
||||
// //右目视野面积
|
||||
// GlobalData.RightEyeArea = GetArea.CalculateEyeArea(filteredRight);
|
||||
// //双目视野面积
|
||||
// GlobalData.BinocularArea = GetArea.CalcBinocularArea(filteredLeft, filteredRight);
|
||||
|
||||
//// 总视野面积
|
||||
GlobalData.TotalEyeArea = GlobalData.LeftEyeArea + GlobalData.RightEyeArea - GlobalData.BinocularArea;
|
||||
// //// 总视野面积
|
||||
// GlobalData.TotalEyeArea = GlobalData.LeftEyeArea + GlobalData.RightEyeArea - GlobalData.BinocularArea;
|
||||
|
||||
//// 空白视野面积
|
||||
GlobalData.BlankArea = GetArea.StandardTotal - GlobalData.TotalEyeArea;
|
||||
// //// 空白视野面积
|
||||
// GlobalData.BlankArea = GetArea.StandardTotal - GlobalData.TotalEyeArea;
|
||||
|
||||
//视野保存率
|
||||
// //视野保存率
|
||||
|
||||
// 左眼平均值数组
|
||||
double[] leftAvg = GetArea.GetEyeAvgArray(filteredLeft);
|
||||
// // 左眼平均值数组
|
||||
// double[] leftAvg = GetArea.GetEyeAvgArray(filteredLeft);
|
||||
|
||||
// 右眼平均值数组
|
||||
double[] rightAvg = GetArea.GetEyeAvgArray(RightEyeDataList);
|
||||
// // 右眼平均值数组
|
||||
// double[] rightAvg = GetArea.GetEyeAvgArray(RightEyeDataList);
|
||||
|
||||
double leftLowerAngle = GetArea.CalcLowerAngle(leftAvg, perAngle);
|
||||
double rightLowerAngle = GetArea.CalcLowerAngle(rightAvg, perAngle);
|
||||
//下方视野
|
||||
GlobalData.LowerVision = Math.Min(leftLowerAngle, rightLowerAngle);
|
||||
// double leftLowerAngle = GetArea.CalcLowerAngle(leftAvg, perAngle);
|
||||
// double rightLowerAngle = GetArea.CalcLowerAngle(rightAvg, perAngle);
|
||||
// //下方视野
|
||||
// GlobalData.LowerVision = Math.Min(leftLowerAngle, rightLowerAngle);
|
||||
|
||||
//视野保存率
|
||||
GlobalData.VisionRetentionRate = GetArea.CalcVisionRate(GlobalData.LeftEyeArea, GlobalData.RightEyeArea);
|
||||
// //视野保存率
|
||||
// GlobalData.VisionRetentionRate = GetArea.CalcVisionRate(GlobalData.LeftEyeArea, GlobalData.RightEyeArea);
|
||||
|
||||
//打印数值显示在系统上面
|
||||
System.Diagnostics.Debug.WriteLine("左目视野面积" + GlobalData.LeftEyeArea);
|
||||
System.Diagnostics.Debug.WriteLine("右目视野面积" + GlobalData.RightEyeArea);
|
||||
System.Diagnostics.Debug.WriteLine("双目视野面积" + GlobalData.BinocularArea);
|
||||
System.Diagnostics.Debug.WriteLine("总视野面积" + GlobalData.TotalEyeArea);
|
||||
System.Diagnostics.Debug.WriteLine("下方视野" + GlobalData.LowerVision);
|
||||
System.Diagnostics.Debug.WriteLine("视野保存率" + GlobalData.VisionRetentionRate);
|
||||
// //打印数值显示在系统上面
|
||||
// System.Diagnostics.Debug.WriteLine("左目视野面积" + GlobalData.LeftEyeArea);
|
||||
// System.Diagnostics.Debug.WriteLine("右目视野面积" + GlobalData.RightEyeArea);
|
||||
// System.Diagnostics.Debug.WriteLine("双目视野面积" + GlobalData.BinocularArea);
|
||||
// System.Diagnostics.Debug.WriteLine("总视野面积" + GlobalData.TotalEyeArea);
|
||||
// System.Diagnostics.Debug.WriteLine("下方视野" + GlobalData.LowerVision);
|
||||
// System.Diagnostics.Debug.WriteLine("视野保存率" + GlobalData.VisionRetentionRate);
|
||||
|
||||
}
|
||||
//}
|
||||
|
||||
|
||||
//#endregion
|
||||
|
||||
Reference in New Issue
Block a user