ComBin.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. /// 忠实搬运 operate 侧 ComBin.HorizontalMotorForwardWait(命令构造器 control 侧已存在)。
  431. /// </summary>
  432. public bool HorizontalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
  433. {
  434. custom.commandType = Enums.HorizontalMotorForward;
  435. custom.logDateTime = DateTime.Now;
  436. custom.commandNumber = commandNumber++;
  437. custom.HorizontalMotorPulse = newValue;
  438. custom.CreateHorizontalMotorForwardCommand(newValue);
  439. custom.IsWaitOne = true;
  440. Enqueue(custom);
  441. taskAutoResetEvent.WaitOne();
  442. if (waitTime > 0)
  443. {
  444. Thread.Sleep(waitTime);
  445. }
  446. return custom.IsSuccess;
  447. }
  448. /// <summary>
  449. /// 水平电机反向运动(相对)
  450. /// 忠实搬运 operate 侧 ComBin.HorizontalMotorBackward(命令构造器 control 侧已存在)。
  451. /// </summary>
  452. public bool HorizontalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
  453. {
  454. custom.commandType = Enums.HorizontalMotorBackward;
  455. custom.logDateTime = DateTime.Now;
  456. custom.commandNumber = commandNumber++;
  457. custom.HorizontalMotorPulse = newValue;
  458. custom.CreateHorizontalMotorBackwardCommand(newValue);
  459. custom.IsWaitOne = true;
  460. Enqueue(custom);
  461. taskAutoResetEvent.WaitOne();
  462. if (waitTime > 0)
  463. {
  464. Thread.Sleep(waitTime);
  465. }
  466. return custom.IsSuccess;
  467. }
  468. /// <summary>
  469. /// 读水平电机位置
  470. /// </summary>
  471. public int ReadHorizontalMotorWait(CustomProtocol custom)
  472. {
  473. custom.commandType = Enums.ReadHorizontalMotor;
  474. custom.logDateTime = DateTime.Now;
  475. custom.commandNumber = commandNumber++;
  476. custom.CreateReadHorizontalMotorCommand();
  477. custom.IsWaitOne = true;
  478. Enqueue(custom);
  479. taskAutoResetEvent.WaitOne();
  480. if (!custom.IsSuccess) return -1;
  481. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  482. }
  483. /// <summary>
  484. /// 读垂直电机位置
  485. /// </summary>
  486. public int ReadVerticalMotorWait(CustomProtocol custom)
  487. {
  488. custom.commandType = Enums.ReadVerticalMotor;
  489. custom.logDateTime = DateTime.Now;
  490. custom.commandNumber = commandNumber++;
  491. custom.CreateReadVerticalMotorCommand();
  492. custom.IsWaitOne = true;
  493. Enqueue(custom);
  494. taskAutoResetEvent.WaitOne();
  495. if (!custom.IsSuccess) return -1;
  496. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  497. }
  498. /// <summary>
  499. /// 垂直电机复位
  500. /// </summary>
  501. public bool VerticalMotorResetWait(CustomProtocol custom, int waitTime)
  502. {
  503. custom.commandType = Enums.VerticalMotorReset;
  504. custom.logDateTime = DateTime.Now;
  505. custom.commandNumber = commandNumber++;
  506. custom.CreateVerticalMotorResetCommand();
  507. custom.IsWaitOne = true;
  508. Enqueue(custom);
  509. taskAutoResetEvent.WaitOne();
  510. if (waitTime > 0)
  511. {
  512. Thread.Sleep(waitTime);
  513. }
  514. return custom.IsSuccess;
  515. }
  516. /// <summary>
  517. /// 垂直电机绝对运动
  518. /// </summary>
  519. /// <param name="custom">指令对象</param>
  520. /// <param name="waitTime">电机运动等待时间</param>
  521. /// <param name="currentVer">垂直电机目标位置</param>
  522. /// <param name="currentHor">当前水平电机位置</param>
  523. /// <param name="pictureId">图片编号</param>
  524. /// <param name="well">当前well</param>
  525. /// <param name="focal">当前层数</param>
  526. public bool VerticalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int currentVer, int currentHor, int pictureId, int well, int focal)
  527. {
  528. custom.commandType = Enums.VerticalMotorAbsolute;
  529. custom.logDateTime = DateTime.Now;
  530. custom.commandNumber = commandNumber++;
  531. custom.VerticalMotorPulse = currentVer;
  532. custom.HorizontalMotorPulse = currentHor;
  533. custom.pictureId = pictureId;
  534. custom.well = well;
  535. custom.focal = focal;
  536. custom.CreateVerticalMotorAbsoluteMovementCommand(currentVer);
  537. custom.IsWaitOne = true;
  538. Enqueue(custom);
  539. taskAutoResetEvent.WaitOne();
  540. if (waitTime > 0)
  541. {
  542. Thread.Sleep(waitTime);
  543. }
  544. return custom.IsSuccess;
  545. }
  546. /// <summary>
  547. /// 垂直电机正向运动(相对)
  548. /// 忠实搬运 operate 侧 ComBin.VerticalMotorForwardWait(命令构造器 control 侧已存在)。
  549. /// </summary>
  550. public bool VerticalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
  551. {
  552. custom.commandType = Enums.VerticalMotorForward;
  553. custom.logDateTime = DateTime.Now;
  554. custom.commandNumber = commandNumber++;
  555. custom.CreateVerticalMotorForwardCommand(newValue);
  556. custom.IsWaitOne = true;
  557. Enqueue(custom);
  558. taskAutoResetEvent.WaitOne();
  559. if (waitTime > 0)
  560. {
  561. Thread.Sleep(waitTime);
  562. }
  563. return custom.IsSuccess;
  564. }
  565. /// <summary>
  566. /// 垂直电机反向运动(相对)
  567. /// 忠实搬运 operate 侧 ComBin.VerticalMotorBackwardWait(命令构造器 control 侧已存在)。
  568. /// </summary>
  569. public bool VerticalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
  570. {
  571. custom.commandType = Enums.VerticalMotorBackward;
  572. custom.logDateTime = DateTime.Now;
  573. custom.commandNumber = commandNumber++;
  574. custom.CreateVerticalMotorBackwardCommand(newValue);
  575. custom.IsWaitOne = true;
  576. Enqueue(custom);
  577. taskAutoResetEvent.WaitOne();
  578. if (waitTime > 0)
  579. {
  580. Thread.Sleep(waitTime);
  581. }
  582. return custom.IsSuccess;
  583. }
  584. #endregion
  585. #region 控制
  586. /// <summary>
  587. /// 自动换气开关
  588. /// 忠实搬运 operate 侧 ComBin.AutoWait(命令构造器/枚举 control 侧本任务已补)。
  589. /// </summary>
  590. public bool AutoWait(CustomProtocol custom, bool auto)
  591. {
  592. custom.logDateTime = DateTime.Now;
  593. custom.commandNumber = commandNumber++;
  594. custom.commandType = Enums.AutoAirSwap;
  595. custom.CreateAutoCommand(auto);
  596. custom.IsWaitOne = true;
  597. Enqueue(custom);
  598. taskAutoResetEvent.WaitOne();
  599. if (!custom.IsSuccess) return false;
  600. return custom.receivedBuffer[custom.lenght - 2] == 0;
  601. }
  602. /// <summary>
  603. /// 仓室补气
  604. /// </summary>
  605. /// <param name="custom"></param>
  606. public bool HouseAerationWait(CustomProtocol custom)
  607. {
  608. custom.commandType = Enums.HouseAeration;
  609. custom.logDateTime = DateTime.Now;
  610. custom.commandNumber = commandNumber++;
  611. custom.CreateHouseAerationCommand();
  612. custom.IsWaitOne = true;
  613. Enqueue(custom);
  614. taskAutoResetEvent.WaitOne();
  615. return custom.IsSuccess;
  616. }
  617. /// <summary>
  618. /// 仓室排气
  619. /// </summary>
  620. /// <param name="custom"></param>
  621. public bool HouseVentWait(CustomProtocol custom)
  622. {
  623. custom.commandType = Enums.HouseVent;
  624. custom.logDateTime = DateTime.Now;
  625. custom.commandNumber = commandNumber++;
  626. custom.CreateHouseVentCommand();
  627. custom.IsWaitOne = true;
  628. Enqueue(custom);
  629. taskAutoResetEvent.WaitOne();
  630. return custom.IsSuccess;
  631. }
  632. /// <summary>
  633. /// 缓冲瓶补气
  634. /// </summary>
  635. /// <param name="custom"></param>
  636. public bool BufferBottleAerationWait(CustomProtocol custom)
  637. {
  638. custom.commandType = Enums.BufferBottleAeration;
  639. custom.logDateTime = DateTime.Now;
  640. custom.commandNumber = commandNumber++;
  641. custom.CreateBufferBottleAerationCommand();
  642. custom.IsWaitOne = true;
  643. Enqueue(custom);
  644. taskAutoResetEvent.WaitOne();
  645. return custom.IsSuccess;
  646. }
  647. /// <summary>
  648. /// 关闭仓室进气阀
  649. /// </summary>
  650. /// <param name="custom"></param>
  651. /// <param name="waitTime"></param>
  652. public bool CloseIntakeValveWait(CustomProtocol custom, int waitTime)
  653. {
  654. custom.commandType = Enums.CloseIntakeValve;
  655. custom.logDateTime = DateTime.Now;
  656. custom.commandNumber = commandNumber++;
  657. custom.CreateCloseIntakeValveCommand();
  658. custom.IsWaitOne = true;
  659. Enqueue(custom);
  660. taskAutoResetEvent.WaitOne();
  661. if (waitTime > 0)
  662. {
  663. Thread.Sleep(waitTime);
  664. }
  665. return custom.IsSuccess;
  666. }
  667. /// <summary>
  668. /// 打开仓室排气阀
  669. /// </summary>
  670. /// <param name="custom"></param>
  671. public bool OpenExhaustValveWait(CustomProtocol custom, int waitTime)
  672. {
  673. custom.commandType = Enums.OpenExhaustValve;
  674. custom.logDateTime = DateTime.Now;
  675. custom.commandNumber = commandNumber++;
  676. custom.CreateOpenExhaustValveCommand();
  677. custom.IsWaitOne = true;
  678. Enqueue(custom);
  679. taskAutoResetEvent.WaitOne();
  680. if (waitTime > 0)
  681. {
  682. Thread.Sleep(waitTime);
  683. }
  684. return custom.IsSuccess;
  685. }
  686. /// <summary>
  687. /// 打开舱室进气阀
  688. /// </summary>
  689. /// <param name="custom"></param>
  690. /// <param name="waitTime"></param>
  691. /// <returns></returns>
  692. public bool OpenIntakeValveWait(CustomProtocol custom, int waitTime)
  693. {
  694. custom.commandType = Enums.OpenIntakeValve;
  695. custom.logDateTime = DateTime.Now;
  696. custom.commandNumber = commandNumber++;
  697. custom.CreateOpenIntakeValveCommand();
  698. custom.IsWaitOne = true;
  699. Enqueue(custom);
  700. taskAutoResetEvent.WaitOne();
  701. if (waitTime > 0)
  702. {
  703. Thread.Sleep(waitTime);
  704. }
  705. return custom.IsSuccess;
  706. }
  707. /// <summary>
  708. /// 关闭排气阀
  709. /// </summary>
  710. /// <param name="custom"></param>
  711. /// <param name="waitTime"></param>
  712. public void CloseExhaustValveWait(CustomProtocol custom, int waitTime)
  713. {
  714. custom.commandType = Enums.CloseExhaustValve;
  715. custom.logDateTime = DateTime.Now;
  716. custom.commandNumber = commandNumber++;
  717. custom.CreateCloseExhaustValveCommand();
  718. custom.IsWaitOne = true;
  719. Enqueue(custom);
  720. taskAutoResetEvent.WaitOne();
  721. if (waitTime > 0)
  722. {
  723. Thread.Sleep(waitTime);
  724. }
  725. }
  726. /// <summary>
  727. /// 打开LEd灯
  728. /// </summary>
  729. /// <param name="custom"></param>
  730. public void OpenLEDWait(CustomProtocol custom)
  731. {
  732. custom.commandType = Enums.OpenLED;
  733. custom.logDateTime = DateTime.Now;
  734. custom.commandNumber = commandNumber++;
  735. custom.CreateOpenLEDCommand();
  736. custom.IsWaitOne = true;
  737. Enqueue(custom);
  738. taskAutoResetEvent.WaitOne();
  739. }
  740. /// <summary>
  741. /// 关闭Led灯
  742. /// </summary>
  743. /// <param name="custom"></param>
  744. public void CloseLEDWait(CustomProtocol custom)
  745. {
  746. custom.commandType = Enums.CloseLED;
  747. custom.logDateTime = DateTime.Now;
  748. custom.commandNumber = commandNumber++;
  749. custom.CreateCloseLEDCommand();
  750. custom.IsWaitOne = true;
  751. Enqueue(custom);
  752. taskAutoResetEvent.WaitOne();
  753. }
  754. #endregion
  755. #region 参数
  756. /// <summary>
  757. /// 读取TL仪器编号
  758. /// </summary>
  759. public int ReadTLNumWait(CustomProtocol custom)
  760. {
  761. custom.commandType = Enums.ReadTLNum;
  762. custom.logDateTime = DateTime.Now;
  763. custom.commandNumber = commandNumber++;
  764. custom.CreateReadTLNumCommand();
  765. custom.IsWaitOne = true;
  766. Enqueue(custom);
  767. taskAutoResetEvent.WaitOne();
  768. if (!custom.IsSuccess) return -1;
  769. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  770. }
  771. /// <summary>
  772. /// 获取CCDSN
  773. /// </summary>
  774. public int GetCCDSNWait(CustomProtocol custom)
  775. {
  776. custom.commandType = Enums.GetCCDSN;
  777. custom.logDateTime = DateTime.Now;
  778. custom.commandNumber = commandNumber++;
  779. custom.CreateGetModuleCommand();
  780. custom.IsWaitOne = true;
  781. Enqueue(custom);
  782. taskAutoResetEvent.WaitOne();
  783. if (!custom.IsSuccess) return -1;
  784. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  785. }
  786. /// <summary>
  787. /// 读E方-->水平电机位置
  788. /// </summary>
  789. public int ReadEEPROMhoriMtWellHorHorWait(CustomProtocol custom,int horIndex)
  790. {
  791. custom.commandType = Enums.ReadEEPROMhoriMtWellHor;
  792. custom.logDateTime = DateTime.Now;
  793. custom.commandNumber = commandNumber++;
  794. custom.CreateReadEEPROMhoriMtWellHoriPos(horIndex);
  795. custom.IsWaitOne = true;
  796. Enqueue(custom);
  797. taskAutoResetEvent.WaitOne();
  798. if (!custom.IsSuccess) return -1;
  799. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  800. }
  801. /// <summary>
  802. /// 读E方-->垂直电机清晰位置
  803. /// </summary>
  804. /// <returns></returns>
  805. public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom)
  806. {
  807. custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
  808. custom.logDateTime = DateTime.Now;
  809. custom.commandNumber = commandNumber++;
  810. custom.CreateReadEEPROMvertMtStartPulse(1);
  811. custom.IsWaitOne = true;
  812. Enqueue(custom);
  813. taskAutoResetEvent.WaitOne();
  814. if (!custom.IsSuccess) return -1;
  815. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  816. }
  817. /// <summary>
  818. /// 读E方-->垂直电机间隔脉冲
  819. /// </summary>
  820. /// <returns></returns>
  821. public int ReadEEPROMvertMtScanPluseWait(CustomProtocol custom)
  822. {
  823. custom.commandType = Enums.ReadEEPROMvertMtScanPluse;
  824. custom.logDateTime = DateTime.Now;
  825. custom.commandNumber = commandNumber++;
  826. custom.IsWaitOne = true;
  827. custom.CreateReadEEPROMvertMtScanPluse();
  828. Enqueue( custom);//断层扫描间隔数
  829. taskAutoResetEvent.WaitOne();
  830. if (!custom.IsSuccess) return -1;
  831. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  832. }
  833. /// <summary>
  834. /// 读E方-->仓室进气阀打开时间
  835. /// </summary>
  836. /// <returns></returns>
  837. public int ReadEEPROOpenIntakeTimeWait(CustomProtocol custom)
  838. {
  839. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  840. custom.logDateTime = DateTime.Now;
  841. custom.commandNumber = commandNumber++;
  842. custom.IsWaitOne = true;
  843. custom.CreateReadEEPROOpenIntakeTimeCommand();
  844. Enqueue( custom);
  845. taskAutoResetEvent.WaitOne();
  846. if (!custom.IsSuccess) return -1;
  847. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  848. }
  849. /// <summary>
  850. /// 读E方-->缓冲瓶进气阀打开时间
  851. /// </summary>
  852. /// <returns></returns>
  853. public int ReadEEPROOpenIntakeTimeBufferWait(CustomProtocol custom)
  854. {
  855. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  856. custom.logDateTime = DateTime.Now;
  857. custom.commandNumber = commandNumber++;
  858. custom.IsWaitOne = true;
  859. custom.CreateReadEEPROOpenIntakeTimeBufferCommand();
  860. Enqueue(custom);
  861. taskAutoResetEvent.WaitOne();
  862. if (!custom.IsSuccess) return -1;
  863. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  864. }
  865. /// <summary>
  866. /// 读E方-->下加热板目标温度
  867. /// </summary>
  868. /// <returns></returns>
  869. public decimal ReadTargetTempWait(CustomProtocol custom)
  870. {
  871. custom.commandType = Enums.ReadTargetTemp;
  872. custom.logDateTime = DateTime.Now;
  873. custom.commandNumber = commandNumber++;
  874. custom.CreateReadTargetTemp();
  875. custom.IsWaitOne = true;
  876. Enqueue( custom);
  877. taskAutoResetEvent.WaitOne();
  878. if (!custom.IsSuccess) return -1m;
  879. var num = Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  880. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  881. }
  882. /// <summary>
  883. /// 读E方-->灯光亮度
  884. /// </summary>
  885. /// <returns></returns>
  886. public int ReadEEPROMLightNumWait(CustomProtocol custom)
  887. {
  888. custom.commandType = Enums.ReadLightNum;
  889. custom.logDateTime = DateTime.Now;
  890. custom.commandNumber = commandNumber++;
  891. custom.IsWaitOne = true;
  892. custom.CreateReadEEPROMLightNum();
  893. Enqueue(custom);
  894. taskAutoResetEvent.WaitOne();
  895. if (!custom.IsSuccess) return -1;
  896. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  897. }
  898. // ── M1-B2 写E方调试动作包装(operate 调试页接入 lease.Serial 走本类,杜绝调试页 new ComBin/开第二个物理口)──
  899. // 字节级一致性:T1.1 子代理已逐字节核实下列 4 个 Create* builder 与 operate 端完全相同
  900. // (命令码 0x12 + 相同 well 地址表/固定地址 + 小端 pulse + 同 CreateORC 校验),故发送字节安全。
  901. // ⚠ 待真机(V-010):写E方为破坏性操作;control 端 CustomProtocolLength 对 0x12 期望回包长度与 operate 不同,
  902. // "写成功判定/回包等待"语义需真机核对(不影响发出的命令字节)。WriteEEPROOpenVentTime/WriteLightNum
  903. // 因 control Commander 缺对应 builder,未在此补——调试页对应动作保留旧路径并标注待真机,不臆造字节。
  904. /// <summary>写E方→第 wellSn(1-16) 水平电机位置脉冲(命令 0x12,builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
  905. public void WriteEEPROMhoriMtWellHorHorWait(CustomProtocol custom, int wellSn, int newValue)
  906. {
  907. custom.commandType = Enums.WriteEEPROMhoriMtWell1HoriPos;
  908. custom.logDateTime = DateTime.Now;
  909. custom.commandNumber = commandNumber++;
  910. custom.IsWaitOne = true;
  911. custom.CreateWriteEEPROMhoriMtWellHoriPos(wellSn, newValue);
  912. Enqueue(custom);
  913. taskAutoResetEvent.WaitOne();
  914. return;
  915. }
  916. /// <summary>写E方→垂直电机扫描间隔脉冲(builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
  917. public void WriteEEPROMvertMtScanPluseWait(CustomProtocol custom, int newValue)
  918. {
  919. custom.commandType = Enums.WriteEEPROMvertMtScanPluse;
  920. custom.logDateTime = DateTime.Now;
  921. custom.commandNumber = commandNumber++;
  922. custom.IsWaitOne = true;
  923. custom.CreateWriteEEPROMvertMtScanPluse(newValue);
  924. Enqueue(custom);
  925. taskAutoResetEvent.WaitOne();
  926. return;
  927. }
  928. /// <summary>写E方→舱室进气阀打开时间(builder 与 operate 逐字节一致,地址 00 03 0c)。⚠ 待真机 V-010。</summary>
  929. public void WriteEEPROOpenIntakeTimeWait(CustomProtocol custom, int newValue)
  930. {
  931. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  932. custom.logDateTime = DateTime.Now;
  933. custom.commandNumber = commandNumber++;
  934. custom.IsWaitOne = true;
  935. custom.CreateWriteEEPROOpenIntakeTimeCommand(newValue);
  936. Enqueue(custom);
  937. taskAutoResetEvent.WaitOne();
  938. return;
  939. }
  940. /// <summary>写E方→缓冲瓶进气阀打开时间(builder 与 operate 逐字节一致,地址 00 05 0c)。⚠ 待真机 V-010。</summary>
  941. public void WriteEEPROOpenIntakeTimeBufferWait(CustomProtocol custom, int newValue)
  942. {
  943. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  944. custom.logDateTime = DateTime.Now;
  945. custom.commandNumber = commandNumber++;
  946. custom.IsWaitOne = true;
  947. custom.CreateWriteEEPROOpenIntakeTimeBufferCommand(newValue);
  948. Enqueue(custom);
  949. taskAutoResetEvent.WaitOne();
  950. return;
  951. }
  952. #endregion
  953. #region 调试模式
  954. #endregion
  955. }
  956. }