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