修复TemperatureData.cs中的头部信息处理,添加验证逻辑
This commit is contained in:
@@ -12,11 +12,17 @@ namespace JoyD.Windows.CS.Toprie
|
||||
public class TemperatureData
|
||||
{
|
||||
/// <summary>
|
||||
/// 温度数据矩阵
|
||||
/// 原始温度数据矩阵(未温补)
|
||||
/// 格式: [行][列]
|
||||
/// </summary>
|
||||
public float[,] TemperatureMatrix { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实际温度数据矩阵(温补后的512×384矩阵)
|
||||
/// 格式: [行][列]
|
||||
/// </summary>
|
||||
public float[,] RealTemperatureMatrix { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 温度数据的宽度(列数)
|
||||
/// </summary>
|
||||
@@ -65,6 +71,8 @@ namespace JoyD.Windows.CS.Toprie
|
||||
Width = width;
|
||||
Height = height;
|
||||
TemperatureMatrix = new float[height, width];
|
||||
// 创建512×384的实际温度矩阵
|
||||
RealTemperatureMatrix = new float[384, 512];
|
||||
Timestamp = DateTime.Now;
|
||||
|
||||
ParseRawData(rawData, compensationValue);
|
||||
@@ -78,52 +86,82 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// <param name="compensationValue">温度补偿值</param>
|
||||
private void ParseRawData(byte[] rawData, float compensationValue)
|
||||
{
|
||||
// 按照SDK文档,温度数据为每个点2字节,摄氏温度=(H*256+L)/10
|
||||
// 需要考虑数据分布逻辑:先发送第一行奇数列,再发送第一行偶数列,接着第二行奇数列,依此类推
|
||||
int dataIndex = 0;
|
||||
// 按照SDK文档,温度数据格式为:24字节头部 + 温度数据
|
||||
// 温度数据为每个点2字节,摄氏温度=(H*256+L)/10
|
||||
// 温度数据是连续发送的,不需要分开处理奇偶数行
|
||||
|
||||
// 处理奇数列
|
||||
// 首先检查数据长度是否足够包含头部信息
|
||||
if (rawData.Length < 24)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("警告: 原始数据长度小于24字节,无法包含有效的头部信息");
|
||||
throw new ArgumentException("原始数据长度不足以包含头部信息");
|
||||
}
|
||||
|
||||
// 验证头部信息
|
||||
if (!ValidateHeader(rawData))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("警告: 温度数据头部信息验证失败,停止解析");
|
||||
throw new ArgumentException("温度数据头部信息无效");
|
||||
}
|
||||
|
||||
// 跳过24字节的头部信息
|
||||
int dataIndex = 24;
|
||||
|
||||
// 确保跳过头部后仍有足够的数据用于解析
|
||||
int requiredDataLength = Width * Height * 2; // 每个温度点2字节
|
||||
if (rawData.Length - dataIndex < requiredDataLength)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"警告: 数据长度不足,需要{requiredDataLength}字节温度数据,但实际只有{rawData.Length - dataIndex}字节");
|
||||
throw new ArgumentException("原始数据长度不足以包含完整的温度数据");
|
||||
}
|
||||
|
||||
// 遍历所有像素点,按顺序处理温度数据
|
||||
for (int row = 0; row < Height; row++)
|
||||
{
|
||||
for (int col = 0; col < Width; col += 2)
|
||||
for (int col = 0; col < Width; col++)
|
||||
{
|
||||
if (dataIndex + 1 < rawData.Length)
|
||||
{
|
||||
// 按照SDK文档计算温度值:摄氏温度=(H*256+L)/10
|
||||
// 这里假设H是高位字节,L是低位字节
|
||||
int highByte = rawData[dataIndex];
|
||||
int lowByte = rawData[dataIndex + 1];
|
||||
float temperature = (highByte * 256 + lowByte) / 10.0f;
|
||||
float rawTemperature = (highByte * 256 + lowByte) / 10.0f;
|
||||
float compensatedTemperature = rawTemperature + compensationValue;
|
||||
|
||||
// 应用温度补偿值
|
||||
temperature += compensationValue;
|
||||
// 原始温度存入TemperatureMatrix(未温补)
|
||||
TemperatureMatrix[row, col] = rawTemperature;
|
||||
|
||||
// 温补后的温度存入RealTemperatureMatrix(映射到512×384矩阵)
|
||||
MapToRealTemperatureMatrix(row, col, compensatedTemperature);
|
||||
|
||||
TemperatureMatrix[row, col] = temperature;
|
||||
dataIndex += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将原始温度数据映射到512×384的实际温度矩阵
|
||||
/// </summary>
|
||||
/// <param name="sourceRow">源数据行索引</param>
|
||||
/// <param name="sourceCol">源数据列索引</param>
|
||||
/// <param name="temperature">温度值</param>
|
||||
private void MapToRealTemperatureMatrix(int sourceRow, int sourceCol, float temperature)
|
||||
{
|
||||
// 计算映射比例(从原始分辨率映射到512×384)
|
||||
float rowRatio = 384.0f / Height;
|
||||
float colRatio = 512.0f / Width;
|
||||
|
||||
// 处理偶数列
|
||||
dataIndex = 0; // 重置索引,重新遍历
|
||||
for (int row = 0; row < Height; row++)
|
||||
{
|
||||
for (int col = 1; col < Width; col += 2)
|
||||
{
|
||||
if (dataIndex + 1 < rawData.Length)
|
||||
{
|
||||
int highByte = rawData[dataIndex];
|
||||
int lowByte = rawData[dataIndex + 1];
|
||||
float temperature = (highByte * 256 + lowByte) / 10.0f;
|
||||
|
||||
// 应用温度补偿值
|
||||
temperature += compensationValue;
|
||||
|
||||
TemperatureMatrix[row, col] = temperature;
|
||||
dataIndex += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 计算目标位置
|
||||
int targetRow = (int)(sourceRow * rowRatio);
|
||||
int targetCol = (int)(sourceCol * colRatio);
|
||||
|
||||
// 确保目标位置在有效范围内
|
||||
targetRow = Math.Min(383, Math.Max(0, targetRow));
|
||||
targetCol = Math.Min(511, Math.Max(0, targetCol));
|
||||
|
||||
// 设置实际温度值
|
||||
RealTemperatureMatrix[targetRow, targetCol] = temperature;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -275,7 +313,42 @@ namespace JoyD.Windows.CS.Toprie
|
||||
/// 私有构造函数,用于FromByteArray方法
|
||||
/// </summary>
|
||||
private TemperatureData()
|
||||
{ }
|
||||
{
|
||||
// 初始化实际温度矩阵
|
||||
RealTemperatureMatrix = new float[384, 512];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证温度数据的头部信息
|
||||
/// </summary>
|
||||
/// <param name="rawData">包含头部信息的原始数据</param>
|
||||
/// <returns>如果头部信息有效返回true,否则返回false</returns>
|
||||
private bool ValidateHeader(byte[] rawData)
|
||||
{
|
||||
// 根据SDK文档,头部信息的前几个字节通常包含标识字符或特定模式
|
||||
// 这里实现基本验证,可以根据SDK文档的具体规范进行扩展
|
||||
|
||||
// 检查原始数据不为空且长度足够
|
||||
if (rawData == null || rawData.Length < 24)
|
||||
return false;
|
||||
|
||||
// 示例验证逻辑:可以根据实际SDK文档中的头部格式规范进行调整
|
||||
// 1. 检查数据长度是否合理(至少包含头部+最小温度数据)
|
||||
if (rawData.Length < 24 + 2) // 24字节头部 + 至少1个温度点(2字节)
|
||||
return false;
|
||||
|
||||
// 2. 如果SDK文档中定义了特定的头部标识,可以在这里验证
|
||||
// 例如:检查前4个字节是否为特定的魔术数字
|
||||
// uint magicNumber = BitConverter.ToUInt32(rawData, 0);
|
||||
// if (magicNumber != 0x12345678) // 假设的魔术数字
|
||||
// return false;
|
||||
|
||||
// 3. 可以验证头部中的某些字段是否在合理范围内
|
||||
// 例如:宽度、高度等信息
|
||||
|
||||
// 目前实现的是基本验证,根据实际SDK规范可以扩展更详细的验证逻辑
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user