ComBin.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. using IvfTl.Control.Entity;
  2. using IvfTl.Control.Entity.GlobalEnums;
  3. using ivf_tl_UtilHelper;
  4. using System.Diagnostics;
  5. namespace ivf_tl_SerialHelper.Util
  6. {
  7. /// <summary>
  8. /// 封装指令发送以及解析线程
  9. /// </summary>
  10. public class ComBin
  11. {
  12. /// <summary>
  13. /// 异常日志
  14. /// 异常、名称、参数
  15. /// </summary>
  16. public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
  17. /// <summary>
  18. /// 错误日志
  19. /// </summary>
  20. public event Action<string, LogEnum> ErrorLogEvent;
  21. /// <summary>
  22. /// 指令记录日志
  23. /// </summary>
  24. public event Action<int, DateTime, string, LogEnum> CommandLogEvent;
  25. /// <summary>
  26. /// 串口状态
  27. /// </summary>
  28. public event Action<int> ComStateEvent;
  29. public ComBin(int houseId, string portName)
  30. {
  31. this.houseId = houseId;
  32. this.portName = portName;
  33. _commandQueue = new Queue<CustomProtocol>();
  34. _channel = new Channel(houseId, portName);
  35. _channel.DataReceived += _channel_DataReceived;
  36. _channel.ExceptionLogEvent += _channel_ExceptionLogEvent;
  37. _channel.ErrorLogEvent += _channel_ErrorLogEvent;
  38. _channel.CommandLogEvent += _channel_CommandLogEvent; ;
  39. StartSendCommandThread();
  40. }
  41. private void _channel_CommandLogEvent(int arg1, DateTime arg2, string arg3, LogEnum arg4)
  42. {
  43. CommandLogEvent?.Invoke(arg1, arg2, arg3, arg4);
  44. }
  45. private void _channel_ErrorLogEvent(string arg1, LogEnum arg2)
  46. {
  47. ErrorLogEvent?.Invoke(arg1, arg2);
  48. }
  49. private void _channel_ExceptionLogEvent(Exception arg1, string arg2, string arg3, LogEnum arg4)
  50. {
  51. ExceptionLogEvent?.Invoke(arg1, arg2, arg3, arg4);
  52. }
  53. public Channel _channel { get; set; } = null;
  54. /// <summary>
  55. /// 是否停止发送指令线程循环
  56. /// </summary>
  57. public bool IsStop = false;
  58. /// <summary>
  59. /// 指令发送失败以后是否无限重发
  60. /// </summary>
  61. public bool IsStopWhile = true;
  62. private int houseId = 0;
  63. private string portName = null;
  64. /// <summary>
  65. /// 命令队列
  66. /// </summary>
  67. private Queue<CustomProtocol> _commandQueue;
  68. /// <summary>
  69. /// 指令超时重发等待毫秒
  70. /// </summary>
  71. private int milliseconds = 1000 * 30;
  72. /// <summary>
  73. /// 发送指令的线程同步控制器
  74. /// </summary>
  75. private AutoResetEvent sendAutoResetEvent = new AutoResetEvent(false);
  76. /// <summary>
  77. /// 运行线程等待指令的同步控制器
  78. /// </summary>
  79. private AutoResetEvent taskAutoResetEvent = new AutoResetEvent(false);
  80. /// <summary>
  81. /// 指令发送锁
  82. /// </summary>
  83. private object SendCommandLock = new object();
  84. /// <summary>
  85. /// 指令计数
  86. /// </summary>
  87. private double commandNumber = 1;
  88. /// <summary>
  89. /// 串口状态 0正常 1异常
  90. /// </summary>
  91. private int ComState = -1;
  92. /// <summary>
  93. /// 开启发送指令线程
  94. /// </summary>
  95. public void StartSendCommandThread()
  96. {
  97. Task.Factory.StartNew(() =>
  98. {
  99. Stopwatch SendStopWatch = new Stopwatch();
  100. CustomProtocol customProtocol = null;
  101. while (true)
  102. {
  103. try
  104. {
  105. if (IsStop)
  106. {
  107. return;
  108. }
  109. lock (_commandQueue)
  110. {
  111. if (_commandQueue.Any())
  112. {
  113. customProtocol = _commandQueue.Dequeue();
  114. }
  115. }
  116. if (customProtocol != null)
  117. {
  118. SendStopWatch.Restart();
  119. string Content = $"[{houseId}][{portName}][{customProtocol.commandNumber}][{customProtocol.commandType}][Send:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}]";
  120. customProtocol.IsSuccess = SendCommand(customProtocol, Content);
  121. string Content11 = $"{Content}[Rec:{StringHelper.ByteToHexStr(customProtocol.receivedBuffer)}]";
  122. if (customProtocol.WaitTime != 0)
  123. {
  124. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[等待{customProtocol.WaitTime}毫秒]", LogEnum.HouseComRecord);
  125. Thread.Sleep(customProtocol.WaitTime);
  126. }
  127. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[本轮指令总耗时:{StringHelper.TimeToString(SendStopWatch.Elapsed)}][End]", LogEnum.HouseComRecord);
  128. SendStopWatch.Stop();
  129. if (customProtocol.IsWaitOne)
  130. {
  131. taskAutoResetEvent.Set();
  132. }
  133. customProtocol = null;
  134. }
  135. else
  136. {
  137. //CommandLogEvent?.Invoke(houseId, DateTime.Now, $"空指令,等待50毫秒", LogEnum.HouseComRecord);
  138. Thread.Sleep(50);
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. ExceptionLogEvent?.Invoke(ex, $"[{houseId}][{portName}]指令发送线程", null, LogEnum.RunException);
  144. }
  145. }
  146. }, TaskCreationOptions.LongRunning);
  147. }
  148. /// <summary>
  149. /// 指令发送
  150. /// </summary>
  151. /// <param name="customProtocol"></param>
  152. /// <param name="Content"></param>
  153. private bool SendCommand(CustomProtocol customProtocol, string Content)
  154. {
  155. try
  156. {
  157. //超时重发3次后表示通讯中断
  158. var result = false;
  159. bool isopen = true;
  160. bool sendRs = false;
  161. do
  162. {
  163. for (int i = 0; i < 3; i++)
  164. {
  165. if (isopen)
  166. {
  167. sendRs = _channel.Send(customProtocol);
  168. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次发送结果:{sendRs}]", LogEnum.HouseComRecord);
  169. }
  170. if (sendRs)
  171. {
  172. result = sendAutoResetEvent.WaitOne(milliseconds);//延时等待下位机回复
  173. if (result)
  174. {
  175. break;
  176. }
  177. else
  178. {
  179. sendRs = false;
  180. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}第{i + 1}次等待超时", LogEnum.HouseComRecord);
  181. ErrorLogEvent?.Invoke($"{Content}第{i + 1}次等待超时", LogEnum.RunError);
  182. if (i != 2)
  183. {
  184. isopen = _channel.ReopenPort();
  185. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次重新打开串口结果:{isopen}]", LogEnum.HouseComRecord);
  186. if (!isopen)
  187. {
  188. ErrorLogEvent?.Invoke($"{Content}第{i + 1}次重新打开串口失败", LogEnum.RunError);
  189. }
  190. }
  191. }
  192. }
  193. else
  194. {
  195. ErrorLogEvent?.Invoke($"{Content}第{i + 1}次发送失败", LogEnum.RunError);
  196. if (i != 2)
  197. {
  198. Thread.Sleep(milliseconds);
  199. isopen = _channel.ReopenPort();
  200. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次重新打开串口结果:{isopen}]", LogEnum.HouseComRecord);
  201. if (!isopen)
  202. {
  203. ErrorLogEvent?.Invoke($"{Content}第{i + 1}次重新打开串口失败", LogEnum.RunError);
  204. }
  205. }
  206. }
  207. }
  208. //ErrorLogEvent?.Invoke($"{result}、{IsStopWhile}", LogEnum.RunError);
  209. if (!result && !IsStopWhile)
  210. {
  211. return false;
  212. }
  213. ComlossAlarm(result);
  214. } while (!result);
  215. return true;
  216. }
  217. catch (Exception ex)
  218. {
  219. ExceptionLogEvent?.Invoke(ex, $"{Content}指令发送", null, LogEnum.RunException);
  220. return false;
  221. }
  222. }
  223. private void _channel_DataReceived(CustomProtocol obj)
  224. {
  225. sendAutoResetEvent.Set();
  226. }
  227. /// <summary>
  228. /// 发送封装
  229. /// </summary>
  230. /// <param name="custom"></param>
  231. private void Enqueue(CustomProtocol custom)
  232. {
  233. lock (_commandQueue)
  234. {
  235. _commandQueue.Enqueue(custom);
  236. }
  237. }
  238. public bool OpenPort()
  239. {
  240. return _channel.OpenPort();
  241. }
  242. public bool ClosePort()
  243. {
  244. return _channel.ClosePort();
  245. }
  246. /// <summary>
  247. /// 通讯报警
  248. /// </summary>
  249. /// <param name="result"></param>
  250. public void ComlossAlarm(bool result)
  251. {
  252. if(!result)
  253. {
  254. if(ComState != 1)
  255. {
  256. ComState = 1;
  257. ComStateEvent?.Invoke(1);
  258. }
  259. return;
  260. }
  261. if (ComState == 0) return;
  262. ComState = 0;
  263. ComStateEvent?.Invoke(0);
  264. }
  265. #region 仓室状态
  266. /// <summary>
  267. /// 握手指令
  268. /// </summary>
  269. public int ShakeHandsWait(CustomProtocol custom)
  270. {
  271. //握手
  272. custom.logDateTime = DateTime.Now;
  273. custom.commandNumber = commandNumber++;
  274. custom.commandType = Enums.ShakeHands;
  275. custom.CreateHandCommand();
  276. custom.IsWaitOne = true;
  277. Enqueue(custom);
  278. taskAutoResetEvent.WaitOne();
  279. if (!custom.IsSuccess) return -1;
  280. return Analysiser.ParseShakeHandsCommand(custom.receivedBuffer);
  281. }
  282. /// <summary>
  283. /// 读仓门状态
  284. /// </summary>
  285. /// <param name="custom"></param>
  286. /// <returns></returns>
  287. public State DoorStatusWait(CustomProtocol custom)
  288. {
  289. custom.commandType = Enums.DoorStatus;
  290. custom.logDateTime = DateTime.Now;
  291. custom.commandNumber = commandNumber++;
  292. custom.CreateReadDoorCommand();
  293. custom.IsWaitOne = true;
  294. Enqueue(custom);
  295. taskAutoResetEvent.WaitOne();
  296. if (!custom.IsSuccess) return State.未知;
  297. return Analysiser.ParseDoorStatus(custom.receivedBuffer);
  298. }
  299. /// <summary>
  300. /// 仓室温度
  301. /// </summary>
  302. /// <param name="custom"></param>
  303. /// <returns></returns>
  304. public decimal TemperatureWait(CustomProtocol custom)
  305. {
  306. custom.commandType = Enums.Temperature;
  307. custom.logDateTime = DateTime.Now;
  308. custom.commandNumber = commandNumber++;
  309. custom.CreateReadTemperatureCommand();
  310. custom.IsWaitOne = true;
  311. Enqueue(custom);
  312. taskAutoResetEvent.WaitOne();
  313. if (!custom.IsSuccess) return -1m;
  314. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  315. }
  316. /// <summary>
  317. /// 上盖板温度
  318. /// </summary>
  319. /// <param name="custom"></param>
  320. /// <returns></returns>
  321. public decimal ShangTemperatureWait(CustomProtocol custom)
  322. {
  323. custom.commandType = Enums.Temperature;
  324. custom.logDateTime = DateTime.Now;
  325. custom.commandNumber = commandNumber++;
  326. custom.CreateReadShangTemperatureCommand();
  327. custom.IsWaitOne = true;
  328. Enqueue(custom);
  329. taskAutoResetEvent.WaitOne();
  330. if (!custom.IsSuccess) return -1m;
  331. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  332. }
  333. /// <summary>
  334. /// 玻璃片下方温度
  335. /// </summary>
  336. /// <param name="custom"></param>
  337. /// <returns></returns>
  338. public decimal BoLiTemperatureWait(CustomProtocol custom)
  339. {
  340. custom.commandType = Enums.BoLiTemperature;
  341. custom.logDateTime = DateTime.Now;
  342. custom.commandNumber = commandNumber++;
  343. custom.CreateReadBoLiTemperatureCommand();
  344. custom.IsWaitOne = true;
  345. Enqueue(custom);
  346. taskAutoResetEvent.WaitOne();
  347. if (!custom.IsSuccess) return -1m;
  348. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  349. }
  350. /// <summary>
  351. /// 仓室压力
  352. /// </summary>
  353. /// <param name="custom"></param>
  354. /// <returns></returns>
  355. public decimal PressureWait(CustomProtocol custom)
  356. {
  357. custom.commandType = Enums.Pressure;
  358. custom.logDateTime = DateTime.Now;
  359. custom.commandNumber = commandNumber++;
  360. custom.CreateReadPressureCommand();
  361. custom.IsWaitOne = true;
  362. Enqueue(custom);
  363. taskAutoResetEvent.WaitOne();
  364. if (!custom.IsSuccess) return -1m;
  365. return Analysiser.ParsePressureCommand(custom.receivedBuffer);
  366. }
  367. /// <summary>
  368. /// 获取缓冲瓶状态
  369. /// </summary>
  370. /// <returns></returns>
  371. public (decimal, decimal, decimal) BufferBottleState(CustomProtocol custom)
  372. {
  373. custom.commandType = Enums.BufferBottlePressure;
  374. custom.logDateTime = DateTime.Now;
  375. custom.commandNumber = commandNumber++;
  376. custom.CreateBufferBottlePressureCommand();
  377. custom.IsWaitOne = true;
  378. Enqueue(custom);
  379. taskAutoResetEvent.WaitOne();
  380. if (!custom.IsSuccess) return (-1m,-1m,-1m);
  381. return (Analysiser.ParsePressureCommand(custom.receivedBuffer), Analysiser.ParseTLTemperature1Command(custom.receivedBuffer), Analysiser.ParseTLTemperature2Command(custom.receivedBuffer));
  382. }
  383. #endregion
  384. #region 电机
  385. /// <summary>
  386. /// 水平电机复位
  387. /// </summary>
  388. /// <param name="custom"></param>
  389. /// <param name="waitTime"></param>
  390. public bool HorizontalMotorResetWait(CustomProtocol custom, int waitTime)
  391. {
  392. custom.commandType = Enums.HorizontalMotorReset;
  393. custom.logDateTime = DateTime.Now;
  394. custom.commandNumber = commandNumber++;
  395. custom.CreateHorizontalMotorResetCommand();
  396. custom.IsWaitOne = true;
  397. Enqueue(custom);
  398. taskAutoResetEvent.WaitOne();
  399. if (waitTime > 0)
  400. {
  401. Thread.Sleep(waitTime);
  402. }
  403. return custom.IsSuccess;
  404. }
  405. /// <summary>
  406. /// 水平电机绝对运动
  407. /// </summary>
  408. /// <param name="custom"></param>
  409. /// <param name="waitTime"></param>
  410. /// <param name="zero"></param>
  411. /// <param name="newValue"></param>
  412. public bool HorizontalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int newValue)
  413. {
  414. custom.commandType = Enums.HorizontalMotorAbsolute;
  415. custom.logDateTime = DateTime.Now;
  416. custom.commandNumber = commandNumber++;
  417. custom.HorizontalMotorPulse = newValue;
  418. custom.CreateHorizontalMotorMoveToCommand(newValue);
  419. custom.IsWaitOne = true;
  420. Enqueue(custom);
  421. taskAutoResetEvent.WaitOne();
  422. if (waitTime > 0)
  423. {
  424. Thread.Sleep(waitTime);
  425. }
  426. return custom.IsSuccess;
  427. }
  428. /// <summary>
  429. /// 读水平电机位置
  430. /// </summary>
  431. public int ReadHorizontalMotorWait(CustomProtocol custom)
  432. {
  433. custom.commandType = Enums.ReadHorizontalMotor;
  434. custom.logDateTime = DateTime.Now;
  435. custom.commandNumber = commandNumber++;
  436. custom.CreateReadHorizontalMotorCommand();
  437. custom.IsWaitOne = true;
  438. Enqueue(custom);
  439. taskAutoResetEvent.WaitOne();
  440. if (!custom.IsSuccess) return -1;
  441. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  442. }
  443. /// <summary>
  444. /// 读垂直电机位置
  445. /// </summary>
  446. public int ReadVerticalMotorWait(CustomProtocol custom)
  447. {
  448. custom.commandType = Enums.ReadVerticalMotor;
  449. custom.logDateTime = DateTime.Now;
  450. custom.commandNumber = commandNumber++;
  451. custom.CreateReadVerticalMotorCommand();
  452. custom.IsWaitOne = true;
  453. Enqueue(custom);
  454. taskAutoResetEvent.WaitOne();
  455. if (!custom.IsSuccess) return -1;
  456. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  457. }
  458. /// <summary>
  459. /// 垂直电机复位
  460. /// </summary>
  461. public bool VerticalMotorResetWait(CustomProtocol custom, int waitTime)
  462. {
  463. custom.commandType = Enums.VerticalMotorReset;
  464. custom.logDateTime = DateTime.Now;
  465. custom.commandNumber = commandNumber++;
  466. custom.CreateVerticalMotorResetCommand();
  467. custom.IsWaitOne = true;
  468. Enqueue(custom);
  469. taskAutoResetEvent.WaitOne();
  470. if (waitTime > 0)
  471. {
  472. Thread.Sleep(waitTime);
  473. }
  474. return custom.IsSuccess;
  475. }
  476. /// <summary>
  477. /// 垂直电机绝对运动
  478. /// </summary>
  479. /// <param name="custom">指令对象</param>
  480. /// <param name="waitTime">电机运动等待时间</param>
  481. /// <param name="currentVer">垂直电机目标位置</param>
  482. /// <param name="currentHor">当前水平电机位置</param>
  483. /// <param name="pictureId">图片编号</param>
  484. /// <param name="well">当前well</param>
  485. /// <param name="focal">当前层数</param>
  486. public bool VerticalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int currentVer, int currentHor, int pictureId, int well, int focal)
  487. {
  488. custom.commandType = Enums.VerticalMotorAbsolute;
  489. custom.logDateTime = DateTime.Now;
  490. custom.commandNumber = commandNumber++;
  491. custom.VerticalMotorPulse = currentVer;
  492. custom.HorizontalMotorPulse = currentHor;
  493. custom.pictureId = pictureId;
  494. custom.well = well;
  495. custom.focal = focal;
  496. custom.CreateVerticalMotorAbsoluteMovementCommand(currentVer);
  497. custom.IsWaitOne = true;
  498. Enqueue(custom);
  499. taskAutoResetEvent.WaitOne();
  500. if (waitTime > 0)
  501. {
  502. Thread.Sleep(waitTime);
  503. }
  504. return custom.IsSuccess;
  505. }
  506. #endregion
  507. #region 控制
  508. /// <summary>
  509. /// 仓室补气
  510. /// </summary>
  511. /// <param name="custom"></param>
  512. public bool HouseAerationWait(CustomProtocol custom)
  513. {
  514. custom.commandType = Enums.HouseAeration;
  515. custom.logDateTime = DateTime.Now;
  516. custom.commandNumber = commandNumber++;
  517. custom.CreateHouseAerationCommand();
  518. custom.IsWaitOne = true;
  519. Enqueue(custom);
  520. taskAutoResetEvent.WaitOne();
  521. return custom.IsSuccess;
  522. }
  523. /// <summary>
  524. /// 仓室排气
  525. /// </summary>
  526. /// <param name="custom"></param>
  527. public bool HouseVentWait(CustomProtocol custom)
  528. {
  529. custom.commandType = Enums.HouseVent;
  530. custom.logDateTime = DateTime.Now;
  531. custom.commandNumber = commandNumber++;
  532. custom.CreateHouseVentCommand();
  533. custom.IsWaitOne = true;
  534. Enqueue(custom);
  535. taskAutoResetEvent.WaitOne();
  536. return custom.IsSuccess;
  537. }
  538. /// <summary>
  539. /// 缓冲瓶补气
  540. /// </summary>
  541. /// <param name="custom"></param>
  542. public bool BufferBottleAerationWait(CustomProtocol custom)
  543. {
  544. custom.commandType = Enums.BufferBottleAeration;
  545. custom.logDateTime = DateTime.Now;
  546. custom.commandNumber = commandNumber++;
  547. custom.CreateBufferBottleAerationCommand();
  548. custom.IsWaitOne = true;
  549. Enqueue(custom);
  550. taskAutoResetEvent.WaitOne();
  551. return custom.IsSuccess;
  552. }
  553. /// <summary>
  554. /// 关闭仓室进气阀
  555. /// </summary>
  556. /// <param name="custom"></param>
  557. /// <param name="waitTime"></param>
  558. public bool CloseIntakeValveWait(CustomProtocol custom, int waitTime)
  559. {
  560. custom.commandType = Enums.CloseIntakeValve;
  561. custom.logDateTime = DateTime.Now;
  562. custom.commandNumber = commandNumber++;
  563. custom.CreateCloseIntakeValveCommand();
  564. custom.IsWaitOne = true;
  565. Enqueue(custom);
  566. taskAutoResetEvent.WaitOne();
  567. if (waitTime > 0)
  568. {
  569. Thread.Sleep(waitTime);
  570. }
  571. return custom.IsSuccess;
  572. }
  573. /// <summary>
  574. /// 打开仓室排气阀
  575. /// </summary>
  576. /// <param name="custom"></param>
  577. public bool OpenExhaustValveWait(CustomProtocol custom, int waitTime)
  578. {
  579. custom.commandType = Enums.OpenExhaustValve;
  580. custom.logDateTime = DateTime.Now;
  581. custom.commandNumber = commandNumber++;
  582. custom.CreateOpenExhaustValveCommand();
  583. custom.IsWaitOne = true;
  584. Enqueue(custom);
  585. taskAutoResetEvent.WaitOne();
  586. if (waitTime > 0)
  587. {
  588. Thread.Sleep(waitTime);
  589. }
  590. return custom.IsSuccess;
  591. }
  592. /// <summary>
  593. /// 打开舱室进气阀
  594. /// </summary>
  595. /// <param name="custom"></param>
  596. /// <param name="waitTime"></param>
  597. /// <returns></returns>
  598. public bool OpenIntakeValveWait(CustomProtocol custom, int waitTime)
  599. {
  600. custom.commandType = Enums.OpenIntakeValve;
  601. custom.logDateTime = DateTime.Now;
  602. custom.commandNumber = commandNumber++;
  603. custom.CreateOpenIntakeValveCommand();
  604. custom.IsWaitOne = true;
  605. Enqueue(custom);
  606. taskAutoResetEvent.WaitOne();
  607. if (waitTime > 0)
  608. {
  609. Thread.Sleep(waitTime);
  610. }
  611. return custom.IsSuccess;
  612. }
  613. /// <summary>
  614. /// 关闭排气阀
  615. /// </summary>
  616. /// <param name="custom"></param>
  617. /// <param name="waitTime"></param>
  618. public void CloseExhaustValveWait(CustomProtocol custom, int waitTime)
  619. {
  620. custom.commandType = Enums.CloseExhaustValve;
  621. custom.logDateTime = DateTime.Now;
  622. custom.commandNumber = commandNumber++;
  623. custom.CreateCloseExhaustValveCommand();
  624. custom.IsWaitOne = true;
  625. Enqueue(custom);
  626. taskAutoResetEvent.WaitOne();
  627. if (waitTime > 0)
  628. {
  629. Thread.Sleep(waitTime);
  630. }
  631. }
  632. /// <summary>
  633. /// 打开LEd灯
  634. /// </summary>
  635. /// <param name="custom"></param>
  636. public void OpenLEDWait(CustomProtocol custom)
  637. {
  638. custom.commandType = Enums.OpenLED;
  639. custom.logDateTime = DateTime.Now;
  640. custom.commandNumber = commandNumber++;
  641. custom.CreateOpenLEDCommand();
  642. custom.IsWaitOne = true;
  643. Enqueue(custom);
  644. taskAutoResetEvent.WaitOne();
  645. }
  646. /// <summary>
  647. /// 关闭Led灯
  648. /// </summary>
  649. /// <param name="custom"></param>
  650. public void CloseLEDWait(CustomProtocol custom)
  651. {
  652. custom.commandType = Enums.CloseLED;
  653. custom.logDateTime = DateTime.Now;
  654. custom.commandNumber = commandNumber++;
  655. custom.CreateCloseLEDCommand();
  656. custom.IsWaitOne = true;
  657. Enqueue(custom);
  658. taskAutoResetEvent.WaitOne();
  659. }
  660. #endregion
  661. #region 参数
  662. /// <summary>
  663. /// 读取TL仪器编号
  664. /// </summary>
  665. public int ReadTLNumWait(CustomProtocol custom)
  666. {
  667. custom.commandType = Enums.ReadTLNum;
  668. custom.logDateTime = DateTime.Now;
  669. custom.commandNumber = commandNumber++;
  670. custom.CreateReadTLNumCommand();
  671. custom.IsWaitOne = true;
  672. Enqueue(custom);
  673. taskAutoResetEvent.WaitOne();
  674. if (!custom.IsSuccess) return -1;
  675. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  676. }
  677. /// <summary>
  678. /// 获取CCDSN
  679. /// </summary>
  680. public int GetCCDSNWait(CustomProtocol custom)
  681. {
  682. custom.commandType = Enums.GetCCDSN;
  683. custom.logDateTime = DateTime.Now;
  684. custom.commandNumber = commandNumber++;
  685. custom.CreateGetModuleCommand();
  686. custom.IsWaitOne = true;
  687. Enqueue(custom);
  688. taskAutoResetEvent.WaitOne();
  689. if (!custom.IsSuccess) return -1;
  690. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  691. }
  692. /// <summary>
  693. /// 读E方-->水平电机位置
  694. /// </summary>
  695. public int ReadEEPROMhoriMtWellHorHorWait(CustomProtocol custom,int horIndex)
  696. {
  697. custom.commandType = Enums.ReadEEPROMhoriMtWellHor;
  698. custom.logDateTime = DateTime.Now;
  699. custom.commandNumber = commandNumber++;
  700. custom.CreateReadEEPROMhoriMtWellHoriPos(horIndex);
  701. custom.IsWaitOne = true;
  702. Enqueue(custom);
  703. taskAutoResetEvent.WaitOne();
  704. if (!custom.IsSuccess) return -1;
  705. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  706. }
  707. /// <summary>
  708. /// 读E方-->垂直电机清晰位置
  709. /// </summary>
  710. /// <returns></returns>
  711. public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom)
  712. {
  713. custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
  714. custom.logDateTime = DateTime.Now;
  715. custom.commandNumber = commandNumber++;
  716. custom.CreateReadEEPROMvertMtStartPulse(1);
  717. custom.IsWaitOne = true;
  718. Enqueue(custom);
  719. taskAutoResetEvent.WaitOne();
  720. if (!custom.IsSuccess) return -1;
  721. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  722. }
  723. /// <summary>
  724. /// 读E方-->垂直电机间隔脉冲
  725. /// </summary>
  726. /// <returns></returns>
  727. public int ReadEEPROMvertMtScanPluseWait(CustomProtocol custom)
  728. {
  729. custom.commandType = Enums.ReadEEPROMvertMtScanPluse;
  730. custom.logDateTime = DateTime.Now;
  731. custom.commandNumber = commandNumber++;
  732. custom.IsWaitOne = true;
  733. custom.CreateReadEEPROMvertMtScanPluse();
  734. Enqueue( custom);//断层扫描间隔数
  735. taskAutoResetEvent.WaitOne();
  736. if (!custom.IsSuccess) return -1;
  737. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  738. }
  739. /// <summary>
  740. /// 读E方-->仓室进气阀打开时间
  741. /// </summary>
  742. /// <returns></returns>
  743. public int ReadEEPROOpenIntakeTimeWait(CustomProtocol custom)
  744. {
  745. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  746. custom.logDateTime = DateTime.Now;
  747. custom.commandNumber = commandNumber++;
  748. custom.IsWaitOne = true;
  749. custom.CreateReadEEPROOpenIntakeTimeCommand();
  750. Enqueue( custom);
  751. taskAutoResetEvent.WaitOne();
  752. if (!custom.IsSuccess) return -1;
  753. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  754. }
  755. /// <summary>
  756. /// 读E方-->缓冲瓶进气阀打开时间
  757. /// </summary>
  758. /// <returns></returns>
  759. public int ReadEEPROOpenIntakeTimeBufferWait(CustomProtocol custom)
  760. {
  761. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  762. custom.logDateTime = DateTime.Now;
  763. custom.commandNumber = commandNumber++;
  764. custom.IsWaitOne = true;
  765. custom.CreateReadEEPROOpenIntakeTimeBufferCommand();
  766. Enqueue(custom);
  767. taskAutoResetEvent.WaitOne();
  768. if (!custom.IsSuccess) return -1;
  769. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  770. }
  771. /// <summary>
  772. /// 读E方-->下加热板目标温度
  773. /// </summary>
  774. /// <returns></returns>
  775. public decimal ReadTargetTempWait(CustomProtocol custom)
  776. {
  777. custom.commandType = Enums.ReadTargetTemp;
  778. custom.logDateTime = DateTime.Now;
  779. custom.commandNumber = commandNumber++;
  780. custom.CreateReadTargetTemp();
  781. custom.IsWaitOne = true;
  782. Enqueue( custom);
  783. taskAutoResetEvent.WaitOne();
  784. if (!custom.IsSuccess) return -1m;
  785. var num = Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  786. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  787. }
  788. /// <summary>
  789. /// 读E方-->灯光亮度
  790. /// </summary>
  791. /// <returns></returns>
  792. public int ReadEEPROMLightNumWait(CustomProtocol custom)
  793. {
  794. custom.commandType = Enums.ReadLightNum;
  795. custom.logDateTime = DateTime.Now;
  796. custom.commandNumber = commandNumber++;
  797. custom.IsWaitOne = true;
  798. custom.CreateReadEEPROMLightNum();
  799. Enqueue(custom);
  800. taskAutoResetEvent.WaitOne();
  801. if (!custom.IsSuccess) return -1;
  802. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  803. }
  804. #endregion
  805. #region 调试模式
  806. #endregion
  807. }
  808. }