You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. namespace ModbusDemo.Uitls
  8. {
  9. public static class CheckData
  10. {
  11. public static bool CheckResponse(byte[] response)
  12. {
  13. if (response.Length == 0)
  14. {
  15. MessageBox.Show("返回数据为空", "error", MessageBoxButton.OK, MessageBoxImage.Error);
  16. return false;
  17. }
  18. // 检查数组长度是否足够
  19. if (response.Length > 1)
  20. {
  21. byte secondByte = response[1]; // 获取第二个字节(索引为1)
  22. // 使用掩码0xF0获取高4位,并右移4位
  23. int highNibble = (secondByte & 0xF0) >> 4;
  24. // 判断高4位是否等于8
  25. if (highNibble == 8)
  26. {
  27. MessageBox.Show($"数据错误:错误码为:0x{response[2]:X2}");
  28. return false;
  29. }
  30. }
  31. if (!CRCUitl.ValidateCRC(response))
  32. {
  33. MessageBox.Show("0x14:CRC校验错误");
  34. return false;
  35. }
  36. return true;
  37. }
  38. }
  39. }