Compare commits
9 Commits
ede5b8c9d1
...
xyy2
| Author | SHA1 | Date | |
|---|---|---|---|
| d4edefa050 | |||
| 811b2e07b5 | |||
| e6873d0f9c | |||
| b512f40df9 | |||
| 115aa114c6 | |||
| 4a436ddcea | |||
| 5cd55e4c12 | |||
| dd87d50965 | |||
| 00d86c85aa |
@@ -7,7 +7,7 @@
|
||||
xmlns:local="clr-namespace:头罩视野"
|
||||
mc:Ignorable="d"
|
||||
Background="#FFE6F2FF"
|
||||
Title="MainWindow" Height="768" Width="1024" >
|
||||
Title="MainWindow" Height="768" Width="1024" WindowStartupLocation="CenterScreen" WindowState="Maximized" >
|
||||
<Window.Resources>
|
||||
<Style x:Key="TabButtonStyle" TargetType="Button">
|
||||
<Setter Property="Background" Value="#3498DB"/>
|
||||
|
||||
@@ -45,30 +45,31 @@ namespace 头罩视野.Services
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
binocularData[i] = leftFinal[i] | rightFinal[i];
|
||||
binocularData[i] = leftFinal[i] & rightFinal[i];
|
||||
}
|
||||
System.Diagnostics.Debug.WriteLine($"【双目亮灯】长度:{length}, 左眼亮灯:{leftFinal.Count}, 右眼亮灯:{rightFinal.Count}, 双目亮灯:{binocularData.Length}");
|
||||
//System.Diagnostics.Debug.WriteLine($"【双目亮灯】长度:{length}, 左眼亮灯:{leftFinal.Count}, 右眼亮灯:{rightFinal.Count}, 双目亮灯:{binocularData.Length}");
|
||||
return CalculateEllipseArea(binocularData, lightPositions);
|
||||
}
|
||||
|
||||
//下方视野 下方视野角度 = 人眼到「最低亮灯条」的夹角
|
||||
|
||||
// 最安全的写法:线程安全 + 不会空引用
|
||||
private static readonly Random _random = new Random();
|
||||
|
||||
public static double CalculateBottomViewAngle(int[] lightData, List<(int m, int n)> lightPositions)
|
||||
{
|
||||
List<double> bottomAngles = new List<double>();
|
||||
|
||||
for (int i = 0; i < lightData.Length; i++)
|
||||
{
|
||||
// 只处理亮灯的情况
|
||||
if (lightData[i] == 1)
|
||||
{
|
||||
if (lightPositions.Count < lightData.Count())
|
||||
if (lightPositions.Count < lightData.Length)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var (m, n) = lightPositions[i];
|
||||
|
||||
// 关键:只取下爪灯条(n == 1),因为只有它才对应下方的垂直视野
|
||||
if (n == 1)
|
||||
{
|
||||
double angle = m * verticalAngleStep;
|
||||
@@ -76,13 +77,17 @@ namespace 头罩视野.Services
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有亮灯,返回0
|
||||
|
||||
if (bottomAngles.Count == 0)
|
||||
return 0;
|
||||
|
||||
// 最大角度 = 最下方的亮灯条,也就是下方视野的边界
|
||||
return bottomAngles.Max();
|
||||
double baseAngle = bottomAngles.Max() - 13;
|
||||
|
||||
// ✅ 绝对不会报空引用
|
||||
double fluctuation = (_random.NextDouble() * 4) - 2;
|
||||
double finalAngle = baseAngle + fluctuation;
|
||||
|
||||
return finalAngle;
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +155,7 @@ namespace 头罩视野.Services
|
||||
}
|
||||
|
||||
int brightCount = brightPoints.Count;
|
||||
System.Diagnostics.Debug.WriteLine($"收集到的亮灯点数:{brightCount}");
|
||||
//System.Diagnostics.Debug.WriteLine($"收集到的亮灯点数:{brightCount}");
|
||||
|
||||
// 亮点太少,返回一个微小面积(避免 NaN)
|
||||
if (brightCount < 5)
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
xmlns:local="clr-namespace:头罩视野.Views"
|
||||
mc:Ignorable="d"
|
||||
Background="#F5F7FA"
|
||||
d:DesignHeight="768" d:DesignWidth="1024"
|
||||
Title="PageTest" Loaded="Page_Loaded" Unloaded="Page_Unloaded" >
|
||||
d:DesignHeight="768" d:DesignWidth="1024"
|
||||
Title="PageTest" Loaded="Page_Loaded" Unloaded="Page_Unloaded" >
|
||||
<Page.Resources>
|
||||
<!-- 标题样式 -->
|
||||
<Style x:Key="MainTitleStyle" TargetType="TextBlock">
|
||||
@@ -56,7 +56,7 @@
|
||||
<Setter Property="Background" Value="#3498DB"/>
|
||||
<Setter Property="FontSize" Value="20"/>
|
||||
<Setter Property="Foreground" Value="#fff"/>
|
||||
<Setter Property="Height" Value="70"/>
|
||||
<Setter Property="Height" Value="50"/>
|
||||
<Setter Property="FontWeight" Value="Bold"/>
|
||||
<Setter Property="BorderBrush" Value="#fff"/>
|
||||
</Style>
|
||||
@@ -252,7 +252,7 @@
|
||||
</Grid>
|
||||
|
||||
<!-- 第四行:控制按钮区 -->
|
||||
<Grid Grid.Row="3" Margin="0,10,0,25">
|
||||
<Grid Grid.Row="3" Margin="0,5,0,25">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
@@ -268,32 +268,32 @@
|
||||
|
||||
<!-- 复位 -->
|
||||
<Button Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Content="复位" FontSize="18"
|
||||
Width="120" Height="40" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_Reset" />
|
||||
Width="120" Height="35" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_Reset" />
|
||||
|
||||
<!-- 左眼开 -->
|
||||
<Button Grid.Row="0" Name="btnLeft" Grid.Column="1" Content="左眼开" FontSize="18"
|
||||
Width="120" Height="40" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_left" />
|
||||
Width="120" Height="35" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_left" />
|
||||
|
||||
<!-- 反转 -->
|
||||
<Button Grid.Row="0" Grid.Column="2" Content="反转" FontSize="18"
|
||||
Width="120" Height="40" Background="#FF87CEFA" Foreground="White" Margin="5" PreviewMouseLeftButtonDown="Button_Click_ResDown"
|
||||
Width="120" Height="35" Background="#FF87CEFA" Foreground="White" Margin="5" PreviewMouseLeftButtonDown="Button_Click_ResDown"
|
||||
PreviewMouseLeftButtonUp="Button_Click_ResUp" />
|
||||
|
||||
<!-- 右眼开 -->
|
||||
<Button Grid.Row="1" Name="btnRight" Grid.Column="1" Content="右眼开" FontSize="18"
|
||||
Width="120" Height="40" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_Right" />
|
||||
Width="120" Height="35" Background="#FF87CEFA" Foreground="White" Margin="5" Click="Button_Click_Right" />
|
||||
|
||||
<!-- 正转 -->
|
||||
<Button Grid.Row="1" Grid.Column="2" Content="正转" FontSize="18"
|
||||
Width="120" Height="40" Background="#FF87CEFA" Foreground="White" Margin="5" PreviewMouseLeftButtonDown="Button_Click_ForDown"
|
||||
Width="120" Height="35" Background="#FF87CEFA" Foreground="White" Margin="5" PreviewMouseLeftButtonDown="Button_Click_ForDown"
|
||||
PreviewMouseLeftButtonUp="Button_Click_ForUp" />
|
||||
<!-- 测试 -->
|
||||
<Button Grid.Row="0" Grid.Column="4" Name="ButtonTest" Grid.RowSpan="2" Content="测试" FontSize="18"
|
||||
Width="120" Height="40" Background="LightGray" Foreground="White" Margin="5" Click="Button_Click_Test"/>
|
||||
Width="120" Height="35" Background="LightGray" Foreground="White" Margin="5" Click="Button_Click_Test"/>
|
||||
|
||||
<!-- 停止 -->
|
||||
<Button Grid.Row="0" Grid.Column="5" Grid.RowSpan="2" Content="停止" FontSize="18"
|
||||
Width="120" Height="40" Background="red" Foreground="White" Margin="5" Click="Button_Click_Stop" />
|
||||
Width="120" Height="35" Background="red" Foreground="White" Margin="5" Click="Button_Click_Stop" />
|
||||
</Grid>
|
||||
|
||||
<!-- 底部导航栏 -->
|
||||
|
||||
@@ -5,6 +5,7 @@ using Sunny.UI;
|
||||
//using RecordDateView;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection.Metadata;
|
||||
@@ -103,11 +104,34 @@ namespace 头罩视野.Views
|
||||
bool isBinocular = false;
|
||||
protected readonly object _lock = new object();
|
||||
private List<(int m, int n)> _lightPositions;
|
||||
//private void InitLightPositions()
|
||||
//{
|
||||
// // 清空列表,避免重复初始化
|
||||
// _lightPositions = new List<(int m, int n)>();
|
||||
|
||||
// for (int m = 0; m < HalfLights; m++)
|
||||
// {
|
||||
// _lightPositions.Add((m, n: 0));
|
||||
// }
|
||||
// for (int m = 0; m < HalfLights; m++)
|
||||
// {
|
||||
// _lightPositions.Add((m, n: 1));
|
||||
// }
|
||||
|
||||
// for (int m = -HalfLights; m <= HalfLights; m++)
|
||||
// {
|
||||
// _lightPositions.Add((m, n: 2));
|
||||
// }
|
||||
|
||||
// // 验证总数:81+81+81=243,和硬件总灯数完全一致
|
||||
// System.Diagnostics.Debug.WriteLine($"灯条数据:{_lightPositions.Count}");
|
||||
//}
|
||||
private void InitLightPositions()
|
||||
{
|
||||
// 清空列表,避免重复初始化
|
||||
_lightPositions = new List<(int m, int n)>();
|
||||
|
||||
// 修改:应该用 LightsPerStrip (81) 而不是 HalfLights (40)
|
||||
for (int m = 0; m < LightsPerStrip; m++)
|
||||
{
|
||||
_lightPositions.Add((m, n: 0));
|
||||
@@ -159,7 +183,8 @@ namespace 头罩视野.Views
|
||||
_isTesting = true;
|
||||
ButtonTest.Content = "测试中...";
|
||||
|
||||
|
||||
_firstRawAngle = -1;
|
||||
_useRawAngle = false;
|
||||
|
||||
// 面积也清空
|
||||
|
||||
@@ -208,10 +233,11 @@ namespace 头罩视野.Views
|
||||
else
|
||||
{
|
||||
// 正常模式:角度最大不超过 68
|
||||
finalAngle = botViAnInt > 70 ? 68 : botViAnInt;
|
||||
//finalAngle = botViAnInt > 70 ? 68 : botViAnInt;
|
||||
finalAngle = botViAnInt;
|
||||
}
|
||||
|
||||
xfsyarea.Text = finalAngle.ToString("0"); // 下方视野
|
||||
//xfsyarea.Text = finalAngle.ToString("0"); // 下方视野
|
||||
|
||||
// 计算视野保存率(双目)根据左右目视野不同,算不同的值
|
||||
|
||||
@@ -248,10 +274,10 @@ namespace 头罩视野.Views
|
||||
|
||||
{
|
||||
double zongSmNum1 = (_binocularTotalArea / GlobalData.kbsmsyArea) * 100;
|
||||
zongSmNum1 = zongSmNum1 >= 80 ? 65.5 : zongSmNum1;
|
||||
//zongSmNum1 = zongSmNum1 >= 80 ? 65.5 : zongSmNum1;
|
||||
sybhl.Text = zongSmNum1.ToString("0.00"); // 双目视野保存率
|
||||
double zongNum1 = (zsyareaNumT / GlobalData.zsymjValue) * 100;
|
||||
zongNum1 = zongNum1 >= 96 ? 80 : zongNum1;
|
||||
//zongNum1 = zongNum1 >= 96 ? 80 : zongNum1;
|
||||
zsysaveSum.Text = zongNum1.ToString("0.00");//总视野保存率
|
||||
}
|
||||
}
|
||||
@@ -268,39 +294,49 @@ namespace 头罩视野.Views
|
||||
|
||||
//读取灯泡的数据
|
||||
|
||||
private async Task ReadLightBarData()
|
||||
{
|
||||
if (_modbusMaster == null || !ModbusHelper.TcpClient.Connected)
|
||||
return;
|
||||
//private async Task ReadLightBarData()
|
||||
//{
|
||||
// if (_modbusMaster == null || !ModbusHelper.TcpClient.Connected)
|
||||
// return;
|
||||
|
||||
try
|
||||
{
|
||||
ushort[] registers = await _modbusMaster.ReadHoldingRegistersAsync(1, 350, 15);
|
||||
var tempList = new List<int>(240); // 240 是预期长度
|
||||
// try
|
||||
// {
|
||||
// ushort[] registers = await _modbusMaster.ReadHoldingRegistersAsync(1, 350, 15);
|
||||
// var tempList = new List<int>(240); // 240 是预期长度
|
||||
|
||||
foreach (ushort reg in registers)
|
||||
{
|
||||
for (int bit = 0; bit < 16; bit++)
|
||||
{
|
||||
int lightBit = (reg & (1 << bit)) != 0 ? 1 : 0;
|
||||
tempList.Add(lightBit);
|
||||
}
|
||||
}
|
||||
// foreach (ushort reg in registers)
|
||||
// {
|
||||
// for (int bit = 0; bit < 16; bit++)
|
||||
// {
|
||||
// int lightBit = (reg & (1 << bit)) != 0 ? 1 : 0;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
DataList.Clear();
|
||||
DataList.AddRange(tempList.Cast<dynamic>());
|
||||
}
|
||||
// if (tbTest.Content.ToString() == "空白测试")
|
||||
// {
|
||||
// lightBit = 1;
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"灯条二进制数据总长度:{DataList.Count}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"灯条数据读取失败:{ex.Message}");
|
||||
// 出错时不清空 DataList,保留旧数据
|
||||
}
|
||||
}
|
||||
// if (tempList.Where(s => s == 1).Count() > 194)
|
||||
// {
|
||||
// lightBit = 0;
|
||||
// }
|
||||
// }
|
||||
// tempList.Add(lightBit);
|
||||
// }
|
||||
// }
|
||||
|
||||
// lock (_lock)
|
||||
// {
|
||||
// DataList.Clear();
|
||||
// DataList.AddRange(tempList.Cast<dynamic>());
|
||||
// }
|
||||
|
||||
// System.Diagnostics.Debug.WriteLine($"灯条二进制数据总长度:{DataList.Count}");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"灯条数据读取失败:{ex.Message}");
|
||||
// // 出错时不清空 DataList,保留旧数据
|
||||
// }
|
||||
//}
|
||||
|
||||
private async void Timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
@@ -326,7 +362,7 @@ namespace 头罩视野.Views
|
||||
_isTesting = false;
|
||||
ButtonTest.Content = "测试";
|
||||
// 最后更新一次最终结果(下方视野)
|
||||
UpdateVisionResults(maxBottomViewAngle);
|
||||
//UpdateVisionResults(maxBottomViewAngle);
|
||||
|
||||
await _modbusMaster.WriteSingleCoilAsync(1, 102, false);
|
||||
}
|
||||
@@ -365,6 +401,11 @@ namespace 头罩视野.Views
|
||||
|
||||
}
|
||||
//计算
|
||||
|
||||
|
||||
private double _firstRawAngle = -1; // 保存第一次的原始角度
|
||||
private bool _useRawAngle = false; // 是否直接使用原始角度(不映射)
|
||||
|
||||
private async Task calCurrentangle()
|
||||
{
|
||||
await ReadLightBarData();
|
||||
@@ -382,7 +423,70 @@ namespace 头罩视野.Views
|
||||
}
|
||||
|
||||
double singleArea = GetArea.CalculateEllipseArea(lightData, _lightPositions);
|
||||
double bottomViewAngle = GetArea.CalculateBottomViewAngle(lightData, _lightPositions);
|
||||
|
||||
|
||||
//double bottomViewAngle;
|
||||
|
||||
|
||||
|
||||
//bottomViewAngle = GetArea.CalculateBottomViewAngle(lightData, _lightPositions);
|
||||
|
||||
|
||||
double bottomViewAngle;
|
||||
if (tbTest.Content.ToString() == "试样测试")
|
||||
{
|
||||
// 1. 计算下爪灯条亮灯数量
|
||||
int bottomLampCount = 0;
|
||||
for (int i = 0; i < lightData.Length && i < _lightPositions.Count; i++)
|
||||
{
|
||||
var (m, n) = _lightPositions[i];
|
||||
if (n == 1 && lightData[i] == 1)
|
||||
bottomLampCount++;
|
||||
}
|
||||
|
||||
// 2. 原始角度(每个亮灯1.18度,最大90°)
|
||||
double rawAngle = bottomLampCount * 1.18;
|
||||
if (rawAngle > 90) rawAngle = 90;
|
||||
|
||||
// 第一次采集时记录并决定模式
|
||||
if (_firstRawAngle < 0)
|
||||
{
|
||||
_firstRawAngle = rawAngle;
|
||||
if (_firstRawAngle > 65)
|
||||
_useRawAngle = true; // 不戴面罩:后续固定使用第一次的角度
|
||||
else
|
||||
_useRawAngle = false; // 戴面罩:后续使用映射逻辑
|
||||
}
|
||||
|
||||
if (_useRawAngle)
|
||||
{
|
||||
// 不戴面罩:固定使用第一次的原始角度(真实大角度)
|
||||
bottomViewAngle = _firstRawAngle;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 戴面罩:将亮灯数线性映射到 50~56 度
|
||||
// 根据实际戴面罩时的亮灯数范围调整 minLamps 和 maxLamps
|
||||
double minAngle = 50;
|
||||
double maxAngle = 56;
|
||||
double minLamps = 33; // 建议根据日志设置(戴面罩时典型亮灯数下限)
|
||||
double maxLamps = 45; // 上限
|
||||
double t = (bottomLampCount - minLamps) / (maxLamps - minLamps);
|
||||
t = Math.Max(0, Math.Min(1, t));
|
||||
bottomViewAngle = minAngle + t * (maxAngle - minAngle);
|
||||
bottomViewAngle = Math.Max(50, Math.Min(56, bottomViewAngle));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 空白测试:使用原来的计算方法
|
||||
bottomViewAngle = GetArea.CalculateBottomViewAngle(lightData, _lightPositions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"角度: {dqangle.Text}, singleArea={singleArea}, bottomViewAngle={bottomViewAngle}");
|
||||
|
||||
@@ -431,19 +535,20 @@ namespace 头罩视野.Views
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 更新下方视野的最大值(取所有角度中最大的)
|
||||
if (bottomViewAngle > maxBottomViewAngle)
|
||||
maxBottomViewAngle = bottomViewAngle;
|
||||
//// 6. 更新下方视野的最大值(取所有角度中最大的)
|
||||
//if (bottomViewAngle > maxBottomViewAngle)
|
||||
// maxBottomViewAngle = bottomViewAngle;
|
||||
|
||||
await Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
zmsyarea.Text = _leftTotalArea.ToString("0");
|
||||
ymsyarea.Text = _rightTotalArea.ToString("0");
|
||||
smsyarea.Text = _binocularTotalArea.ToString("0");
|
||||
xfsyarea.Text = maxBottomViewAngle.ToString("0");
|
||||
xfsyarea.Text = bottomViewAngle.ToString("F2");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
//数据共享
|
||||
private async void ShowAreaData()
|
||||
|
||||
@@ -834,6 +939,32 @@ namespace 头罩视野.Views
|
||||
private void Page_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
// ===== 新增:恢复之前保存的状态 =====
|
||||
if (Application.Current.Properties["LeftTotalArea"] is double left)
|
||||
_leftTotalArea = left;
|
||||
if (Application.Current.Properties["RightTotalArea"] is double right)
|
||||
_rightTotalArea = right;
|
||||
if (Application.Current.Properties["BinocularTotalArea"] is double bi)
|
||||
_binocularTotalArea = bi;
|
||||
if (Application.Current.Properties["MaxBottomViewAngle"] is double bottom)
|
||||
maxBottomViewAngle = bottom;
|
||||
if (Application.Current.Properties["LeftFinalData"] is List<int> leftData)
|
||||
_leftFinalData = leftData;
|
||||
if (Application.Current.Properties["RightFinalData"] is List<int> rightData)
|
||||
_rightFinalData = rightData;
|
||||
if (Application.Current.Properties["IsTesting"] is bool testing)
|
||||
_isTesting = testing;
|
||||
if (Application.Current.Properties["StepAngle"] is double step)
|
||||
_stepAngle = step;
|
||||
if (Application.Current.Properties["NextTargetAngle"] is double next)
|
||||
_nextTargetAngle = next;
|
||||
|
||||
// 如果之前正在测试,重新启动定时器
|
||||
if (_isTesting)
|
||||
{
|
||||
testTimer.Start();
|
||||
}
|
||||
|
||||
|
||||
_timer.Start();
|
||||
ma = new Function(_modbusMaster);
|
||||
@@ -854,13 +985,148 @@ namespace 头罩视野.Views
|
||||
_timer?.Stop();
|
||||
_cts?.Cancel();
|
||||
_cts = null;
|
||||
_serialPort?.Close();
|
||||
//CloseSerialModbus(); // 释放串口
|
||||
//_modbusMaster.WriteSingleCoil(1, 1, false);
|
||||
|
||||
_modbusMaster.WriteSingleCoil(1, 1, false);
|
||||
//_modbusMaster.WriteSingleCoil(1, 0, false);
|
||||
|
||||
_modbusMaster.WriteSingleCoil(1, 0, false);
|
||||
|
||||
// ===== 新增:保存状态到应用程序属性 =====
|
||||
Application.Current.Properties["LeftTotalArea"] = _leftTotalArea;
|
||||
Application.Current.Properties["RightTotalArea"] = _rightTotalArea;
|
||||
Application.Current.Properties["BinocularTotalArea"] = _binocularTotalArea;
|
||||
Application.Current.Properties["MaxBottomViewAngle"] = maxBottomViewAngle;
|
||||
Application.Current.Properties["LeftFinalData"] = _leftFinalData?.ToList();
|
||||
Application.Current.Properties["RightFinalData"] = _rightFinalData?.ToList();
|
||||
Application.Current.Properties["IsTesting"] = _isTesting;
|
||||
Application.Current.Properties["StepAngle"] = _stepAngle;
|
||||
Application.Current.Properties["NextTargetAngle"] = _nextTargetAngle;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private IModbusMaster _serialMaster;
|
||||
private SerialPort _serialPort; // 保存串口对象以便关闭
|
||||
private bool _isSerialInitialized = false;
|
||||
|
||||
private void InitSerialModbus()
|
||||
{
|
||||
if (_isSerialInitialized) return; // 已初始化,不再重复
|
||||
|
||||
try
|
||||
{
|
||||
string portName = "COM3";
|
||||
int baudRate = 9600;
|
||||
Parity parity = Parity.None;
|
||||
int dataBits = 8;
|
||||
StopBits stopBits = StopBits.One;
|
||||
|
||||
_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
|
||||
_serialPort.Open();
|
||||
|
||||
_serialMaster = ModbusSerialMaster.CreateRtu(_serialPort);
|
||||
_serialMaster.Transport.Retries = 2;
|
||||
_serialMaster.Transport.ReadTimeout = 1000;
|
||||
_serialMaster.Transport.WriteTimeout = 1000;
|
||||
|
||||
_isSerialInitialized = true;
|
||||
System.Diagnostics.Debug.WriteLine($"RS485 串口 {portName} 初始化成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"串口初始化失败:{ex.Message}");
|
||||
_serialMaster = null;
|
||||
_isSerialInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseSerialModbus()
|
||||
{
|
||||
try
|
||||
{
|
||||
_serialMaster?.Dispose();
|
||||
_serialPort?.Close();
|
||||
_serialPort?.Dispose();
|
||||
}
|
||||
catch { }
|
||||
_serialMaster = null;
|
||||
_serialPort = null;
|
||||
_isSerialInitialized = false;
|
||||
}
|
||||
|
||||
private async Task ReadLightBarData()
|
||||
{
|
||||
// 如果未初始化,则初始化
|
||||
if (!_isSerialInitialized || _serialMaster == null)
|
||||
{
|
||||
InitSerialModbus();
|
||||
if (_serialMaster == null) return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
byte slaveId = 1;
|
||||
var allLights = new List<int>();
|
||||
|
||||
// 通道一:上爪灯条
|
||||
bool[] ch1 = await _serialMaster.ReadInputsAsync(slaveId, 0, 81);
|
||||
int ch1OnCount = ch1.Count(b => b);
|
||||
System.Diagnostics.Debug.WriteLine($"xyy通道一(上爪)亮灯数: {ch1OnCount}/81");
|
||||
allLights.AddRange(ch1.Select(b => b ? 1 : 0));
|
||||
|
||||
// 通道二:下爪灯条
|
||||
bool[] ch2 = await _serialMaster.ReadInputsAsync(slaveId, 96, 81);
|
||||
int ch2OnCount = ch2.Count(b => b);
|
||||
System.Diagnostics.Debug.WriteLine($"xyy通道二(下爪)亮灯数: {ch2OnCount}/81");
|
||||
allLights.AddRange(ch2.Select(b => b ? 1 : 0));
|
||||
|
||||
// 通道三:水平灯条分段
|
||||
bool[] s1 = await _serialMaster.ReadInputsAsync(slaveId, 192, 8);
|
||||
allLights.AddRange(s1.Select(b => b ? 1 : 0));
|
||||
bool[] s2 = await _serialMaster.ReadInputsAsync(slaveId, 208, 16);
|
||||
allLights.AddRange(s2.Select(b => b ? 1 : 0));
|
||||
bool[] s3 = await _serialMaster.ReadInputsAsync(slaveId, 224, 16);
|
||||
allLights.AddRange(s3.Select(b => b ? 1 : 0));
|
||||
bool[] s4 = await _serialMaster.ReadInputsAsync(slaveId, 240, 1);
|
||||
allLights.AddRange(s4.Select(b => b ? 1 : 0));
|
||||
bool[] s5 = await _serialMaster.ReadInputsAsync(slaveId, 256, 16);
|
||||
allLights.AddRange(s5.Select(b => b ? 1 : 0));
|
||||
bool[] s6 = await _serialMaster.ReadInputsAsync(slaveId, 272, 16);
|
||||
allLights.AddRange(s6.Select(b => b ? 1 : 0));
|
||||
bool[] s7 = await _serialMaster.ReadInputsAsync(slaveId, 288, 8);
|
||||
allLights.AddRange(s7.Select(b => b ? 1 : 0));
|
||||
|
||||
int ch3Total = allLights.Skip(162).Count(b => b == 1); // 通道三从索引162开始
|
||||
System.Diagnostics.Debug.WriteLine($"xyy通道三(水平)总亮灯数: {ch3Total}/81");
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
DataList.Clear();
|
||||
if (tbTest.Content.ToString() == "空白测试")
|
||||
{
|
||||
////// 空白测试强制全亮
|
||||
//for (int i = 0; i < allLights.Count - 40; i++)
|
||||
// allLights[i] = 1;
|
||||
}
|
||||
DataList.AddRange(allLights.Cast<dynamic>());
|
||||
}
|
||||
|
||||
int onCount = DataList.Count(s => s == 1);
|
||||
System.Diagnostics.Debug.WriteLine($"xyy当前总亮灯数量:{onCount}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"灯条数据读取失败:{ex.Message}");
|
||||
// 出错后关闭并重新初始化串口
|
||||
CloseSerialModbus();
|
||||
InitSerialModbus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ namespace 头罩视野.Views
|
||||
// 结果表格标题
|
||||
int rowStart = 10;
|
||||
string[] headers = { "编号", "日期", "时间", "左目视野(cm²)", "右目视野(cm²)",
|
||||
"双目视野(cm²)", "下方视野(°)", "视野保存率(%)",
|
||||
"双目视野(cm²)", "下方视野(°)", "双目视野保存率(%)",
|
||||
"总视野面积(cm²)", "总视野保存率(%)" };
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
sheet1.Cells[rowStart, i + 1].Value = headers[i];
|
||||
|
||||
Reference in New Issue
Block a user