FfuAAFHandler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.FFUs.AAF
  8. {
  9. public abstract class FfuAAFHandler : HandlerBase
  10. {
  11. public FfuAAF Device { get; }
  12. public byte FanAddress;
  13. public bool IsNeedFeedback;
  14. protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas)
  15. : base(BuildMessage(prefix, command, groupAddress, datas))
  16. {
  17. Device = device;
  18. IsNeedFeedback = true;
  19. AckTimeout = TimeSpan.FromSeconds(3);
  20. }
  21. protected FfuAAFHandler(FfuAAF device, byte prefix, byte command, byte groupAddress, byte[] datas,bool needfeedback)
  22. : base(BuildMessage(prefix, command, groupAddress, datas))
  23. {
  24. Device = device;
  25. IsNeedFeedback = needfeedback;
  26. }
  27. private static byte[] BuildMessage(byte prefix, byte command, byte groupAddress,byte[] datas)
  28. {
  29. List<byte> result = new List<byte>() { prefix, command , groupAddress };
  30. if (datas != null)
  31. {
  32. foreach(byte data in datas)
  33. result.Add(data);
  34. }
  35. result.Add(ModRTU_CRC(result.ToArray()));
  36. return result.ToArray();
  37. }
  38. public override bool HandleMessage(MessageBase msg, out bool handled)
  39. {
  40. ResponseMessage = msg;
  41. handled = true;
  42. return true;
  43. }
  44. private static byte ModRTU_CRC(byte[] buffer)
  45. {
  46. //ushort crc = 0xFFFF;
  47. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  48. var buf = buffer;
  49. var len = buffer.Length;
  50. byte temp = buffer[0];
  51. for (int i = 1; i < buffer.Length; i++)
  52. {
  53. temp = (byte)(temp ^ buffer[i]);
  54. }
  55. return (byte)~temp;
  56. }
  57. }
  58. public class FfuAAFQuerySpeedHandler : FfuAAFHandler
  59. {
  60. public FfuAAFQuerySpeedHandler(FfuAAF device,byte fanaddress, byte groupAddress)
  61. : base(device, 0x15, (byte)(0x20 + fanaddress), groupAddress, null)
  62. {
  63. Name = "Query Speed";
  64. FanAddress = fanaddress;
  65. }
  66. public override bool HandleMessage(MessageBase msg, out bool handled)
  67. {
  68. var result = msg as FfuAAFMessage;
  69. handled = false;
  70. if (!result.IsResponse) return true;
  71. if (FanAddress == 1) Device.FFU1Speed = result.Data1 * Device.NMaxSpeed / 250;
  72. if (FanAddress == 3) Device.FFU2Speed = result.Data1 * Device.NMaxSpeed / 250;
  73. ResponseMessage = msg;
  74. handled = true;
  75. return true;
  76. }
  77. }
  78. public class FfuAAFQueryStatusHandler : FfuAAFHandler
  79. {
  80. public FfuAAFQueryStatusHandler(FfuAAF device,byte fanaddress, byte groupAddress)
  81. : base(device, 0x35, fanaddress, groupAddress, new byte[] { 0x00 })
  82. {
  83. Name = "Query Status";
  84. }
  85. public override bool HandleMessage(MessageBase msg, out bool handled)
  86. {
  87. var result = msg as FfuAAFMessage;
  88. //if (result.IsError)
  89. //{
  90. // Device.SetError(result.Data);
  91. //}
  92. //else
  93. //{
  94. // if (int.TryParse(result.Data, out int code))
  95. // {
  96. // Device.SetErrorCode(code);
  97. // }
  98. // else
  99. // {
  100. // Device.SetError(result.Data + "format error");
  101. // }
  102. //}
  103. ResponseMessage = msg;
  104. handled = true;
  105. return true;
  106. }
  107. }
  108. public class FfuAAFSetSpeedHandler : FfuAAFHandler
  109. {
  110. public FfuAAFSetSpeedHandler(FfuAAF device, int fanaddress,int groupAddress,int speed)
  111. : base(device, 0x35, (byte)(0x40 + fanaddress), (byte)groupAddress, new byte[] { (byte)(speed*250/device.NMaxSpeed) },false)
  112. {
  113. Name = "SetSpeed";
  114. }
  115. public override bool HandleMessage(MessageBase msg, out bool handled)
  116. {
  117. var result = msg as FfuAAFMessage;
  118. if(result.IsAck)
  119. {
  120. handled = true;
  121. return true;
  122. }
  123. handled = false;
  124. return false;
  125. }
  126. }
  127. }