修复DeviceManager.cs中的IsValidHeaderMarker函数和FindHeaderPosition函数逻辑

This commit is contained in:
zqm
2025-11-04 14:18:11 +08:00
parent 365bbb7ecc
commit d9cd0518ca
2 changed files with 32 additions and 12 deletions

View File

@@ -4,8 +4,8 @@
3. 还原时,恢复到最大化前的位置和大小。 3. 还原时,恢复到最大化前的位置和大小。
4. 关闭时,从父容器中移除。 4. 关闭时,从父容器中移除。
5. 拖拽时,允许在父容器内移动,不允许超出父容器边界。 5. 拖拽时,允许在父容器内移动,不允许超出父容器边界。
6. 当内容区只包含一个Panel时显示Area的标题栏。 6. 当内容区只包含一个Panel时显示Area的标题栏。
7. 当内容区只包含一个Panel时拖动Panel标题栏可以移动Area。 7. 当内容区只包含一个Panel时拖动Panel标题栏就相当于拖动Area。
### Panel ### Panel

View File

@@ -1269,7 +1269,8 @@ namespace JoyD.Windows.CS.Toprie
// 检测暂停状态是否发生变化 // 检测暂停状态是否发生变化
bool currentPaused = IsTemperatureReceivingPaused(); bool currentPaused = IsTemperatureReceivingPaused();
if (currentPaused != lastPaused) bool pausedChanged = currentPaused != lastPaused;
if (pausedChanged)
{ {
if (currentPaused) if (currentPaused)
{ {
@@ -1289,12 +1290,14 @@ namespace JoyD.Windows.CS.Toprie
{ {
// 减少暂停状态下的日志记录频率,避免日志过于冗长 // 减少暂停状态下的日志记录频率,避免日志过于冗长
DateTime currentTime = DateTime.Now; DateTime currentTime = DateTime.Now;
if (pausedChanged)
{
if ((currentTime - lastPausedLogTime).TotalMilliseconds > PAUSED_LOG_INTERVAL_MS) if ((currentTime - lastPausedLogTime).TotalMilliseconds > PAUSED_LOG_INTERVAL_MS)
{ {
Log("温度接收已暂停,等待恢复"); Log("温度接收已暂停,等待恢复");
lastPausedLogTime = currentTime; lastPausedLogTime = currentTime;
} }
}
// 在暂停状态的Sleep前检查是否收到停止信号 // 在暂停状态的Sleep前检查是否收到停止信号
if (ShouldStop()) if (ShouldStop())
{ {
@@ -1320,7 +1323,7 @@ namespace JoyD.Windows.CS.Toprie
else else
{ {
// 只有从暂停状态恢复时状态从true变为false的瞬间才检查和重建连接 // 只有从暂停状态恢复时状态从true变为false的瞬间才检查和重建连接
if (lastPaused && !isPaused && localTcpClient != null && !IsTcpClientConnected(localTcpClient)) if (pausedChanged && localTcpClient != null && !IsTcpClientConnected(localTcpClient))
{ {
Log("恢复接收时检测到连接无效,需要重建连接"); Log("恢复接收时检测到连接无效,需要重建连接");
CleanupConnectionResources(localStream, localTcpClient, out localStream, out localTcpClient); CleanupConnectionResources(localStream, localTcpClient, out localStream, out localTcpClient);
@@ -1329,7 +1332,7 @@ namespace JoyD.Windows.CS.Toprie
} }
// 根据SDK文档建立TCP连接后不需要发送任何开始命令 // 根据SDK文档建立TCP连接后不需要发送任何开始命令
// 从暂停状态恢复时,只需继续监听数据流即可 // 从暂停状态恢复时,只需继续监听数据流即可
if (lastPaused && !isPaused) if (pausedChanged)
{ {
Log("从暂停状态恢复,继续接收温度数据"); Log("从暂停状态恢复,继续接收温度数据");
} }
@@ -1494,11 +1497,22 @@ namespace JoyD.Windows.CS.Toprie
private const int HEIGHT = 192; private const int HEIGHT = 192;
/// <param name="dataAccumulator">累积的温度数据包列表</param> /// <param name="dataAccumulator">累积的温度数据包列表</param>
// 重入保护标志
private bool _isProcessingTemperatureData = false;
private void ProcessReceivedTemperatureData(List<byte[]> dataAccumulator) private void ProcessReceivedTemperatureData(List<byte[]> dataAccumulator)
{ {
// 检查是否已经在处理中,防止重入
if (_isProcessingTemperatureData)
{
Log($"检测到ProcessReceivedTemperatureData重入跳过此次调用");
return;
}
try try
{ {
// 设置重入保护标志
_isProcessingTemperatureData = true;
Log($"开始处理温度数据,当前累积数据包数量: {dataAccumulator.Count}"); Log($"开始处理温度数据,当前累积数据包数量: {dataAccumulator.Count}");
// 按照README中温度数据处理逻辑执行 // 按照README中温度数据处理逻辑执行
@@ -1619,6 +1633,12 @@ namespace JoyD.Windows.CS.Toprie
Log("清空温度数据累积器,准备重新接收"); Log("清空温度数据累积器,准备重新接收");
dataAccumulator.Clear(); dataAccumulator.Clear();
} }
finally
{
// 重置重入保护标志
_isProcessingTemperatureData = false;
Log($"温度数据处理完成,重入保护标志已重置");
}
} }
/// <summary> /// <summary>
@@ -1637,7 +1657,7 @@ namespace JoyD.Windows.CS.Toprie
endIndex = data.Length; endIndex = data.Length;
// 确保有足够的数据检查头信息 // 确保有足够的数据检查头信息
int searchEnd = endIndex - 4; // 至少需要4个字节检查标记 int searchEnd = endIndex - 5; // 至少需要5个字节检查标记("+TEMP")
if (searchEnd < startIndex) if (searchEnd < startIndex)
return -1; return -1;
@@ -1727,12 +1747,12 @@ namespace JoyD.Windows.CS.Toprie
if (data == null || position + 5 > data.Length) if (data == null || position + 5 > data.Length)
return false; return false;
// 验证"+TEMP"标记 // 验证"+TEMP"标记5个字符
return data[position] == '+' && return data[position] == '+' &&
data[position + 1] == 'T' && data[position + 1] == 'T' &&
data[position + 2] == 'E' && data[position + 2] == 'E' &&
data[position + 3] == 'M' && data[position + 3] == 'M' &&
data[position + 4] == 0; // 假设第5个字节是结束符 data[position + 4] == 'P'; // 第5个字节应为'P'
} }
/// <summary> /// <summary>