初始化页面调试
This commit is contained in:
@@ -36,7 +36,7 @@ namespace 头罩视野.Services
|
||||
|
||||
public static List<dynamic> RemoveOutliers(List<dynamic> data)
|
||||
{
|
||||
for (int i = 1; i < data.Count - 1; i++)
|
||||
for (int i = 3; i < data.Count - 1; i++)
|
||||
{
|
||||
if (Math.Abs(data[i] - data[i - 1]) > OutlierDiffThreshold && Math.Abs(data[i] - data[i + 1]) > OutlierDiffThreshold)
|
||||
{
|
||||
@@ -52,7 +52,6 @@ namespace 头罩视野.Services
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 计算单眼视野面积
|
||||
/// </summary>
|
||||
|
||||
@@ -179,8 +179,8 @@ namespace 头罩视野.Views
|
||||
ma.BtnClickFunction(Function.ButtonType.复归型, 100);
|
||||
ButtonTest.Content = "测试中....";
|
||||
testTimer.Start();
|
||||
var recordPage = GetRecordDatePage();
|
||||
recordPage?.StartPlcReadTimer(100);
|
||||
//var recordPage = GetRecordDatePage();
|
||||
//recordPage?.StartPlcReadTimer(100);
|
||||
|
||||
}
|
||||
//停止btn
|
||||
|
||||
@@ -34,10 +34,13 @@ namespace 头罩视野.Views
|
||||
public List<dynamic> LeftEyeDataList = new List<dynamic>();
|
||||
public List<dynamic> RightEyeDataList = new List<dynamic>();
|
||||
|
||||
public List<dynamic> CalLeftData = new List<dynamic>();
|
||||
public List<dynamic> CalRightData = new List<dynamic>();
|
||||
|
||||
// 配置:和你PLC地址完全对应 左目
|
||||
private const int LeftEyeStartAddress = 1362; // D1362
|
||||
private const int ChannelCount = 72; // 72个通道
|
||||
private const int RegistersPerChannel = 2; // 每个通道2个寄存器(Float)
|
||||
private const int RegistersPerChannel = 1; // 每个通道2个寄存器(Float)
|
||||
|
||||
//右目
|
||||
|
||||
@@ -52,7 +55,7 @@ namespace 头罩视野.Views
|
||||
DynamicHeader();
|
||||
// 2. 调用(名字和上面的变量一致)
|
||||
// 2. 启动定时器,定时读取数据(每100ms读一次)
|
||||
//StartPlcReadTimer(100);
|
||||
StartPlcReadTimer(1000);
|
||||
|
||||
//// 判断连接
|
||||
if (!ModbusHelper.IsConnected)
|
||||
@@ -107,22 +110,19 @@ namespace 头罩视野.Views
|
||||
ReadPlcDataGeneric(
|
||||
slaveAddress: 1,
|
||||
startAddress: LeftEyeStartAddress,
|
||||
count: (ushort)(ChannelCount * RegistersPerChannel),
|
||||
count: 72,
|
||||
dataList: LeftEyeDataList,
|
||||
dataGrid: dataGrid1);
|
||||
|
||||
// 右通道
|
||||
ReadPlcDataGeneric(
|
||||
slaveAddress: 1,
|
||||
startAddress: RightEyeStartAddress,
|
||||
count: (ushort)(ChannelCount * RegistersPerChannel),
|
||||
dataList: RightEyeDataList,
|
||||
dataGrid: dataGrid2);
|
||||
//// 右通道
|
||||
//ReadPlcDataGeneric(
|
||||
// slaveAddress: 1,
|
||||
// startAddress: RightEyeStartAddress,
|
||||
// count: (ushort)(ChannelCount * RegistersPerChannel),
|
||||
// dataList: RightEyeDataList,
|
||||
// dataGrid: dataGrid2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 读取PLC HoldingRegisters 并更新到列表和DataGrid
|
||||
|
||||
@@ -151,10 +151,11 @@ namespace 头罩视野.Views
|
||||
ushort[] registers = _modbusMaster.ReadHoldingRegisters(
|
||||
slaveAddress: slaveAddress,
|
||||
startAddress: startAddress,
|
||||
numberOfPoints: count);
|
||||
numberOfPoints: (ushort)count);
|
||||
|
||||
uint[] data32 = ConvertRegistersToUInt32(registers, true);
|
||||
// 先把变量存为局部变量,解决闭包问题
|
||||
var regCopy = registers;
|
||||
var regCopy = data32;
|
||||
var listCopy = dataList;
|
||||
var gridCopy = dataGrid;
|
||||
|
||||
@@ -163,6 +164,8 @@ namespace 头罩视野.Views
|
||||
{
|
||||
AddPlcDataRow(regCopy, listCopy, gridCopy);
|
||||
});
|
||||
|
||||
System.Diagnostics.Debug.WriteLine("读取寄存器数据" );
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -170,38 +173,61 @@ namespace 头罩视野.Views
|
||||
}
|
||||
}
|
||||
|
||||
private uint[] ConvertRegistersToUInt32(ushort[] registers, bool isLittleEndian)
|
||||
{
|
||||
if (registers == null || registers.Length % 2 != 0)
|
||||
throw new ArgumentException("寄存器数量必须是偶数");
|
||||
|
||||
uint[] result = new uint[registers.Length / 2];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
ushort low = registers[i * 2];
|
||||
ushort high = registers[i * 2 + 1];
|
||||
|
||||
if (isLittleEndian)
|
||||
result[i] = (uint)(low | (high << 16));
|
||||
else
|
||||
result[i] = (uint)(high | (low << 16));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 把PLC数据添加到动态表格
|
||||
/// </summary>
|
||||
private void AddPlcDataRow(ushort[] registers, List<dynamic> dataList, DataGrid dg)
|
||||
|
||||
private int _rowIndex = 1;
|
||||
|
||||
private void AddPlcDataRow(uint[] registers, List<dynamic> dataList, DataGrid dg)
|
||||
{
|
||||
// 1. 先清空临时列表,不影响历史数据
|
||||
//dataList.Clear();
|
||||
|
||||
// 清空旧数据,防止重复
|
||||
dataList.Clear();
|
||||
// 2. 构建一次采集的单行数据(包含所有通道)
|
||||
dynamic newRow = new System.Dynamic.ExpandoObject();
|
||||
var dict = (IDictionary<string, object>)newRow;
|
||||
|
||||
// 把PLC数据存入列表
|
||||
dataList.AddRange(registers);
|
||||
// 固定列:编号、时间、日期(每次采集的这一行)
|
||||
dict["Id"] = _rowIndex++;
|
||||
dict["Time"] = DateTime.Now.ToString("HH:mm:ss");
|
||||
dict["Date"] = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
|
||||
// 构建动态行(适配你原来的ExpandoObject逻辑)
|
||||
List<dynamic> rows = new List<dynamic>();
|
||||
// 关键:循环给所有通道赋值,一行里包含所有通道值
|
||||
for (int i = 0; i < registers.Length; i++)
|
||||
{
|
||||
dynamic row = new System.Dynamic.ExpandoObject();
|
||||
var dict = (IDictionary<string, object>)row;
|
||||
|
||||
// 固定列:编号、时间、数值
|
||||
dict["Id"] = i + 1;
|
||||
dict["Time"] = DateTime.Now.ToString("HH:mm:ss");
|
||||
dict["Value"] = registers[i];
|
||||
|
||||
rows.Add(row);
|
||||
// 字段名和你界面列绑定的名称必须完全一致,比如 ch.1 / Ch1
|
||||
dict[$"Ch{i + 1}"] = registers[i];
|
||||
//dict[$"ch.{i + 1}"] = registers[i];
|
||||
}
|
||||
|
||||
// 更新DataGrid
|
||||
// 3. 把这一行加到历史列表,实现“读多行”
|
||||
dataList.Add(newRow);
|
||||
|
||||
// 4. 强制刷新DataGrid,让界面显示多行
|
||||
dg.Dispatcher.Invoke(() =>
|
||||
{
|
||||
dg.ItemsSource = null;
|
||||
dg.ItemsSource = rows;
|
||||
|
||||
|
||||
dg.ItemsSource = dataList;
|
||||
dg.Items.Refresh(); // 强制刷新视图,避免不渲染
|
||||
});
|
||||
}
|
||||
|
||||
//面积的计算方法
|
||||
@@ -213,13 +239,12 @@ namespace 头罩视野.Views
|
||||
var filteredLeft = GetArea.RemoveOutliers(leftEyeDataList);
|
||||
var filteredRight = GetArea.RemoveOutliers(RightEyeDataList);
|
||||
|
||||
|
||||
//左目视野面积
|
||||
GlobalData.LeftEyeArea = GetArea.CalculateEyeArea(leftEyeDataList);
|
||||
GlobalData.LeftEyeArea = GetArea.CalculateEyeArea(filteredLeft);
|
||||
//右目视野面积
|
||||
GlobalData.RightEyeArea = GetArea.CalculateEyeArea(RightEyeDataList);
|
||||
GlobalData.RightEyeArea = GetArea.CalculateEyeArea(filteredRight);
|
||||
//双目视野面积
|
||||
GlobalData.BinocularArea = GetArea.CalcBinocularArea(leftEyeDataList, RightEyeDataList);
|
||||
GlobalData.BinocularArea = GetArea.CalcBinocularArea(filteredLeft, filteredRight);
|
||||
|
||||
//// 总视野面积
|
||||
GlobalData.TotalEyeArea = GlobalData.LeftEyeArea + GlobalData.RightEyeArea - GlobalData.BinocularArea;
|
||||
@@ -230,7 +255,7 @@ namespace 头罩视野.Views
|
||||
//视野保存率
|
||||
|
||||
// 左眼平均值数组
|
||||
double[] leftAvg = GetArea.GetEyeAvgArray(leftEyeDataList);
|
||||
double[] leftAvg = GetArea.GetEyeAvgArray(filteredLeft);
|
||||
|
||||
// 右眼平均值数组
|
||||
double[] rightAvg = GetArea.GetEyeAvgArray(RightEyeDataList);
|
||||
@@ -273,16 +298,17 @@ namespace 头罩视野.Views
|
||||
//清除
|
||||
private void btnClear_MouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
_isClearPressed = true;
|
||||
_clearThread = new Thread(() =>
|
||||
{
|
||||
Thread.Sleep(500); // 长按1秒触发
|
||||
if (_isClearPressed)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() => ClearAllData());
|
||||
}
|
||||
});
|
||||
_clearThread.Start();
|
||||
//_isClearPressed = true;
|
||||
//_clearThread = new Thread(() =>
|
||||
//{
|
||||
// Thread.Sleep(500); // 长按1秒触发
|
||||
// if (_isClearPressed)
|
||||
// {
|
||||
// Application.Current.Dispatcher.Invoke(() => ClearAllData());
|
||||
// }
|
||||
//});
|
||||
//_clearThread.Start();
|
||||
_plcReadTimer?.Stop();
|
||||
}
|
||||
// 清除所有数据
|
||||
private void ClearAllData()
|
||||
|
||||
Reference in New Issue
Block a user