This commit is contained in:
@@ -1092,6 +1092,49 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
}
|
||||
}
|
||||
|
||||
//private void ProcessPowerMeterData(string data)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// if (string.IsNullOrEmpty(data)) return;
|
||||
|
||||
// string trimmedLine = data.Trim();
|
||||
// WriteToLog($"处理数据行: '{trimmedLine}'");
|
||||
|
||||
|
||||
// if (double.TryParse(trimmedLine, NumberStyles.Float, CultureInfo.InvariantCulture, out double powerValue))
|
||||
// {
|
||||
|
||||
// uiLabel23.Text = $"{powerValue:F3}";
|
||||
// _waitingResponse = false;
|
||||
// }
|
||||
// else if (trimmedLine.Contains(".") && trimmedLine.Length > 0)
|
||||
// {
|
||||
// string[] parts = trimmedLine.Split(' ');
|
||||
// foreach (string part in parts)
|
||||
// {
|
||||
// if (double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out double power))
|
||||
// {
|
||||
// uiLabel23.Text = $"{power:F3} W";
|
||||
// _waitingResponse = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// WriteToLog($"未解析的数据: {trimmedLine}");
|
||||
// _waitingResponse = false;
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// WriteToLog($"处理功率计数据错误: {ex.Message}");
|
||||
// _waitingResponse = false;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
private void ProcessPowerMeterData(string data)
|
||||
{
|
||||
try
|
||||
@@ -1104,9 +1147,17 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
|
||||
if (double.TryParse(trimmedLine, NumberStyles.Float, CultureInfo.InvariantCulture, out double powerValue))
|
||||
{
|
||||
if (ShouldFilterPowerValue(powerValue))
|
||||
{
|
||||
WriteToLog($"过滤噪声值: {powerValue:F3} ");
|
||||
uiLabel23.Text = "0.000"; // 显示为0
|
||||
}
|
||||
else
|
||||
{
|
||||
uiLabel23.Text = $"{powerValue:F3} W";
|
||||
}
|
||||
|
||||
uiLabel23.Text = $"{powerValue:F3}";
|
||||
_waitingResponse = false;
|
||||
_waitingResponse = false;
|
||||
}
|
||||
else if (trimmedLine.Contains(".") && trimmedLine.Length > 0)
|
||||
{
|
||||
@@ -1115,8 +1166,16 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
{
|
||||
if (double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out double power))
|
||||
{
|
||||
uiLabel23.Text = $"{power:F3} W";
|
||||
_waitingResponse = false;
|
||||
if (ShouldFilterPowerValue(power))
|
||||
{
|
||||
WriteToLog($"过滤噪声值: {power:F3} W");
|
||||
uiLabel23.Text = "0.000";
|
||||
}
|
||||
else
|
||||
{
|
||||
uiLabel23.Text = $"{power:F3} W";
|
||||
}
|
||||
_waitingResponse = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1130,9 +1189,46 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteToLog($"处理功率计数据错误: {ex.Message}");
|
||||
_waitingResponse = false;
|
||||
_waitingResponse = false;
|
||||
}
|
||||
}
|
||||
private bool ShouldFilterPowerValue(double powerValue)
|
||||
{
|
||||
const double NOISE_THRESHOLD = 0.005; // 5mW,根据实际情况调整
|
||||
|
||||
if (Math.Abs(powerValue) < NOISE_THRESHOLD)
|
||||
{
|
||||
return true; // 过滤掉接近0的小噪声
|
||||
}
|
||||
|
||||
|
||||
double[] suspiciousValues = { 2.0, 4.0, 8.0, 16.0, 32.0 };
|
||||
foreach (double suspicious in suspiciousValues)
|
||||
{
|
||||
if (Math.Abs(powerValue - suspicious) < 0.1) // 允许0.1的误差
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (IsPowerOfTwo(powerValue))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsPowerOfTwo(double value)
|
||||
{
|
||||
if (value <= 0) return false;
|
||||
|
||||
int intValue = (int)Math.Round(value);
|
||||
|
||||
|
||||
return (intValue != 0) && ((intValue & (intValue - 1)) == 0);
|
||||
}
|
||||
|
||||
private void PowerMeterTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user