Analysiser.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using IvfTl.Control.Entity.GlobalEnums;
  2. namespace ivf_tl_SerialHelper.Util
  3. {
  4. public class Analysiser
  5. {
  6. /// <summary>
  7. /// 解析E方
  8. /// </summary>
  9. /// <param name="customProtocol"></param>
  10. /// <returns></returns>
  11. public static int ParseEEPROMPulse(byte[] arg)
  12. {
  13. if(!arg.Any()) return -1;
  14. byte[] temp = { arg[4], arg[5] , arg[6], arg[7] };
  15. if (!BitConverter.IsLittleEndian)
  16. Array.Reverse(temp);
  17. return BitConverter.ToInt32(temp, 0);
  18. }
  19. /// <summary>
  20. /// 握手指令解析
  21. /// </summary>
  22. /// <param name="arg"></param>
  23. /// <returns></returns>
  24. public static int ParseShakeHandsCommand(byte[] arg)
  25. {
  26. if (arg.Any()) return Convert.ToInt32(arg[2]);
  27. return -1;
  28. }
  29. /// <summary>
  30. /// 仓门状态
  31. /// </summary>
  32. /// <param name="bs"></param>
  33. /// <returns></returns>
  34. public static State ParseDoorStatus(byte[] arg)
  35. {
  36. if (!arg.Any()) return State.未知;
  37. return arg[4] == 0x01 ? State.打开 : State.关闭;
  38. }
  39. /// <summary>
  40. /// 温度解析
  41. /// </summary>
  42. /// <param name="bs"></param>
  43. /// <returns></returns>
  44. public static decimal ParseTemperatureCommand(byte[] arg)
  45. {
  46. if (!arg.Any()) return -1m;
  47. byte[] temp = { arg[5], arg[4] };
  48. if (!BitConverter.IsLittleEndian)
  49. Array.Reverse(temp);
  50. int num = BitConverter.ToInt16(temp);
  51. var result = Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  52. return result;
  53. }
  54. /// <summary>
  55. /// 气压解析
  56. /// </summary>
  57. /// <param name="arg"></param>
  58. /// <returns></returns>
  59. public static decimal ParsePressureCommand(byte[] arg)
  60. {
  61. if (!arg.Any()) return -1m;
  62. byte[] temp = { arg[5], arg[4] };
  63. if (!BitConverter.IsLittleEndian)
  64. Array.Reverse(temp);
  65. int num = BitConverter.ToInt16(temp);
  66. return (decimal)num;
  67. }
  68. /// <summary>
  69. /// 仪器温度1解析
  70. /// </summary>
  71. /// <param name="customProtocol"></param>
  72. /// <returns></returns>
  73. public static decimal ParseTLTemperature1Command(byte[] arg)
  74. {
  75. if (!arg.Any()) return -1m;
  76. byte[] temp = { arg[7], arg[6] };
  77. if (!BitConverter.IsLittleEndian)
  78. Array.Reverse(temp);
  79. int num = BitConverter.ToInt16(temp);
  80. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  81. }
  82. /// <summary>
  83. /// 仪器温度2解析
  84. /// </summary>
  85. /// <param name="customProtocol"></param>
  86. /// <returns></returns>
  87. public static decimal ParseTLTemperature2Command(byte[] arg)
  88. {
  89. if (!arg.Any()) return -1m;
  90. byte[] temp = { arg[9], arg[8] };
  91. if (!BitConverter.IsLittleEndian)
  92. Array.Reverse(temp);
  93. int num = BitConverter.ToInt16(temp);
  94. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  95. }
  96. /// <summary>
  97. /// 当前电机位置解析
  98. /// </summary>
  99. /// <param name="customProtocol"></param>
  100. /// <returns></returns>
  101. public static int ParseCurrentMotor(byte[] arg)
  102. {
  103. if (!arg.Any()) return -1;
  104. byte[] temp = { arg[7], arg[6], arg[5], arg[4] };
  105. if (!BitConverter.IsLittleEndian)
  106. Array.Reverse(temp);
  107. return BitConverter.ToInt32(temp, 0);
  108. }
  109. }
  110. }