修复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

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