HoribaHandler.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.SCCore;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Communications;
  5. using MECF.Framework.Common.Device.Bases;
  6. using System.Collections.Generic;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs.HoribaMFC
  8. {
  9. public abstract class HoribaHandler : HandlerBase
  10. {
  11. private const byte Header = 0x40;
  12. private const byte STX = 0x02;
  13. private const byte ETX = 0x03;
  14. protected HoribaConnection _connection;
  15. public string Address { get; set; }
  16. protected HoribaHandler(HoribaConnection connection, string msdLsdAddress, string data)
  17. : base(BuildMessage(msdLsdAddress, data))
  18. {
  19. Address = msdLsdAddress;
  20. _connection = connection;
  21. }
  22. private static byte[] BuildMessage(string msdLsdAddress, string data)
  23. {
  24. List<byte> message = new List<byte>();
  25. message.Add(Header);
  26. PacketMessage(msdLsdAddress, message);
  27. message.Add(STX);
  28. PacketMessage(data, message);
  29. message.Add(ETX);
  30. message.Add((byte)CalcBBC(data));
  31. if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
  32. message.Add(0x0D);
  33. return message.ToArray();
  34. }
  35. public override bool HandleMessage(MessageBase msg, out bool handled)
  36. {
  37. ResponseMessage = msg;
  38. handled = true;
  39. return true;
  40. }
  41. private static int CalcBBC(string value)
  42. {
  43. int sum = 0;
  44. foreach (var item in value)
  45. {
  46. sum += (int)item;
  47. }
  48. sum += ETX;
  49. return sum % 128;
  50. }
  51. private static List<byte> PacketMessage(string msg, List<byte> msgList)
  52. {
  53. foreach (var item in msg)
  54. {
  55. msgList.Add((byte)item);
  56. }
  57. return msgList;
  58. }
  59. private static byte[] String2Ascii(string s)
  60. {
  61. byte[] array = System.Text.Encoding.ASCII.GetBytes(s);
  62. return array;
  63. }
  64. }
  65. public class HoribaMFCSetFlow : HoribaHandler
  66. {
  67. public HoribaMFCSetFlow(HoribaConnection connection, string deviceAddress, float flow)
  68. : base(connection, deviceAddress, $"AFC{flow},B")
  69. {
  70. Name = "SetFlow";
  71. }
  72. public override bool HandleMessage(MessageBase msg, out bool handled)
  73. {
  74. var result = msg as HoribaMessage;
  75. handled = false;
  76. if (!result.IsResponse) return true;
  77. if (result.IsError || result.IsFormatError || result.IsNak)
  78. {
  79. _connection.SetError(result.RawMessage);
  80. }
  81. else
  82. {
  83. if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b)
  84. {
  85. _connection.SetError(result.RawMessage);
  86. }
  87. }
  88. ResponseMessage = msg;
  89. handled = true;
  90. return true;
  91. }
  92. }
  93. public class HoribaMFCQueryFlow : HoribaHandler
  94. {
  95. public HoribaMFCQueryFlow(HoribaConnection connection, string deviceAddress)
  96. : base(connection, deviceAddress, $"RFV")
  97. {
  98. Name = "QueryFlow";
  99. }
  100. public override bool HandleMessage(MessageBase msg, out bool handled)
  101. {
  102. var result = msg as HoribaMessage;
  103. handled = false;
  104. if (!result.IsResponse) return true;
  105. if (result.IsError || result.IsFormatError || result.IsNak)
  106. {
  107. _connection.SetError(result.RawMessage);
  108. }
  109. else
  110. {
  111. float.TryParse(System.Text.Encoding.ASCII.GetString(result.Datas), out float flow);
  112. //MFCDevice.FeedBack = flow;
  113. _connection.NoteFlowReadout(Address, flow);
  114. }
  115. ResponseMessage = msg;
  116. handled = true;
  117. return true;
  118. }
  119. }
  120. public class HoribaMFCQueryScale : HoribaHandler
  121. {
  122. public HoribaMFCQueryScale(HoribaConnection connection, string deviceAddress)
  123. : base(connection, deviceAddress, $"RFS")
  124. {
  125. Name = "QueryScale";
  126. }
  127. public override bool HandleMessage(MessageBase msg, out bool handled)
  128. {
  129. var result = msg as HoribaMessage;
  130. handled = false;
  131. if (!result.IsResponse) return true;
  132. if (result.IsError || result.IsFormatError || result.IsNak)
  133. {
  134. _connection.SetError(result.RawMessage);
  135. }
  136. else
  137. {
  138. var dataArray = System.Text.Encoding.ASCII.GetString(result.Datas).Split(',');
  139. float.TryParse(dataArray[0], out float range);
  140. //temporary comment to fix compile issue
  141. if (dataArray.Length > 1)
  142. {
  143. //if (MFCDevice.Unit != dataArray[1].ToUpper())
  144. // EV.PostInfoLog(MFCDevice.Module, $"{MFCDevice.Module} {MFCDevice.Name} change from {MFCDevice.Unit} to {dataArray[1].ToUpper()}");
  145. //MFCDevice.Unit = dataArray[1].ToUpper();
  146. _connection.NoteUnitReadout(Address, dataArray[1].ToUpper());
  147. }
  148. }
  149. ResponseMessage = msg;
  150. handled = true;
  151. return true;
  152. }
  153. }
  154. public class HoribaMFCSetDigitalMode : HoribaHandler
  155. {
  156. public HoribaMFCSetDigitalMode(HoribaConnection connection, string deviceAddress)
  157. : base(connection, deviceAddress, $"'!+REVERSE")
  158. {
  159. Name = "SetDigitalMode";
  160. }
  161. public override bool HandleMessage(MessageBase msg, out bool handled)
  162. {
  163. var result = msg as HoribaMessage;
  164. handled = false;
  165. if (!result.IsResponse) return true;
  166. if (result.IsError || result.IsFormatError || result.IsNak)
  167. {
  168. _connection.SetError(result.RawMessage);
  169. }
  170. else
  171. {
  172. if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b)
  173. {
  174. _connection.SetError(result.RawMessage);
  175. }
  176. }
  177. ResponseMessage = msg;
  178. handled = true;
  179. return true;
  180. }
  181. }
  182. public class HoribaMFCSetAnalogMode : HoribaHandler
  183. {
  184. public HoribaMFCSetAnalogMode(HoribaConnection connection, string deviceAddress)
  185. : base(connection, deviceAddress, $"'!+NORMAL")
  186. {
  187. Name = "SetAnalogMode";
  188. }
  189. public override bool HandleMessage(MessageBase msg, out bool handled)
  190. {
  191. var result = msg as HoribaMessage;
  192. handled = false;
  193. if (!result.IsResponse) return true;
  194. if (result.IsError || result.IsFormatError || result.IsNak)
  195. {
  196. _connection.SetError(result.RawMessage);
  197. }
  198. else
  199. {
  200. if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b)
  201. {
  202. _connection.SetError(result.RawMessage);
  203. }
  204. }
  205. ResponseMessage = msg;
  206. handled = true;
  207. return true;
  208. }
  209. }
  210. }