FujikinMFCHandler.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using MECF.Framework.Common.Communications;
  2. using MECF.Framework.Common.Utilities;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs.FujikinMFC
  10. {
  11. public abstract class FujikinMFCHandler : HandlerBase
  12. {
  13. public FujikinMFC Device { get; }
  14. public string _command;
  15. public string _commandType;
  16. private static byte _start = 0x02;
  17. private static byte _end = 0x00;
  18. //private static byte _ack = 0x06;
  19. //private static byte _nak = 0x16;
  20. private static byte _address = 0x21;
  21. public string DevicePropName;
  22. protected FujikinMFCHandler(FujikinMFC device, string commandType, string command, string parameter = null)
  23. : base(BuildMessage(commandType,command,parameter))
  24. {
  25. Device = device;
  26. _command = command;
  27. _commandType = commandType;
  28. _address = Convert.ToByte(Device.Address, 16);
  29. Name = command;
  30. }
  31. protected FujikinMFCHandler(FujikinMFC device, string commandType, string command, byte[] parameter)
  32. : base(BuildMessage(commandType, command, parameter))
  33. {
  34. Device = device;
  35. _command = command;
  36. _commandType = commandType;
  37. _address = Convert.ToByte(Device.Address, 16);
  38. Name = command;
  39. }
  40. private static byte[] BuildMessage(string commandType, string command, string parameter)
  41. {
  42. List<byte> buffer = new List<byte>();
  43. var parameterArray = parameter!=null? parameter.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray():null;
  44. buffer.Add(_address);
  45. buffer.Add(_start);
  46. buffer.Add(Convert.ToByte(commandType, 16));
  47. buffer.Add((byte)(3 + (parameterArray!=null ? parameterArray.Length:0)));
  48. buffer.AddRange(command.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  49. if(parameterArray != null)
  50. buffer.AddRange(parameterArray);
  51. buffer.Add(_end);
  52. byte checkSum = 0;
  53. for (int i = 1; i < buffer.Count - 1; i++)
  54. {
  55. checkSum += buffer[i];
  56. }
  57. buffer.Add(checkSum);
  58. return buffer.ToArray();
  59. }
  60. protected static byte[] BuildMessage(string commandType, string command, byte[] parameterArray = null)
  61. {
  62. List<byte> buffer = new List<byte>();
  63. buffer.Add(_address);
  64. buffer.Add(_start);
  65. buffer.Add(Convert.ToByte(commandType, 16));
  66. buffer.Add((byte)(3 + (parameterArray != null ? parameterArray.Length : 0)));
  67. buffer.AddRange(command.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  68. if (parameterArray != null)
  69. buffer.AddRange(parameterArray);
  70. buffer.Add(_end);
  71. byte checkSum = 0;
  72. for (int i = 1; i < buffer.Count - 1; i++)
  73. {
  74. checkSum += buffer[i];
  75. }
  76. buffer.Add(checkSum);
  77. return buffer.ToArray();
  78. }
  79. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  80. {
  81. FujikinMFCMessage response = msg as FujikinMFCMessage;
  82. ResponseMessage = msg;
  83. if (response.IsNak)
  84. {
  85. Device.NoteNAK();
  86. transactionComplete = true;
  87. return true;
  88. }
  89. else if (response.IsAck)
  90. {
  91. if (this.IsAcked)
  92. {
  93. SetState(EnumHandlerState.Completed);
  94. transactionComplete = true;
  95. return true;
  96. }
  97. else
  98. {
  99. SetState(EnumHandlerState.Acked);
  100. transactionComplete = false;
  101. return false;
  102. }
  103. }
  104. else if(response.IsComplete)
  105. {
  106. SetState(EnumHandlerState.Completed);
  107. transactionComplete = true;
  108. return true;
  109. }
  110. else
  111. {
  112. msg.IsFormatError = true;
  113. transactionComplete = false;
  114. return false;
  115. }
  116. }
  117. protected static byte[] StringToByteArray(string parameter)
  118. {
  119. if (parameter == null)
  120. return new byte[] { };
  121. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  122. }
  123. protected static byte[] ShortToByteArray(short num)
  124. {
  125. byte[] bytes = BitConverter.GetBytes(num);
  126. return bytes;
  127. }
  128. protected static byte[] IntToByteArray(int num)
  129. {
  130. byte[] bytes = BitConverter.GetBytes(num);
  131. return bytes;
  132. }
  133. protected static byte[] FloarToByteArray(float num)
  134. {
  135. byte[] bytes = BitConverter.GetBytes(num);
  136. return bytes;
  137. }
  138. protected int ByteArrayToInt(byte[] bytes)
  139. {
  140. int temp = BitConverter.ToInt32(bytes, 0);
  141. return temp;
  142. }
  143. protected short ByteArrayToShort(byte[] bytes)
  144. {
  145. short temp = BitConverter.ToInt16(bytes, 0);
  146. return temp;
  147. }
  148. protected float ByteArrayToFloat(byte[] bytes)
  149. {
  150. if(bytes.Length != 4)
  151. {
  152. return 0;
  153. }
  154. float temp = BitConverter.ToSingle(bytes, 0);
  155. return temp;
  156. }
  157. protected void SetDevicePropValue<T>(string propName, T value)
  158. {
  159. try
  160. {
  161. Type objType = Device.GetType();
  162. objType.GetProperty(propName).SetValue(Device, value, null);
  163. }
  164. catch
  165. {
  166. }
  167. }
  168. }
  169. public class FujikinMFCRawCommandHandler : FujikinMFCHandler
  170. {
  171. public FujikinMFCRawCommandHandler(FujikinMFC device, string commandType, string command, string parameter)
  172. : base(device, commandType, command, parameter)
  173. {
  174. }
  175. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  176. {
  177. if(base.HandleMessage(msg, out transactionComplete))
  178. {
  179. FujikinMFCMessage response = msg as FujikinMFCMessage;
  180. Device.NoteRawCommandInfo(_commandType, _command, string.Join(",", response.RawMessage.Select(bt => bt.ToString("X2")).ToArray()), msg.IsAck);
  181. return true;
  182. }
  183. return false;
  184. }
  185. }
  186. public class FujikinMFCResetHandler : FujikinMFCHandler
  187. {
  188. public FujikinMFCResetHandler(FujikinMFC device)
  189. : base(device, "81","01,01,C7")
  190. {
  191. }
  192. public override bool HandleMessage(MessageBase msg, out bool handled)
  193. {
  194. if (base.HandleMessage(msg, out handled))
  195. {
  196. FujikinMFCMessage response = msg as FujikinMFCMessage;
  197. Device.NoteResetCompleted();
  198. return true;
  199. }
  200. return false;
  201. }
  202. }
  203. public class FujikinMFCSetEnableHandler : FujikinMFCHandler
  204. {
  205. public FujikinMFCSetEnableHandler(FujikinMFC device, string command, string enableValue)
  206. : base(device, "81", command, BoolToByte(enableValue))
  207. {
  208. }
  209. private static string BoolToByte(string boolStr)
  210. {
  211. return boolStr == "true" ? "01" : "00";
  212. }
  213. public override bool HandleMessage(MessageBase msg, out bool handled)
  214. {
  215. if (base.HandleMessage(msg, out handled))
  216. {
  217. FujikinMFCMessage response = msg as FujikinMFCMessage;
  218. Device.NoteActionCompleted();
  219. return true;
  220. }
  221. return false;
  222. }
  223. }
  224. public class FujikinMFCSet8BitValueHandler : FujikinMFCHandler
  225. {
  226. public FujikinMFCSet8BitValueHandler(FujikinMFC device, string command, byte value)
  227. : base(device, "81", command, new byte[] { value })
  228. {
  229. }
  230. public override bool HandleMessage(MessageBase msg, out bool handled)
  231. {
  232. if (base.HandleMessage(msg, out handled))
  233. {
  234. FujikinMFCMessage response = msg as FujikinMFCMessage;
  235. Device.NoteActionCompleted();
  236. return true;
  237. }
  238. return false;
  239. }
  240. }
  241. public class FujikinMFCSet16BitValueHandler : FujikinMFCHandler
  242. {
  243. public FujikinMFCSet16BitValueHandler(FujikinMFC device, string command, short value)
  244. : base(device, "81", command, ShortToByteArray(value))
  245. {
  246. }
  247. public override bool HandleMessage(MessageBase msg, out bool handled)
  248. {
  249. if (base.HandleMessage(msg, out handled))
  250. {
  251. FujikinMFCMessage response = msg as FujikinMFCMessage;
  252. Device.NoteActionCompleted();
  253. return true;
  254. }
  255. return false;
  256. }
  257. }
  258. public class FujikinMFCSet32BitValueHandler : FujikinMFCHandler
  259. {
  260. public FujikinMFCSet32BitValueHandler(FujikinMFC device, string command, int value)
  261. : base(device, "81", command, IntToByteArray(value))
  262. {
  263. }
  264. public override bool HandleMessage(MessageBase msg, out bool handled)
  265. {
  266. if (base.HandleMessage(msg, out handled))
  267. {
  268. FujikinMFCMessage response = msg as FujikinMFCMessage;
  269. Device.NoteActionCompleted();
  270. return true;
  271. }
  272. return false;
  273. }
  274. }
  275. public class FujikinMFCReadExceptionStatusHandler : FujikinMFCHandler
  276. {
  277. public FujikinMFCReadExceptionStatusHandler(FujikinMFC device)
  278. : base(device, "80", "65,01,A0")
  279. {
  280. }
  281. public override bool HandleMessage(MessageBase msg, out bool handled)
  282. {
  283. if (base.HandleMessage(msg, out handled))
  284. {
  285. FujikinMFCMessage response = msg as FujikinMFCMessage;
  286. if(response.Data.Length != 1)
  287. {
  288. Device.NoteError("invalid ExceptionStatus");
  289. }
  290. else
  291. {
  292. Device.NoteExcetionInfo(response.Data[0]);
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. }
  299. public class FujikinMFCReadAlarmDetailHandler : FujikinMFCHandler
  300. {
  301. public FujikinMFCReadAlarmDetailHandler(FujikinMFC device)
  302. : base(device, "80", "65,01,A1")
  303. {
  304. }
  305. public override bool HandleMessage(MessageBase msg, out bool handled)
  306. {
  307. if (base.HandleMessage(msg, out handled))
  308. {
  309. FujikinMFCMessage response = msg as FujikinMFCMessage;
  310. if (response.Data.Length != 2)
  311. {
  312. Device.NoteError("invalid AlarmDetai");
  313. }
  314. else
  315. {
  316. Device.NoteAlarmDetail(ByteArrayToShort(response.Data));
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. }
  323. public class FujikinMFCReadWarningDetailHandler : FujikinMFCHandler
  324. {
  325. public FujikinMFCReadWarningDetailHandler(FujikinMFC device)
  326. : base(device, "80", "65,01,A2")
  327. {
  328. }
  329. public override bool HandleMessage(MessageBase msg, out bool handled)
  330. {
  331. if (base.HandleMessage(msg, out handled))
  332. {
  333. FujikinMFCMessage response = msg as FujikinMFCMessage;
  334. if (response.Data.Length != 1)
  335. {
  336. Device.NoteError("invalid WarningDetail");
  337. }
  338. else
  339. {
  340. Device.NoteWarningDetail(ByteArrayToShort(response.Data));
  341. return true;
  342. }
  343. }
  344. return false;
  345. }
  346. }
  347. public class FujikinMFCReadEnableHandler : FujikinMFCHandler
  348. {
  349. public FujikinMFCReadEnableHandler(FujikinMFC device, string command, string propName)
  350. : base(device, "80", command)
  351. {
  352. DevicePropName = propName;
  353. }
  354. public override bool HandleMessage(MessageBase msg, out bool handled)
  355. {
  356. if (base.HandleMessage(msg, out handled))
  357. {
  358. FujikinMFCMessage response = msg as FujikinMFCMessage;
  359. if (response.Data.Length != 1)
  360. {
  361. Device.NoteError("invalid Enable response");
  362. }
  363. else
  364. {
  365. SetDevicePropValue<bool>(DevicePropName, response.Data[0] == 1);
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. }
  372. public class FujikinMFCRead8BitHandler : FujikinMFCHandler
  373. {
  374. public FujikinMFCRead8BitHandler(FujikinMFC device, string command, string propName)
  375. : base(device, "80", command)
  376. {
  377. DevicePropName = propName;
  378. }
  379. public override bool HandleMessage(MessageBase msg, out bool handled)
  380. {
  381. if (base.HandleMessage(msg, out handled))
  382. {
  383. FujikinMFCMessage response = msg as FujikinMFCMessage;
  384. if (response.Data.Length != 1)
  385. {
  386. Device.NoteError("invalid 8Bit response");
  387. }
  388. else
  389. {
  390. SetDevicePropValue<short>(DevicePropName, (short)response.Data[0]);
  391. return true;
  392. }
  393. }
  394. return false;
  395. }
  396. }
  397. public class FujikinMFCRead16BitHandler : FujikinMFCHandler
  398. {
  399. public FujikinMFCRead16BitHandler(FujikinMFC device, string command, string propName)
  400. : base(device, "80", command)
  401. {
  402. DevicePropName = propName;
  403. }
  404. public override bool HandleMessage(MessageBase msg, out bool handled)
  405. {
  406. if (base.HandleMessage(msg, out handled))
  407. {
  408. FujikinMFCMessage response = msg as FujikinMFCMessage;
  409. if (response.Data.Length != 2)
  410. {
  411. Device.NoteError("invalid 16Bit response");
  412. }
  413. else
  414. {
  415. SetDevicePropValue<short>(DevicePropName, ByteArrayToShort(response.Data));
  416. return true;
  417. }
  418. }
  419. return false;
  420. }
  421. }
  422. public class FujikinMFCRead32BitHandler : FujikinMFCHandler
  423. {
  424. public FujikinMFCRead32BitHandler(FujikinMFC device, string command, string propName)
  425. : base(device, "80", command)
  426. {
  427. DevicePropName = propName;
  428. }
  429. public override bool HandleMessage(MessageBase msg, out bool handled)
  430. {
  431. if (base.HandleMessage(msg, out handled))
  432. {
  433. FujikinMFCMessage response = msg as FujikinMFCMessage;
  434. if (response.Data.Length != 4)
  435. {
  436. Device.NoteError("invalid 32Bit response");
  437. }
  438. else
  439. {
  440. SetDevicePropValue<int>(DevicePropName, ByteArrayToInt(response.Data));
  441. return true;
  442. }
  443. }
  444. return false;
  445. }
  446. }
  447. }