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.

42 regels
1.1 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. // 检查数组长度是否足够
  14. if (response.Length > 1)
  15. {
  16. byte secondByte = response[1]; // 获取第二个字节(索引为1)
  17. // 使用掩码0xF0获取高4位,并右移4位
  18. int highNibble = (secondByte & 0xF0) >> 4;
  19. // 判断高4位是否等于8
  20. if (highNibble == 8)
  21. {
  22. MessageBox.Show($"数据错误:错误码为:0x{response[2]:X2}");
  23. }
  24. return false;
  25. }
  26. if(response.Length == 0)
  27. {
  28. MessageBox.Show("返回数据为空","error",MessageBoxButton.OK,MessageBoxImage.Error);
  29. return false;
  30. }
  31. return true;
  32. }
  33. }
  34. }