移除DeviceManager.cs中多余的_isReceivingTemperatureData变量,优化代码结构
This commit is contained in:
@@ -80,68 +80,58 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析原始温度数据,支持根据数据长度自适应宽高
|
||||
/// </summary>
|
||||
/// <param name="rawData">原始温度数据</param>
|
||||
/// <param name="compensationValue">温度补偿值</param>
|
||||
private void ParseRawData(byte[] rawData, float compensationValue)
|
||||
/// 解析原始温度数据
|
||||
/// </summary>
|
||||
/// <param name="rawData">原始温度数据</param>
|
||||
/// <param name="compensationValue">温度补偿值</param>
|
||||
private void ParseRawData(byte[] rawData, float compensationValue)
|
||||
{
|
||||
// 检查数据是否有效
|
||||
if (rawData == null || rawData.Length == 0)
|
||||
{
|
||||
// 根据SDK实际实现,温度数据格式为:
|
||||
// 9字节头部 (+TEMP + 4字节长度) + 低字节数据 + 高字节数据
|
||||
// 温度计算公式:(低字节 + 高字节*256 - 2730)/10
|
||||
throw new ArgumentNullException(nameof(rawData), "原始温度数据为空");
|
||||
}
|
||||
|
||||
// 根据SDK文档要求,使用24字节头部
|
||||
const int HEADER_SIZE = 24;
|
||||
|
||||
// 确保数据长度足够(至少包含头部)
|
||||
if (rawData.Length < HEADER_SIZE)
|
||||
{
|
||||
throw new ArgumentException("数据长度不足,无法解析");
|
||||
}
|
||||
|
||||
// 计算温度数据长度和像素总数
|
||||
int dataLength = rawData.Length - HEADER_SIZE;
|
||||
int pixelCount = dataLength / 2; // 每个像素2字节
|
||||
|
||||
// 计算最大可处理的像素数
|
||||
int maxPixels = Math.Min(pixelCount, Width * Height);
|
||||
|
||||
// 跳过24字节头部,按照行优先顺序解析温度数据
|
||||
for (int i = 0; i < maxPixels; i++)
|
||||
{
|
||||
// 计算行列索引
|
||||
int row = i / Width;
|
||||
int col = i % Width;
|
||||
|
||||
// 首先检查数据长度是否足够包含头部信息
|
||||
if (rawData.Length < 9)
|
||||
if (row < Height && col < Width)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("警告: 原始数据长度小于9字节,无法包含有效的头部信息");
|
||||
throw new ArgumentException("原始数据长度不足以包含头部信息");
|
||||
}
|
||||
|
||||
// 验证头部标识字符是否为"+TEMP"
|
||||
string headerMark = Encoding.ASCII.GetString(rawData, 0, 5).TrimEnd('\0');
|
||||
if (headerMark != "+TEMP")
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"警告: 头部标识不匹配,期望'+TEMP',实际为'{headerMark}'");
|
||||
// 即使头部不匹配,我们也尝试继续处理,因为有些情况下数据可能没有标准头部
|
||||
}
|
||||
|
||||
// 计算像素总数 - 根据SDK实现,数据长度应该是:头部(9字节) + 像素数*2
|
||||
int totalDataSize = rawData.Length - 9; // 减去9字节头部
|
||||
int pixelCount = totalDataSize / 2; // 每个像素点使用2字节(低字节和高字节分开存储)
|
||||
|
||||
// 根据像素数确定宽高 - 使用标准分辨率
|
||||
int[] widthHeight = GetWidthHeightByPixelCount(pixelCount);
|
||||
int adaptiveWidth = widthHeight[0];
|
||||
int adaptiveHeight = widthHeight[1];
|
||||
|
||||
// 输出自适应宽高信息
|
||||
System.Diagnostics.Debug.WriteLine($"根据数据长度自适应宽高: {adaptiveWidth}x{adaptiveHeight} (像素数: {pixelCount})");
|
||||
|
||||
// 更新宽高并初始化矩阵
|
||||
Width = adaptiveWidth;
|
||||
Height = adaptiveHeight;
|
||||
TemperatureMatrix = new float[Height, Width];
|
||||
|
||||
// 计算最大可处理的像素数
|
||||
int maxPixels = Math.Min(pixelCount, Width * Height);
|
||||
|
||||
// 按照SDK实现的数据布局处理:低字节在前半部分,高字节在后半部分
|
||||
int lowByteOffset = 9; // 低字节数据起始位置(跳过9字节头部)
|
||||
int highByteOffset = 9 + pixelCount; // 高字节数据起始位置(在低字节数据之后)
|
||||
|
||||
// 遍历所有像素点,按照SDK的方式计算温度
|
||||
for (int i = 0; i < maxPixels; i++)
|
||||
{
|
||||
// 计算行列索引
|
||||
int row = i / Width;
|
||||
int col = i % Width;
|
||||
// 计算数据偏移量(跳过24字节头部)
|
||||
int offset = HEADER_SIZE + i * 2;
|
||||
|
||||
if (row < Height && col < Width)
|
||||
if (offset + 1 < rawData.Length)
|
||||
{
|
||||
// 按照SDK实现计算温度值:(低字节 + 高字节*256 - 2730)/10
|
||||
int lowByte = rawData[lowByteOffset + i];
|
||||
int highByte = rawData[highByteOffset + i];
|
||||
float rawTemperature = (lowByte + highByte * 256 - 2730) / 10.0f;
|
||||
// 根据SDK文档要求,温度值计算方法:(H×256+L)/10,单位为摄氏度
|
||||
// H为高8位,L为低8位
|
||||
int highByte = rawData[offset + 1]; // 高8位
|
||||
int lowByte = rawData[offset]; // 低8位
|
||||
int tempValue = (highByte << 8) | lowByte;
|
||||
|
||||
// 计算实际温度值:(H×256+L)/10
|
||||
float rawTemperature = tempValue / 10.0f;
|
||||
|
||||
// 应用温度补偿值
|
||||
float compensatedTemperature = rawTemperature + compensationValue;
|
||||
|
||||
// 原始温度存入TemperatureMatrix(未温补)
|
||||
@@ -152,6 +142,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将原始温度数据映射到512×384的实际温度矩阵
|
||||
|
||||
Reference in New Issue
Block a user