修复DeviceManager.cs中的语法错误和缩进问题
This commit is contained in:
@@ -1009,7 +1009,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
Log("温度接收线程已正常停止");
|
Log("温度接收线程已正常停止");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log("警告:温度接收线程可能未正常停止,已超时");
|
Log("警告:温度接收线程可能未正常停止,已超时");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1185,60 +1185,60 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有数据可读,避免阻塞
|
// 检查是否有数据可读,避免阻塞
|
||||||
if (localStream.DataAvailable)
|
if (localStream.DataAvailable)
|
||||||
{
|
|
||||||
// 读取数据
|
|
||||||
int bytesRead = localStream.Read(buffer, 0, buffer.Length);
|
|
||||||
if (bytesRead > 0)
|
|
||||||
{
|
{
|
||||||
// 将读取的数据添加到累积器
|
// 读取数据
|
||||||
byte[] receivedBytes = new byte[bytesRead];
|
int bytesRead = localStream.Read(buffer, 0, buffer.Length);
|
||||||
Array.Copy(buffer, receivedBytes, bytesRead);
|
if (bytesRead > 0)
|
||||||
|
|
||||||
Log($"接收到温度数据字节数: {bytesRead}");
|
|
||||||
|
|
||||||
// 线程安全地更新累积器或直接处理
|
|
||||||
lock (temperatureDataAccumulator)
|
|
||||||
{
|
{
|
||||||
temperatureDataAccumulator.AddRange(receivedBytes);
|
// 将读取的数据添加到累积器
|
||||||
ProcessReceivedTemperatureData(temperatureDataAccumulator);
|
byte[] receivedBytes = new byte[bytesRead];
|
||||||
|
Array.Copy(buffer, receivedBytes, bytesRead);
|
||||||
|
|
||||||
|
Log($"接收到温度数据字节数: {bytesRead}");
|
||||||
|
|
||||||
|
// 线程安全地更新累积器或直接处理
|
||||||
|
lock (temperatureDataAccumulator)
|
||||||
|
{
|
||||||
|
temperatureDataAccumulator.AddRange(receivedBytes);
|
||||||
|
ProcessReceivedTemperatureData(temperatureDataAccumulator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 读取到0字节表示连接已关闭
|
||||||
|
Log("远程主机关闭了连接");
|
||||||
|
shouldContinue = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 读取到0字节表示连接已关闭
|
// 如果没有数据可读,短暂休眠避免CPU占用过高
|
||||||
Log("远程主机关闭了连接");
|
Thread.Sleep(10);
|
||||||
shouldContinue = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 如果没有数据可读,短暂休眠避免CPU占用过高
|
|
||||||
Thread.Sleep(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 定期检查连接状态,确保连接仍然有效
|
// 定期检查连接状态,确保连接仍然有效
|
||||||
if (DateTime.Now - lastConnectionCheckTime > TimeSpan.FromSeconds(30))
|
if (DateTime.Now - lastConnectionCheckTime > TimeSpan.FromSeconds(30))
|
||||||
{
|
|
||||||
Log("定期检查温度数据连接状态");
|
|
||||||
// 尝试发送一个简单的心跳消息到TCP连接
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (localStream != null && localStream.CanWrite)
|
Log("定期检查温度数据连接状态");
|
||||||
|
// 尝试发送一个简单的心跳消息到TCP连接
|
||||||
|
try
|
||||||
{
|
{
|
||||||
byte[] heartbeat = Encoding.ASCII.GetBytes("heartbeat\r\n");
|
if (localStream != null && localStream.CanWrite)
|
||||||
localStream.Write(heartbeat, 0, heartbeat.Length);
|
{
|
||||||
localStream.Flush();
|
byte[] heartbeat = Encoding.ASCII.GetBytes("heartbeat\r\n");
|
||||||
Log("温度数据连接心跳发送成功");
|
localStream.Write(heartbeat, 0, heartbeat.Length);
|
||||||
|
localStream.Flush();
|
||||||
|
Log("温度数据连接心跳发送成功");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log($"温度数据连接心跳发送失败: {ex.Message},可能需要重新连接");
|
||||||
|
}
|
||||||
|
lastConnectionCheckTime = DateTime.Now;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log($"温度数据连接心跳发送失败: {ex.Message},可能需要重新连接");
|
|
||||||
}
|
|
||||||
lastConnectionCheckTime = DateTime.Now;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (TimeoutException)
|
catch (TimeoutException)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user