using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ModbusDemo.Uitls
{
public static class CheckData
{
///
/// 检测返回的数据格式是否正确
///
///
///
public static bool CheckResponse(byte[] response)
{
if (response == null || response.Length == 0)
{
MessageBox.Show("返回数据为空", "error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
// 检查数组长度是否足够
if (response.Length > 1)
{
byte secondByte = response[1]; // 获取第二个字节(索引为1)
// 使用掩码0xF0获取高4位,并右移4位
int highNibble = (secondByte & 0xF0) >> 4;
// 判断高4位是否等于8
if (highNibble == 8)
{
MessageBox.Show($"数据错误:错误码为:0x{response[2]:X2}");
return false;
}
}
if (!CRCUitl.ValidateCRC(response))
{
MessageBox.Show("0x14:CRC校验错误");
return false;
}
return true;
}
}
}