TruPlasmaRF1001Handler.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.Log;
  6. using MECF.Framework.Common.Communications;
  7. using MECF.Framework.Common.Utilities;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.TruPlasmaRF
  9. {
  10. //TruPlasma RF 1001 to 1003
  11. public abstract class TruPlasmaRF1001Handler : HandlerBase
  12. {
  13. public TruPlasmaRF1001 Device { get; }
  14. public string _command;
  15. protected string _parameter;
  16. protected TruPlasmaRF1001Handler(TruPlasmaRF1001 device, string cmd):base("")
  17. {
  18. Device = device;
  19. Name = _command = cmd;
  20. }
  21. protected TruPlasmaRF1001Handler(TruPlasmaRF1001 device, string command, string parameter = null)
  22. : base(BuildMessage(ToByteArray(command), ToByteArray(parameter)))
  23. {
  24. Device = device;
  25. _command = command;
  26. _parameter = parameter;
  27. Name = command;
  28. }
  29. protected TruPlasmaRF1001Handler(TruPlasmaRF1001 device, string command, byte[] parameter)
  30. : base(BuildMessage(ToByteArray(command), parameter))
  31. {
  32. Device = device;
  33. _command = command;
  34. //_parameter = parameter;
  35. Name = command;
  36. }
  37. private static byte _start = 0xAA;
  38. private static byte _address = 0x02;
  39. private static byte _GS = 0x00;
  40. private static byte _stop = 0x55;
  41. protected static byte[] BuildMessage(byte[] commandArray, byte[] argumentArray)
  42. {
  43. List<byte> buffer = new List<byte>();
  44. buffer.Add(_start);
  45. buffer.Add(_address);
  46. int length = 1 + (commandArray != null ? commandArray.Length : 0) + (argumentArray != null ? argumentArray.Length : 0);
  47. buffer.Add((byte)length);
  48. buffer.Add(_GS);
  49. if (commandArray != null && commandArray.Length > 0)
  50. {
  51. //skip ManualExecuteCommand
  52. if (!(commandArray.Length == 1 && commandArray[0] == 0))
  53. {
  54. buffer.AddRange(commandArray);
  55. }
  56. }
  57. if (argumentArray != null && argumentArray.Length > 0)
  58. {
  59. buffer.AddRange(argumentArray);
  60. }
  61. //var checkSum = Crc16.CRC16_CCITT(contentBuffer);
  62. var checkSum = Crc16.Crc16Ccitt(buffer.ToArray());
  63. buffer.AddRange(BitConverter.GetBytes(checkSum));
  64. buffer.Add(_stop);
  65. return buffer.ToArray();
  66. }
  67. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  68. {
  69. transactionComplete = false;
  70. TruPlasmaRF1001Message response = msg as TruPlasmaRF1001Message;
  71. ResponseMessage = msg;
  72. if (response.IsError)
  73. {
  74. Device.NoteError(response.ErrorCode);
  75. }
  76. else
  77. {
  78. Device.NoteError(null);
  79. }
  80. if (response.IsAck)
  81. {
  82. SetState(EnumHandlerState.Acked);
  83. }
  84. if (this.IsAcked && response.IsComplete)
  85. {
  86. if (response == null || response.RawMessage == null) return false;
  87. ushort checkSum_calc = Crc16.Crc16Ccitt(response.RawMessage.Take(response.RawMessage.Length - 3).ToArray());
  88. ushort checkSum = BitConverter.ToUInt16(response.RawMessage, response.RawMessage.Length - 3);
  89. if (checkSum_calc != checkSum)
  90. {
  91. LOG.Warning($"{this.Device.Address} received wrong checksum");
  92. return false;
  93. }
  94. if (response.Command == _command)
  95. {
  96. SetState(EnumHandlerState.Completed);
  97. transactionComplete = true;
  98. return true;
  99. }
  100. else
  101. Device.NoteError($"command mismatch set command={_command} response command={response.Command}");
  102. }
  103. return false;
  104. }
  105. protected static byte[] ToByteArray(string parameter)
  106. {
  107. if (parameter == null)
  108. return new byte[] { };
  109. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  110. }
  111. }
  112. public class TruPlasmaRF1001RawCommandHandler : TruPlasmaRF1001Handler
  113. {
  114. public TruPlasmaRF1001RawCommandHandler(TruPlasmaRF1001 device, string command, string parameter = null)
  115. : base(device, command, parameter)
  116. {
  117. }
  118. public override bool HandleMessage(MessageBase msg, out bool handled)
  119. {
  120. if (base.HandleMessage(msg, out handled))
  121. {
  122. var result = msg as TruPlasmaRF1001Message;
  123. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  124. }
  125. return true;
  126. }
  127. }
  128. public class TruPlasmaRF1001GetControlHandler : TruPlasmaRF1001Handler
  129. {
  130. public TruPlasmaRF1001GetControlHandler(TruPlasmaRF1001 device)
  131. : base(device, "05,01,00,00", "FF")
  132. {
  133. }
  134. public override bool HandleMessage(MessageBase msg, out bool handled)
  135. {
  136. if (base.HandleMessage(msg, out handled))
  137. {
  138. var result = msg as TruPlasmaRF1001Message;
  139. Device.NoteInterfaceActived(true);
  140. }
  141. return true;
  142. }
  143. }
  144. public class TruPlasmaRF1001ReleaseControlHandler : TruPlasmaRF1001Handler
  145. {
  146. public TruPlasmaRF1001ReleaseControlHandler(TruPlasmaRF1001 device)
  147. : base(device, "05,02,00,00", "FF")
  148. {
  149. }
  150. public override bool HandleMessage(MessageBase msg, out bool handled)
  151. {
  152. if (base.HandleMessage(msg, out handled))
  153. {
  154. var result = msg as TruPlasmaRF1001Message;
  155. Device.NoteInterfaceActived(false);
  156. }
  157. return true;
  158. }
  159. }
  160. //active RS232
  161. public class TruPlasmaRF1001SetActiveInterfaceHandler : TruPlasmaRF1001Handler
  162. {
  163. private static byte _STAT = 0x00;//No error, data follows.
  164. private static byte _TYP = 0x04;//SINT32
  165. public TruPlasmaRF1001SetActiveInterfaceHandler(TruPlasmaRF1001 device)
  166. : base(device, "02,52,01,01", IntToByteArray(1))
  167. {
  168. }
  169. private static byte[] IntToByteArray(int num)
  170. {
  171. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  172. tmp.AddRange(BitConverter.GetBytes(num));
  173. return tmp.ToArray();
  174. }
  175. public override bool HandleMessage(MessageBase msg, out bool handled)
  176. {
  177. if (base.HandleMessage(msg, out handled))
  178. {
  179. }
  180. return true;
  181. }
  182. }
  183. //Pi=forward power
  184. public class TruPlasmaRF1001PreSetPiValueHandler : TruPlasmaRF1001Handler
  185. {
  186. private static byte _STAT = 0x00;//No error, data follows.
  187. private static byte _TYP = 0x04;//SINT32
  188. public TruPlasmaRF1001PreSetPiValueHandler(TruPlasmaRF1001 device, int piValue)
  189. : base(device, "02,06,00,01", IntToByteArray(piValue))
  190. {
  191. }
  192. private static byte[] IntToByteArray(int num)
  193. {
  194. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  195. tmp.AddRange(BitConverter.GetBytes(num));
  196. return tmp.ToArray();
  197. }
  198. public override bool HandleMessage(MessageBase msg, out bool handled)
  199. {
  200. if (base.HandleMessage(msg, out handled))
  201. {
  202. var result = msg as TruPlasmaRF1001Message;
  203. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  204. }
  205. return true;
  206. }
  207. }
  208. //Pi=forward power
  209. public class TruPlasmaRF1001ReadPiValueHandler : TruPlasmaRF1001Handler
  210. {
  211. public TruPlasmaRF1001ReadPiValueHandler(TruPlasmaRF1001 device)
  212. : base(device, "01,12,00,01", "FF")
  213. {
  214. }
  215. public override bool HandleMessage(MessageBase msg, out bool handled)
  216. {
  217. if (base.HandleMessage(msg, out handled))
  218. {
  219. var result = msg as TruPlasmaRF1001Message;
  220. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  221. if (result.Data != null && result.Data.Length == 4)
  222. {
  223. Device.ForwardPower = BitConverter.ToInt32(result.Data, 0);
  224. }
  225. }
  226. return true;
  227. }
  228. }
  229. //Pr=Reflected power
  230. public class TruPlasmaRF1001ReadPrValueHandler : TruPlasmaRF1001Handler
  231. {
  232. public TruPlasmaRF1001ReadPrValueHandler(TruPlasmaRF1001 device)
  233. : base(device, "01,14,00,01", "FF")
  234. {
  235. }
  236. public override bool HandleMessage(MessageBase msg, out bool handled)
  237. {
  238. if (base.HandleMessage(msg, out handled))
  239. {
  240. var result = msg as TruPlasmaRF1001Message;
  241. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  242. if (result.Data != null && result.Data.Length == 4)
  243. {
  244. Device.ReflectPower = BitConverter.ToInt32(result.Data, 0);
  245. }
  246. }
  247. return true;
  248. }
  249. }
  250. public class TruPlasmaRF1001ReadProcessStatusHandler : TruPlasmaRF1001Handler
  251. {
  252. public TruPlasmaRF1001ReadProcessStatusHandler(TruPlasmaRF1001 device)
  253. : base(device, "01,75,00,01", "FF")
  254. {
  255. }
  256. public override bool HandleMessage(MessageBase msg, out bool handled)
  257. {
  258. if (base.HandleMessage(msg, out handled))
  259. {
  260. var result = msg as TruPlasmaRF1001Message;
  261. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  262. if (result.Data != null && result.Data.Length == 4)
  263. {
  264. //0x00000010 = Power output on
  265. //0x00000080 = Alarm pending
  266. //0x00008000 = Temperature alarm pending
  267. var status = BitConverter.ToUInt32(result.Data, 0);
  268. Device.IsPowerOn = (status & 0x00000010) > 0;
  269. Device.IsError = (status & 0x00000080) > 0 || (status & 0x00008000) > 0;
  270. }
  271. }
  272. return true;
  273. }
  274. }
  275. public class TruPlasmaRF1001SetPowerOnOffHandler : TruPlasmaRF1001Handler
  276. {
  277. private static byte _STAT = 0x00;//No error, data follows.
  278. private static byte _TYP = 0x07;//UINT32
  279. public TruPlasmaRF1001SetPowerOnOffHandler(TruPlasmaRF1001 device, bool isOn)
  280. : base(device, "02,6F,00,01", IntToByteArray(isOn ? 1 : 0))
  281. {
  282. }
  283. private static byte[] IntToByteArray(int num)
  284. {
  285. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  286. tmp.AddRange(BitConverter.GetBytes((uint)num));
  287. return tmp.ToArray();
  288. }
  289. public override bool HandleMessage(MessageBase msg, out bool handled)
  290. {
  291. if (base.HandleMessage(msg, out handled))
  292. {
  293. var result = msg as TruPlasmaRF1001Message;
  294. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  295. }
  296. return true;
  297. }
  298. }
  299. public class TruPlasmaRF1001ReadPowerOnOffHandler : TruPlasmaRF1001Handler
  300. {
  301. public TruPlasmaRF1001ReadPowerOnOffHandler(TruPlasmaRF1001 device)
  302. : base(device, "01,6F,00,01", "FF")
  303. {
  304. }
  305. private static byte[] IntToByteArray(int num)
  306. {
  307. byte[] bytes = BitConverter.GetBytes(num);
  308. return bytes;
  309. }
  310. public override bool HandleMessage(MessageBase msg, out bool handled)
  311. {
  312. if (base.HandleMessage(msg, out handled))
  313. {
  314. var result = msg as TruPlasmaRF1001Message;
  315. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  316. if (result.Data != null && result.Data.Length == 4)
  317. {
  318. Device.IsPowerOn = BitConverter.ToUInt32(result.Data, 0) > 0;
  319. }
  320. }
  321. return true;
  322. }
  323. }
  324. public class TruPlasmaRF1001SetPulseModeHandler : TruPlasmaRF1001Handler
  325. {
  326. private static byte _STAT = 0x00;//No error, data follows.
  327. private static byte _TYP = 0x04;//INT32
  328. public TruPlasmaRF1001SetPulseModeHandler(TruPlasmaRF1001 device, EnumRfPowerWorkMode pulseMode)
  329. : base(device, "02,6A,01,01", IntToByteArray(pulseMode == EnumRfPowerWorkMode.PulsingMode ? 1 : 0))
  330. {
  331. }
  332. private static byte[] IntToByteArray(int num)
  333. {
  334. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  335. tmp.AddRange(BitConverter.GetBytes(num));
  336. return tmp.ToArray();
  337. }
  338. public override bool HandleMessage(MessageBase msg, out bool handled)
  339. {
  340. if (base.HandleMessage(msg, out handled))
  341. {
  342. var result = msg as TruPlasmaRF1001Message;
  343. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  344. }
  345. return true;
  346. }
  347. }
  348. public class TruPlasmaRF1001ReadPulseModeHandler : TruPlasmaRF1001Handler
  349. {
  350. public TruPlasmaRF1001ReadPulseModeHandler(TruPlasmaRF1001 device)
  351. : base(device, "01,6A,01,01", "FF")
  352. {
  353. }
  354. private static byte[] IntToByteArray(int num)
  355. {
  356. byte[] bytes = BitConverter.GetBytes(num);
  357. return bytes;
  358. }
  359. public override bool HandleMessage(MessageBase msg, out bool handled)
  360. {
  361. if (base.HandleMessage(msg, out handled))
  362. {
  363. var result = msg as TruPlasmaRF1001Message;
  364. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  365. if (result.Data != null && result.Data.Length == 4)
  366. {
  367. Device.WorkMode = BitConverter.ToInt32(result.Data, 0) == 1 ? EnumRfPowerWorkMode.PulsingMode : EnumRfPowerWorkMode.ContinuousWaveMode;
  368. }
  369. }
  370. return true;
  371. }
  372. }
  373. public class TruPlasmaRF1001SetClockOffsetModeHandler : TruPlasmaRF1001Handler
  374. {
  375. private static byte _STAT = 0x00;//No error, data follows.
  376. private static byte _TYP = 0x04;//INT32
  377. public TruPlasmaRF1001SetClockOffsetModeHandler(TruPlasmaRF1001 device, bool isFrequencyOffset)
  378. : base(device, "02,04,02,01", IntToByteArray(isFrequencyOffset ? 1 : 0))
  379. {
  380. }
  381. private static byte[] IntToByteArray(int num)
  382. {
  383. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  384. tmp.AddRange(BitConverter.GetBytes(num));
  385. return tmp.ToArray();
  386. }
  387. public override bool HandleMessage(MessageBase msg, out bool handled)
  388. {
  389. if (base.HandleMessage(msg, out handled))
  390. {
  391. var result = msg as TruPlasmaRF1001Message;
  392. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  393. }
  394. return true;
  395. }
  396. }
  397. // Tuning start offset
  398. public class TruPlasmaRF1001TuningOffsetHandler : TruPlasmaRF1001Handler
  399. {
  400. private static byte _STAT = 0x00;//No error, data follows.
  401. private static byte _TYP = 0x04;//INT32
  402. public TruPlasmaRF1001TuningOffsetHandler(TruPlasmaRF1001 device, int tuningOffset)
  403. : base(device, "02,46,02,01")
  404. {
  405. List<byte> offset = new List<byte>() { _STAT, _TYP };
  406. offset.AddRange(BitConverter.GetBytes(tuningOffset));
  407. SendBinary = BuildMessage(ToByteArray(_command), offset.ToArray());
  408. }
  409. public override bool HandleMessage(MessageBase msg, out bool handled)
  410. {
  411. if (base.HandleMessage(msg, out handled))
  412. {
  413. var result = msg as TruPlasmaRF1001Message;
  414. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  415. }
  416. return true;
  417. }
  418. }
  419. // Tuning delay
  420. public class TruPlasmaRF1001TuningDelayHandler : TruPlasmaRF1001Handler
  421. {
  422. private static byte _STAT = 0x00;//No error, data follows.
  423. private static byte _TYP = 0x04;//INT32
  424. public TruPlasmaRF1001TuningDelayHandler(TruPlasmaRF1001 device, int delay)
  425. : base(device, "02,49,02,01")
  426. {
  427. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  428. tmp.AddRange(BitConverter.GetBytes(delay));
  429. SendBinary = BuildMessage(ToByteArray(_command), tmp.ToArray());
  430. }
  431. public override bool HandleMessage(MessageBase msg, out bool handled)
  432. {
  433. if (base.HandleMessage(msg, out handled))
  434. {
  435. var result = msg as TruPlasmaRF1001Message;
  436. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  437. }
  438. return true;
  439. }
  440. }
  441. public class TruPlasmaRF1001SetFreqHandler : TruPlasmaRF1001Handler
  442. {
  443. private static byte _STAT = 0x00;//No error, data follows.
  444. private static byte _TYP = 0x04;//INT32
  445. //unit is Hz
  446. //the colck offset ode must be set to FrequencyOffset
  447. public TruPlasmaRF1001SetFreqHandler(TruPlasmaRF1001 device, int freq)
  448. : base(device, "02,C9,01,01", IntToByteArray(freq))
  449. {
  450. }
  451. private static byte[] IntToByteArray(int num)
  452. {
  453. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  454. tmp.AddRange(BitConverter.GetBytes(num));
  455. return tmp.ToArray();
  456. }
  457. public override bool HandleMessage(MessageBase msg, out bool handled)
  458. {
  459. if (base.HandleMessage(msg, out handled))
  460. {
  461. var result = msg as TruPlasmaRF1001Message;
  462. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  463. }
  464. return true;
  465. }
  466. }
  467. //457
  468. public class TruPlasmaRF1001ReadFreqOffsetHandler : TruPlasmaRF1001Handler
  469. {
  470. public TruPlasmaRF1001ReadFreqOffsetHandler(TruPlasmaRF1001 device)
  471. : base(device, "01,C9,01,01", "FF")
  472. {
  473. }
  474. private static byte[] IntToByteArray(int num)
  475. {
  476. byte[] bytes = BitConverter.GetBytes(num);
  477. return bytes;
  478. }
  479. public override bool HandleMessage(MessageBase msg, out bool handled)
  480. {
  481. if (base.HandleMessage(msg, out handled))
  482. {
  483. var result = msg as TruPlasmaRF1001Message;
  484. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  485. if (result.Data != null && result.Data.Length == 4)
  486. {
  487. Device.Frequency = BitConverter.ToInt32(result.Data, 0) / 1000;//unit kHz
  488. }
  489. }
  490. return true;
  491. }
  492. }
  493. //519
  494. public class TruPlasmaRF1001ReadActualFreqHandler : TruPlasmaRF1001Handler
  495. {
  496. public TruPlasmaRF1001ReadActualFreqHandler(TruPlasmaRF1001 device)
  497. : base(device, "01,07,02,01", "FF")
  498. {
  499. }
  500. private static byte[] IntToByteArray(int num)
  501. {
  502. byte[] bytes = BitConverter.GetBytes(num);
  503. return bytes;
  504. }
  505. public override bool HandleMessage(MessageBase msg, out bool handled)
  506. {
  507. if (base.HandleMessage(msg, out handled))
  508. {
  509. var result = msg as TruPlasmaRF1001Message;
  510. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  511. if (result.Data != null && result.Data.Length == 4)
  512. {
  513. Device.Frequency = BitConverter.ToInt32(result.Data, 0);//unit kHz
  514. }
  515. }
  516. return true;
  517. }
  518. }
  519. public class TruPlasmaRF1001SetClockModeHandler : TruPlasmaRF1001Handler
  520. {
  521. private static byte _STAT = 0x00;//No error, data follows.
  522. private static byte _TYP = 0x04;//INT32
  523. //0=Phase offset;1=Frequency offset;2=Frequency agility
  524. public TruPlasmaRF1001SetClockModeHandler(TruPlasmaRF1001 device, EnumRfPowerClockMode clockMode)
  525. : base(device, "02,04,02,01", IntToByteArray(GetValue(clockMode)))
  526. {
  527. }
  528. private static int GetValue(EnumRfPowerClockMode clockMode)
  529. {
  530. if (clockMode == EnumRfPowerClockMode.FreqAuto)
  531. return 2;
  532. if (clockMode == EnumRfPowerClockMode.FreqManual)
  533. return 1;
  534. return 0;
  535. }
  536. private static byte[] IntToByteArray(int num)
  537. {
  538. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  539. tmp.AddRange(BitConverter.GetBytes(num));
  540. return tmp.ToArray();
  541. }
  542. public override bool HandleMessage(MessageBase msg, out bool handled)
  543. {
  544. if (base.HandleMessage(msg, out handled))
  545. {
  546. var result = msg as TruPlasmaRF1001Message;
  547. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  548. }
  549. return true;
  550. }
  551. }
  552. public class TruPlasmaRF1001ReadClockModeHandler : TruPlasmaRF1001Handler
  553. {
  554. public TruPlasmaRF1001ReadClockModeHandler(TruPlasmaRF1001 device)
  555. : base(device, "01,04,02,01", "FF")
  556. {
  557. }
  558. private static byte[] IntToByteArray(int num)
  559. {
  560. byte[] bytes = BitConverter.GetBytes(num);
  561. return bytes;
  562. }
  563. public override bool HandleMessage(MessageBase msg, out bool handled)
  564. {
  565. if (base.HandleMessage(msg, out handled))
  566. {
  567. var result = msg as TruPlasmaRF1001Message;
  568. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  569. if (result.Data != null && result.Data.Length == 4)
  570. {
  571. Device.ClockMode = GetMode(BitConverter.ToInt32(result.Data, 0));
  572. }
  573. }
  574. return true;
  575. }
  576. private EnumRfPowerClockMode GetMode(int mode)
  577. {
  578. if (mode == 2)
  579. return EnumRfPowerClockMode.FreqAuto;
  580. if (mode == 1)
  581. return EnumRfPowerClockMode.FreqManual;
  582. return EnumRfPowerClockMode.Phase;
  583. }
  584. }
  585. public class TruPlasmaRF1001SetGainHandler : TruPlasmaRF1001Handler
  586. {
  587. private static byte _STAT = 0x00;//No error, data follows.
  588. private static byte _TYP = 0x04;//INT32
  589. public TruPlasmaRF1001SetGainHandler(TruPlasmaRF1001 device, int gain)
  590. : base(device, "02,4C,02,01", IntToByteArray(gain))
  591. {
  592. }
  593. private static byte[] IntToByteArray(int num)
  594. {
  595. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  596. tmp.AddRange(BitConverter.GetBytes(num));
  597. return tmp.ToArray();
  598. }
  599. public override bool HandleMessage(MessageBase msg, out bool handled)
  600. {
  601. if (base.HandleMessage(msg, out handled))
  602. {
  603. var result = msg as TruPlasmaRF1001Message;
  604. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  605. }
  606. return true;
  607. }
  608. }
  609. public class TruPlasmaRF1001SetRelativeGainHandler : TruPlasmaRF1001Handler
  610. {
  611. private static byte _STAT = 0x00;//No error, data follows.
  612. private static byte _TYP = 0x04;//INT32
  613. public TruPlasmaRF1001SetRelativeGainHandler(TruPlasmaRF1001 device, int gain)
  614. : base(device, "02,4D,02,01", IntToByteArray(gain))
  615. {
  616. }
  617. private static byte[] IntToByteArray(int num)
  618. {
  619. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  620. tmp.AddRange(BitConverter.GetBytes(num));
  621. return tmp.ToArray();
  622. }
  623. public override bool HandleMessage(MessageBase msg, out bool handled)
  624. {
  625. if (base.HandleMessage(msg, out handled))
  626. {
  627. var result = msg as TruPlasmaRF1001Message;
  628. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  629. }
  630. return true;
  631. }
  632. }
  633. public class TruPlasmaRF1001SetModulationDeviationHandler : TruPlasmaRF1001Handler
  634. {
  635. private static byte _STAT = 0x00;//No error, data follows.
  636. private static byte _TYP = 0x04;//INT32
  637. public TruPlasmaRF1001SetModulationDeviationHandler(TruPlasmaRF1001 device, int deviation)
  638. : base(device, "02,4E,02,01", IntToByteArray(deviation))
  639. {
  640. }
  641. private static byte[] IntToByteArray(int num)
  642. {
  643. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  644. tmp.AddRange(BitConverter.GetBytes(num));
  645. return tmp.ToArray();
  646. }
  647. public override bool HandleMessage(MessageBase msg, out bool handled)
  648. {
  649. if (base.HandleMessage(msg, out handled))
  650. {
  651. var result = msg as TruPlasmaRF1001Message;
  652. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  653. }
  654. return true;
  655. }
  656. }
  657. public class TruPlasmaRF1001SetRelativeModulationDeviationHandler : TruPlasmaRF1001Handler
  658. {
  659. private static byte _STAT = 0x00;//No error, data follows.
  660. private static byte _TYP = 0x04;//INT32
  661. public TruPlasmaRF1001SetRelativeModulationDeviationHandler(TruPlasmaRF1001 device, int deviation)
  662. : base(device, "02,4F,02,01", IntToByteArray(deviation))
  663. {
  664. }
  665. private static byte[] IntToByteArray(int num)
  666. {
  667. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  668. tmp.AddRange(BitConverter.GetBytes(num));
  669. return tmp.ToArray();
  670. }
  671. public override bool HandleMessage(MessageBase msg, out bool handled)
  672. {
  673. if (base.HandleMessage(msg, out handled))
  674. {
  675. var result = msg as TruPlasmaRF1001Message;
  676. Device.NoteRawCommandInfo(_command, string.Join(",", result.RawMessage.Select(bt => bt.ToString("X2")).ToArray()));
  677. }
  678. return true;
  679. }
  680. }
  681. public class TruPlasmaRF1001ResetHandler : TruPlasmaRF1001Handler
  682. {
  683. private static byte _STAT = 0x00;//No error, data follows.
  684. private static byte _TYP = 0x04;//INT32
  685. public TruPlasmaRF1001ResetHandler(TruPlasmaRF1001 device)
  686. : base(device, "02,4D,00,01", IntToByteArray(0xAA))
  687. {
  688. }
  689. private static byte[] IntToByteArray(int num)
  690. {
  691. List<byte> tmp = new List<byte>() { _STAT, _TYP };
  692. tmp.AddRange(BitConverter.GetBytes(num));
  693. return tmp.ToArray();
  694. }
  695. public override bool HandleMessage(MessageBase msg, out bool handled)
  696. {
  697. if (base.HandleMessage(msg, out handled))
  698. {
  699. }
  700. return true;
  701. }
  702. }
  703. }