|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using ModbusDemo.ErrorCode;
- namespace ModbusDemo.Uitls
- {
- public static class CheckData
- {
- /// <summary>
- /// 检测返回的数据格式是否正确
- /// </summary>
- /// <param name="response"></param>
- /// <returns></returns>
- 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)
- {
- var error = ErrorCode.ErrorCode.FromByte(response[2]);
-
- MessageBox.Show(error.ToString());
- return false;
- }
- }
-
-
- if (!CRCUitl.ValidateCRC(response))
- {
- MessageBox.Show(ErrorCode.ErrorCode.CrcCheckError.ToString());
- return false;
- }
-
- return true;
- }
-
- }
- }
|