Usf500NHandler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MECF.Framework.Common.Communications;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FlowMeters
  8. {
  9. public abstract class Usf500NHandler : HandlerBase
  10. {
  11. public Usf500N Device { get; }
  12. public byte Address;
  13. public byte Channel;
  14. public byte FunctionCode;
  15. protected Usf500NHandler(Usf500N device, byte address, byte functionCode, byte[] datas)
  16. : base(BuildMessage(address, functionCode, datas))
  17. {
  18. Device = device;
  19. Address = address;
  20. FunctionCode = functionCode;
  21. }
  22. private static byte[] BuildMessage(byte address, byte command,byte[] datas)
  23. {
  24. List<byte> result = new List<byte>() { address, command};
  25. if (datas != null)
  26. {
  27. foreach (byte data in datas)
  28. result.Add(data);
  29. }
  30. result.AddRange(Calculate_CRC(result.ToArray()));
  31. return result.ToArray();
  32. }
  33. public override bool HandleMessage(MessageBase msg, out bool handled)
  34. {
  35. ResponseMessage = msg;
  36. handled = true;
  37. return true;
  38. }
  39. private static byte[] Calculate_CRC(byte[] buffer)
  40. {
  41. ushort X = 0xFFFF;
  42. for (int i = 0; i < buffer.Length; i++)
  43. {
  44. int timeNo = 0;
  45. while (timeNo < 9)
  46. {
  47. timeNo++;
  48. X = (ushort)(X ^ buffer[i]);
  49. ushort tempValue = (ushort)(X & 1);
  50. X = (ushort)(X >> 1);
  51. if (tempValue == 1)
  52. {
  53. X = (ushort)(X ^ 0xA001);
  54. }
  55. }
  56. }
  57. return new byte[] { (byte)(X >> 8), (byte)(X & 0xFF) };
  58. }
  59. }
  60. /// <summary>
  61. /// 设置组地址
  62. /// <summary>
  63. /// 设置机地址
  64. /// <summary>
  65. /// 查询最高转速 1
  66. /// </summary>
  67. public class Usf500NQueryFlowRateHandler : Usf500NHandler
  68. {
  69. private int _channel;
  70. public Usf500NQueryFlowRateHandler(Usf500N device, byte address, int channel)
  71. : base(device, address,4,BuildData(channel))
  72. {
  73. Name = "QueryFlowRate";
  74. _channel = channel;
  75. }
  76. public override bool HandleMessage(MessageBase msg, out bool handled)
  77. {
  78. var result = msg as Usf500NMessage;
  79. handled = false;
  80. if (!result.IsResponse)
  81. return true;
  82. Device.ParseFlowRate(Address, (byte)_channel, result.ReadOutData);
  83. handled = true;
  84. return true;
  85. }
  86. private static byte[] BuildData(int channel)
  87. {
  88. ushort regAdd = (ushort)(channel * 100 + 1);
  89. byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF),0,1 };
  90. return ret;
  91. }
  92. }
  93. public class Usf500NQueryFlowVolumeHandler : Usf500NHandler
  94. {
  95. private int _channel;
  96. public Usf500NQueryFlowVolumeHandler(Usf500N device, byte address, int channel)
  97. : base(device, address, 4, BuildData(channel))
  98. {
  99. Name = "QueryFlowRate";
  100. _channel = channel;
  101. }
  102. public override bool HandleMessage(MessageBase msg, out bool handled)
  103. {
  104. var result = msg as Usf500NMessage;
  105. handled = false;
  106. if (!result.IsResponse)
  107. return true;
  108. Device.ParseFlowVolume(Address, (byte)_channel, result.ReadOutData);
  109. handled = true;
  110. return true;
  111. }
  112. private static byte[] BuildData(int channel)
  113. {
  114. ushort regAdd = (ushort)(channel * 100 + 2);
  115. byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF), 0, 1 };
  116. return ret;
  117. }
  118. }
  119. public class Usf500NResetFlowVolumeHandler : Usf500NHandler
  120. {
  121. private int _channel;
  122. public Usf500NResetFlowVolumeHandler(Usf500N device, byte address, int channel)
  123. : base(device, address, 6, BuildData(channel))
  124. {
  125. Name = "QueryFlowRate";
  126. _channel = channel;
  127. }
  128. public override bool HandleMessage(MessageBase msg, out bool handled)
  129. {
  130. var result = msg as Usf500NMessage;
  131. handled = false;
  132. if (!result.IsResponse)
  133. return true;
  134. //Device.ParseFlowRate(Address, (byte)_channel, result.ReadOutData);
  135. handled = true;
  136. return true;
  137. }
  138. private static byte[] BuildData(int channel)
  139. {
  140. ushort regAdd = (ushort)(channel * 100 + 3);
  141. byte[] ret = new byte[] { (byte)(regAdd >> 8), (byte)(regAdd & 0xFF), 0, 2 };
  142. return ret;
  143. }
  144. }
  145. }