ComBin.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. bool ok = _channel.OpenPort();
  241. // 借用复用死锁修复(D1-08):HAL.ScanDevices 扫描每口后经 SerialChannelImpl.Close() 置 IsStop=true,
  242. // 令发送线程 while(IsStop)return 永久退出;采集端 serialBin.Start() 随后借用同一缓存 ComBin、
  243. // 直接调本方法重开端口(返回 True)再 ShakeHandsWait 入队握手指令,但发送线程已死、无人出队处理,
  244. // ShakeHandsWait 的 taskAutoResetEvent.WaitOne()(无超时)→ 永久死锁(旧合并 operate 僵尸即卡此)。
  245. // 故:重开端口成功且发送线程处于停止态时,复位 IsStop 并重启发送线程(SendCommandLock 防并发双起)。
  246. if (ok)
  247. {
  248. lock (SendCommandLock)
  249. {
  250. if (IsStop)
  251. {
  252. IsStop = false;
  253. StartSendCommandThread();
  254. }
  255. }
  256. }
  257. return ok;
  258. }
  259. public bool ClosePort()
  260. {
  261. return _channel.ClosePort();
  262. }
  263. /// <summary>
  264. /// 通讯报警
  265. /// </summary>
  266. /// <param name="result"></param>
  267. public void ComlossAlarm(bool result)
  268. {
  269. if(!result)
  270. {
  271. if(ComState != 1)
  272. {
  273. ComState = 1;
  274. ComStateEvent?.Invoke(1);
  275. }
  276. return;
  277. }
  278. if (ComState == 0) return;
  279. ComState = 0;
  280. ComStateEvent?.Invoke(0);
  281. }
  282. #region 仓室状态
  283. /// <summary>
  284. /// 握手指令
  285. /// </summary>
  286. public int ShakeHandsWait(CustomProtocol custom)
  287. {
  288. //握手
  289. custom.logDateTime = DateTime.Now;
  290. custom.commandNumber = commandNumber++;
  291. custom.commandType = Enums.ShakeHands;
  292. custom.CreateHandCommand();
  293. custom.IsWaitOne = true;
  294. Enqueue(custom);
  295. taskAutoResetEvent.WaitOne();
  296. if (!custom.IsSuccess) return -1;
  297. return Analysiser.ParseShakeHandsCommand(custom.receivedBuffer);
  298. }
  299. /// <summary>
  300. /// 读仓门状态
  301. /// </summary>
  302. /// <param name="custom"></param>
  303. /// <returns></returns>
  304. public State DoorStatusWait(CustomProtocol custom)
  305. {
  306. custom.commandType = Enums.DoorStatus;
  307. custom.logDateTime = DateTime.Now;
  308. custom.commandNumber = commandNumber++;
  309. custom.CreateReadDoorCommand();
  310. custom.IsWaitOne = true;
  311. Enqueue(custom);
  312. taskAutoResetEvent.WaitOne();
  313. if (!custom.IsSuccess) return State.未知;
  314. return Analysiser.ParseDoorStatus(custom.receivedBuffer);
  315. }
  316. /// <summary>
  317. /// 仓室温度
  318. /// </summary>
  319. /// <param name="custom"></param>
  320. /// <returns></returns>
  321. public decimal TemperatureWait(CustomProtocol custom)
  322. {
  323. custom.commandType = Enums.Temperature;
  324. custom.logDateTime = DateTime.Now;
  325. custom.commandNumber = commandNumber++;
  326. custom.CreateReadTemperatureCommand();
  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 ShangTemperatureWait(CustomProtocol custom)
  339. {
  340. custom.commandType = Enums.Temperature;
  341. custom.logDateTime = DateTime.Now;
  342. custom.commandNumber = commandNumber++;
  343. custom.CreateReadShangTemperatureCommand();
  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 BoLiTemperatureWait(CustomProtocol custom)
  356. {
  357. custom.commandType = Enums.BoLiTemperature;
  358. custom.logDateTime = DateTime.Now;
  359. custom.commandNumber = commandNumber++;
  360. custom.CreateReadBoLiTemperatureCommand();
  361. custom.IsWaitOne = true;
  362. Enqueue(custom);
  363. taskAutoResetEvent.WaitOne();
  364. if (!custom.IsSuccess) return -1m;
  365. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  366. }
  367. /// <summary>
  368. /// 仓室压力
  369. /// </summary>
  370. /// <param name="custom"></param>
  371. /// <returns></returns>
  372. public decimal PressureWait(CustomProtocol custom)
  373. {
  374. custom.commandType = Enums.Pressure;
  375. custom.logDateTime = DateTime.Now;
  376. custom.commandNumber = commandNumber++;
  377. custom.CreateReadPressureCommand();
  378. custom.IsWaitOne = true;
  379. Enqueue(custom);
  380. taskAutoResetEvent.WaitOne();
  381. if (!custom.IsSuccess) return -1m;
  382. return Analysiser.ParsePressureCommand(custom.receivedBuffer);
  383. }
  384. /// <summary>
  385. /// 获取缓冲瓶状态
  386. /// </summary>
  387. /// <returns></returns>
  388. public (decimal, decimal, decimal) BufferBottleState(CustomProtocol custom)
  389. {
  390. custom.commandType = Enums.BufferBottlePressure;
  391. custom.logDateTime = DateTime.Now;
  392. custom.commandNumber = commandNumber++;
  393. custom.CreateBufferBottlePressureCommand();
  394. custom.IsWaitOne = true;
  395. Enqueue(custom);
  396. taskAutoResetEvent.WaitOne();
  397. if (!custom.IsSuccess) return (-1m,-1m,-1m);
  398. return (Analysiser.ParsePressureCommand(custom.receivedBuffer), Analysiser.ParseTLTemperature1Command(custom.receivedBuffer), Analysiser.ParseTLTemperature2Command(custom.receivedBuffer));
  399. }
  400. #endregion
  401. #region 电机
  402. /// <summary>
  403. /// 水平电机复位
  404. /// </summary>
  405. /// <param name="custom"></param>
  406. /// <param name="waitTime"></param>
  407. public bool HorizontalMotorResetWait(CustomProtocol custom, int waitTime)
  408. {
  409. custom.commandType = Enums.HorizontalMotorReset;
  410. custom.logDateTime = DateTime.Now;
  411. custom.commandNumber = commandNumber++;
  412. custom.CreateHorizontalMotorResetCommand();
  413. custom.IsWaitOne = true;
  414. Enqueue(custom);
  415. taskAutoResetEvent.WaitOne();
  416. if (waitTime > 0)
  417. {
  418. Thread.Sleep(waitTime);
  419. }
  420. return custom.IsSuccess;
  421. }
  422. /// <summary>
  423. /// 水平电机绝对运动
  424. /// </summary>
  425. /// <param name="custom"></param>
  426. /// <param name="waitTime"></param>
  427. /// <param name="zero"></param>
  428. /// <param name="newValue"></param>
  429. public bool HorizontalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int newValue)
  430. {
  431. custom.commandType = Enums.HorizontalMotorAbsolute;
  432. custom.logDateTime = DateTime.Now;
  433. custom.commandNumber = commandNumber++;
  434. custom.HorizontalMotorPulse = newValue;
  435. custom.CreateHorizontalMotorMoveToCommand(newValue);
  436. custom.IsWaitOne = true;
  437. Enqueue(custom);
  438. taskAutoResetEvent.WaitOne();
  439. if (waitTime > 0)
  440. {
  441. Thread.Sleep(waitTime);
  442. }
  443. return custom.IsSuccess;
  444. }
  445. /// <summary>
  446. /// 水平电机正向运动(相对)
  447. /// 忠实搬运 operate 侧 ComBin.HorizontalMotorForwardWait(命令构造器 control 侧已存在)。
  448. /// </summary>
  449. public bool HorizontalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
  450. {
  451. custom.commandType = Enums.HorizontalMotorForward;
  452. custom.logDateTime = DateTime.Now;
  453. custom.commandNumber = commandNumber++;
  454. custom.HorizontalMotorPulse = newValue;
  455. custom.CreateHorizontalMotorForwardCommand(newValue);
  456. custom.IsWaitOne = true;
  457. Enqueue(custom);
  458. taskAutoResetEvent.WaitOne();
  459. if (waitTime > 0)
  460. {
  461. Thread.Sleep(waitTime);
  462. }
  463. return custom.IsSuccess;
  464. }
  465. /// <summary>
  466. /// 水平电机反向运动(相对)
  467. /// 忠实搬运 operate 侧 ComBin.HorizontalMotorBackward(命令构造器 control 侧已存在)。
  468. /// </summary>
  469. public bool HorizontalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
  470. {
  471. custom.commandType = Enums.HorizontalMotorBackward;
  472. custom.logDateTime = DateTime.Now;
  473. custom.commandNumber = commandNumber++;
  474. custom.HorizontalMotorPulse = newValue;
  475. custom.CreateHorizontalMotorBackwardCommand(newValue);
  476. custom.IsWaitOne = true;
  477. Enqueue(custom);
  478. taskAutoResetEvent.WaitOne();
  479. if (waitTime > 0)
  480. {
  481. Thread.Sleep(waitTime);
  482. }
  483. return custom.IsSuccess;
  484. }
  485. /// <summary>
  486. /// 读水平电机位置
  487. /// </summary>
  488. public int ReadHorizontalMotorWait(CustomProtocol custom)
  489. {
  490. custom.commandType = Enums.ReadHorizontalMotor;
  491. custom.logDateTime = DateTime.Now;
  492. custom.commandNumber = commandNumber++;
  493. custom.CreateReadHorizontalMotorCommand();
  494. custom.IsWaitOne = true;
  495. Enqueue(custom);
  496. taskAutoResetEvent.WaitOne();
  497. if (!custom.IsSuccess) return -1;
  498. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  499. }
  500. /// <summary>
  501. /// 读垂直电机位置
  502. /// </summary>
  503. public int ReadVerticalMotorWait(CustomProtocol custom)
  504. {
  505. custom.commandType = Enums.ReadVerticalMotor;
  506. custom.logDateTime = DateTime.Now;
  507. custom.commandNumber = commandNumber++;
  508. custom.CreateReadVerticalMotorCommand();
  509. custom.IsWaitOne = true;
  510. Enqueue(custom);
  511. taskAutoResetEvent.WaitOne();
  512. if (!custom.IsSuccess) return -1;
  513. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  514. }
  515. /// <summary>
  516. /// 垂直电机复位
  517. /// </summary>
  518. public bool VerticalMotorResetWait(CustomProtocol custom, int waitTime)
  519. {
  520. custom.commandType = Enums.VerticalMotorReset;
  521. custom.logDateTime = DateTime.Now;
  522. custom.commandNumber = commandNumber++;
  523. custom.CreateVerticalMotorResetCommand();
  524. custom.IsWaitOne = true;
  525. Enqueue(custom);
  526. taskAutoResetEvent.WaitOne();
  527. if (waitTime > 0)
  528. {
  529. Thread.Sleep(waitTime);
  530. }
  531. return custom.IsSuccess;
  532. }
  533. /// <summary>
  534. /// 垂直电机绝对运动
  535. /// </summary>
  536. /// <param name="custom">指令对象</param>
  537. /// <param name="waitTime">电机运动等待时间</param>
  538. /// <param name="currentVer">垂直电机目标位置</param>
  539. /// <param name="currentHor">当前水平电机位置</param>
  540. /// <param name="pictureId">图片编号</param>
  541. /// <param name="well">当前well</param>
  542. /// <param name="focal">当前层数</param>
  543. public bool VerticalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int currentVer, int currentHor, int pictureId, int well, int focal)
  544. {
  545. custom.commandType = Enums.VerticalMotorAbsolute;
  546. custom.logDateTime = DateTime.Now;
  547. custom.commandNumber = commandNumber++;
  548. custom.VerticalMotorPulse = currentVer;
  549. custom.HorizontalMotorPulse = currentHor;
  550. custom.pictureId = pictureId;
  551. custom.well = well;
  552. custom.focal = focal;
  553. custom.CreateVerticalMotorAbsoluteMovementCommand(currentVer);
  554. custom.IsWaitOne = true;
  555. Enqueue(custom);
  556. taskAutoResetEvent.WaitOne();
  557. if (waitTime > 0)
  558. {
  559. Thread.Sleep(waitTime);
  560. }
  561. return custom.IsSuccess;
  562. }
  563. /// <summary>
  564. /// 垂直电机正向运动(相对)
  565. /// 忠实搬运 operate 侧 ComBin.VerticalMotorForwardWait(命令构造器 control 侧已存在)。
  566. /// </summary>
  567. public bool VerticalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
  568. {
  569. custom.commandType = Enums.VerticalMotorForward;
  570. custom.logDateTime = DateTime.Now;
  571. custom.commandNumber = commandNumber++;
  572. custom.CreateVerticalMotorForwardCommand(newValue);
  573. custom.IsWaitOne = true;
  574. Enqueue(custom);
  575. taskAutoResetEvent.WaitOne();
  576. if (waitTime > 0)
  577. {
  578. Thread.Sleep(waitTime);
  579. }
  580. return custom.IsSuccess;
  581. }
  582. /// <summary>
  583. /// 垂直电机反向运动(相对)
  584. /// 忠实搬运 operate 侧 ComBin.VerticalMotorBackwardWait(命令构造器 control 侧已存在)。
  585. /// </summary>
  586. public bool VerticalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
  587. {
  588. custom.commandType = Enums.VerticalMotorBackward;
  589. custom.logDateTime = DateTime.Now;
  590. custom.commandNumber = commandNumber++;
  591. custom.CreateVerticalMotorBackwardCommand(newValue);
  592. custom.IsWaitOne = true;
  593. Enqueue(custom);
  594. taskAutoResetEvent.WaitOne();
  595. if (waitTime > 0)
  596. {
  597. Thread.Sleep(waitTime);
  598. }
  599. return custom.IsSuccess;
  600. }
  601. #endregion
  602. #region 控制
  603. /// <summary>
  604. /// 自动换气开关
  605. /// 忠实搬运 operate 侧 ComBin.AutoWait(命令构造器/枚举 control 侧本任务已补)。
  606. /// </summary>
  607. public bool AutoWait(CustomProtocol custom, bool auto)
  608. {
  609. custom.logDateTime = DateTime.Now;
  610. custom.commandNumber = commandNumber++;
  611. custom.commandType = Enums.AutoAirSwap;
  612. custom.CreateAutoCommand(auto);
  613. custom.IsWaitOne = true;
  614. Enqueue(custom);
  615. taskAutoResetEvent.WaitOne();
  616. if (!custom.IsSuccess) return false;
  617. return custom.receivedBuffer[custom.lenght - 2] == 0;
  618. }
  619. /// <summary>
  620. /// 仓室补气
  621. /// </summary>
  622. /// <param name="custom"></param>
  623. public bool HouseAerationWait(CustomProtocol custom)
  624. {
  625. custom.commandType = Enums.HouseAeration;
  626. custom.logDateTime = DateTime.Now;
  627. custom.commandNumber = commandNumber++;
  628. custom.CreateHouseAerationCommand();
  629. custom.IsWaitOne = true;
  630. Enqueue(custom);
  631. taskAutoResetEvent.WaitOne();
  632. return custom.IsSuccess;
  633. }
  634. /// <summary>
  635. /// 仓室排气
  636. /// </summary>
  637. /// <param name="custom"></param>
  638. public bool HouseVentWait(CustomProtocol custom)
  639. {
  640. custom.commandType = Enums.HouseVent;
  641. custom.logDateTime = DateTime.Now;
  642. custom.commandNumber = commandNumber++;
  643. custom.CreateHouseVentCommand();
  644. custom.IsWaitOne = true;
  645. Enqueue(custom);
  646. taskAutoResetEvent.WaitOne();
  647. return custom.IsSuccess;
  648. }
  649. /// <summary>
  650. /// 缓冲瓶补气
  651. /// </summary>
  652. /// <param name="custom"></param>
  653. public bool BufferBottleAerationWait(CustomProtocol custom)
  654. {
  655. custom.commandType = Enums.BufferBottleAeration;
  656. custom.logDateTime = DateTime.Now;
  657. custom.commandNumber = commandNumber++;
  658. custom.CreateBufferBottleAerationCommand();
  659. custom.IsWaitOne = true;
  660. Enqueue(custom);
  661. taskAutoResetEvent.WaitOne();
  662. return custom.IsSuccess;
  663. }
  664. /// <summary>
  665. /// 关闭仓室进气阀
  666. /// </summary>
  667. /// <param name="custom"></param>
  668. /// <param name="waitTime"></param>
  669. public bool CloseIntakeValveWait(CustomProtocol custom, int waitTime)
  670. {
  671. custom.commandType = Enums.CloseIntakeValve;
  672. custom.logDateTime = DateTime.Now;
  673. custom.commandNumber = commandNumber++;
  674. custom.CreateCloseIntakeValveCommand();
  675. custom.IsWaitOne = true;
  676. Enqueue(custom);
  677. taskAutoResetEvent.WaitOne();
  678. if (waitTime > 0)
  679. {
  680. Thread.Sleep(waitTime);
  681. }
  682. return custom.IsSuccess;
  683. }
  684. /// <summary>
  685. /// 打开仓室排气阀
  686. /// </summary>
  687. /// <param name="custom"></param>
  688. public bool OpenExhaustValveWait(CustomProtocol custom, int waitTime)
  689. {
  690. custom.commandType = Enums.OpenExhaustValve;
  691. custom.logDateTime = DateTime.Now;
  692. custom.commandNumber = commandNumber++;
  693. custom.CreateOpenExhaustValveCommand();
  694. custom.IsWaitOne = true;
  695. Enqueue(custom);
  696. taskAutoResetEvent.WaitOne();
  697. if (waitTime > 0)
  698. {
  699. Thread.Sleep(waitTime);
  700. }
  701. return custom.IsSuccess;
  702. }
  703. /// <summary>
  704. /// 打开舱室进气阀
  705. /// </summary>
  706. /// <param name="custom"></param>
  707. /// <param name="waitTime"></param>
  708. /// <returns></returns>
  709. public bool OpenIntakeValveWait(CustomProtocol custom, int waitTime)
  710. {
  711. custom.commandType = Enums.OpenIntakeValve;
  712. custom.logDateTime = DateTime.Now;
  713. custom.commandNumber = commandNumber++;
  714. custom.CreateOpenIntakeValveCommand();
  715. custom.IsWaitOne = true;
  716. Enqueue(custom);
  717. taskAutoResetEvent.WaitOne();
  718. if (waitTime > 0)
  719. {
  720. Thread.Sleep(waitTime);
  721. }
  722. return custom.IsSuccess;
  723. }
  724. /// <summary>
  725. /// 关闭排气阀
  726. /// </summary>
  727. /// <param name="custom"></param>
  728. /// <param name="waitTime"></param>
  729. public void CloseExhaustValveWait(CustomProtocol custom, int waitTime)
  730. {
  731. custom.commandType = Enums.CloseExhaustValve;
  732. custom.logDateTime = DateTime.Now;
  733. custom.commandNumber = commandNumber++;
  734. custom.CreateCloseExhaustValveCommand();
  735. custom.IsWaitOne = true;
  736. Enqueue(custom);
  737. taskAutoResetEvent.WaitOne();
  738. if (waitTime > 0)
  739. {
  740. Thread.Sleep(waitTime);
  741. }
  742. }
  743. /// <summary>
  744. /// 打开LEd灯
  745. /// </summary>
  746. /// <param name="custom"></param>
  747. public void OpenLEDWait(CustomProtocol custom)
  748. {
  749. custom.commandType = Enums.OpenLED;
  750. custom.logDateTime = DateTime.Now;
  751. custom.commandNumber = commandNumber++;
  752. custom.CreateOpenLEDCommand();
  753. custom.IsWaitOne = true;
  754. Enqueue(custom);
  755. taskAutoResetEvent.WaitOne();
  756. }
  757. /// <summary>
  758. /// 关闭Led灯
  759. /// </summary>
  760. /// <param name="custom"></param>
  761. public void CloseLEDWait(CustomProtocol custom)
  762. {
  763. custom.commandType = Enums.CloseLED;
  764. custom.logDateTime = DateTime.Now;
  765. custom.commandNumber = commandNumber++;
  766. custom.CreateCloseLEDCommand();
  767. custom.IsWaitOne = true;
  768. Enqueue(custom);
  769. taskAutoResetEvent.WaitOne();
  770. }
  771. #endregion
  772. #region 参数
  773. /// <summary>
  774. /// 读取TL仪器编号
  775. /// </summary>
  776. public int ReadTLNumWait(CustomProtocol custom)
  777. {
  778. custom.commandType = Enums.ReadTLNum;
  779. custom.logDateTime = DateTime.Now;
  780. custom.commandNumber = commandNumber++;
  781. custom.CreateReadTLNumCommand();
  782. custom.IsWaitOne = true;
  783. Enqueue(custom);
  784. taskAutoResetEvent.WaitOne();
  785. if (!custom.IsSuccess) return -1;
  786. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  787. }
  788. /// <summary>
  789. /// 获取CCDSN
  790. /// </summary>
  791. public int GetCCDSNWait(CustomProtocol custom)
  792. {
  793. custom.commandType = Enums.GetCCDSN;
  794. custom.logDateTime = DateTime.Now;
  795. custom.commandNumber = commandNumber++;
  796. custom.CreateGetModuleCommand();
  797. custom.IsWaitOne = true;
  798. Enqueue(custom);
  799. taskAutoResetEvent.WaitOne();
  800. if (!custom.IsSuccess) return -1;
  801. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  802. }
  803. /// <summary>
  804. /// 读E方-->水平电机位置
  805. /// </summary>
  806. public int ReadEEPROMhoriMtWellHorHorWait(CustomProtocol custom,int horIndex)
  807. {
  808. custom.commandType = Enums.ReadEEPROMhoriMtWellHor;
  809. custom.logDateTime = DateTime.Now;
  810. custom.commandNumber = commandNumber++;
  811. custom.CreateReadEEPROMhoriMtWellHoriPos(horIndex);
  812. custom.IsWaitOne = true;
  813. Enqueue(custom);
  814. taskAutoResetEvent.WaitOne();
  815. if (!custom.IsSuccess) return -1;
  816. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  817. }
  818. /// <summary>
  819. /// 读E方-->垂直电机清晰位置
  820. /// </summary>
  821. /// <returns></returns>
  822. public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom)
  823. {
  824. custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
  825. custom.logDateTime = DateTime.Now;
  826. custom.commandNumber = commandNumber++;
  827. custom.CreateReadEEPROMvertMtStartPulse(1);
  828. custom.IsWaitOne = true;
  829. Enqueue(custom);
  830. taskAutoResetEvent.WaitOne();
  831. if (!custom.IsSuccess) return -1;
  832. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  833. }
  834. /// <summary>
  835. /// 读E方-->垂直电机间隔脉冲
  836. /// </summary>
  837. /// <returns></returns>
  838. public int ReadEEPROMvertMtScanPluseWait(CustomProtocol custom)
  839. {
  840. custom.commandType = Enums.ReadEEPROMvertMtScanPluse;
  841. custom.logDateTime = DateTime.Now;
  842. custom.commandNumber = commandNumber++;
  843. custom.IsWaitOne = true;
  844. custom.CreateReadEEPROMvertMtScanPluse();
  845. Enqueue( custom);//断层扫描间隔数
  846. taskAutoResetEvent.WaitOne();
  847. if (!custom.IsSuccess) return -1;
  848. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  849. }
  850. /// <summary>
  851. /// 读E方-->仓室进气阀打开时间
  852. /// </summary>
  853. /// <returns></returns>
  854. public int ReadEEPROOpenIntakeTimeWait(CustomProtocol custom)
  855. {
  856. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  857. custom.logDateTime = DateTime.Now;
  858. custom.commandNumber = commandNumber++;
  859. custom.IsWaitOne = true;
  860. custom.CreateReadEEPROOpenIntakeTimeCommand();
  861. Enqueue( custom);
  862. taskAutoResetEvent.WaitOne();
  863. if (!custom.IsSuccess) return -1;
  864. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  865. }
  866. /// <summary>
  867. /// 读E方-->缓冲瓶进气阀打开时间
  868. /// </summary>
  869. /// <returns></returns>
  870. public int ReadEEPROOpenIntakeTimeBufferWait(CustomProtocol custom)
  871. {
  872. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  873. custom.logDateTime = DateTime.Now;
  874. custom.commandNumber = commandNumber++;
  875. custom.IsWaitOne = true;
  876. custom.CreateReadEEPROOpenIntakeTimeBufferCommand();
  877. Enqueue(custom);
  878. taskAutoResetEvent.WaitOne();
  879. if (!custom.IsSuccess) return -1;
  880. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  881. }
  882. /// <summary>
  883. /// 读E方-->下加热板目标温度
  884. /// </summary>
  885. /// <returns></returns>
  886. public decimal ReadTargetTempWait(CustomProtocol custom)
  887. {
  888. custom.commandType = Enums.ReadTargetTemp;
  889. custom.logDateTime = DateTime.Now;
  890. custom.commandNumber = commandNumber++;
  891. custom.CreateReadTargetTemp();
  892. custom.IsWaitOne = true;
  893. Enqueue( custom);
  894. taskAutoResetEvent.WaitOne();
  895. if (!custom.IsSuccess) return -1m;
  896. var num = Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  897. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  898. }
  899. /// <summary>
  900. /// 读E方-->灯光亮度
  901. /// </summary>
  902. /// <returns></returns>
  903. public int ReadEEPROMLightNumWait(CustomProtocol custom)
  904. {
  905. custom.commandType = Enums.ReadLightNum;
  906. custom.logDateTime = DateTime.Now;
  907. custom.commandNumber = commandNumber++;
  908. custom.IsWaitOne = true;
  909. custom.CreateReadEEPROMLightNum();
  910. Enqueue(custom);
  911. taskAutoResetEvent.WaitOne();
  912. if (!custom.IsSuccess) return -1;
  913. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  914. }
  915. // ── M1-B2 写E方调试动作包装(operate 调试页接入 lease.Serial 走本类,杜绝调试页 new ComBin/开第二个物理口)──
  916. // 字节级一致性:T1.1 子代理已逐字节核实下列 4 个 Create* builder 与 operate 端完全相同
  917. // (命令码 0x12 + 相同 well 地址表/固定地址 + 小端 pulse + 同 CreateORC 校验),故发送字节安全。
  918. // ⚠ 待真机(V-010):写E方为破坏性操作;control 端 CustomProtocolLength 对 0x12 期望回包长度与 operate 不同,
  919. // "写成功判定/回包等待"语义需真机核对(不影响发出的命令字节)。WriteEEPROOpenVentTime/WriteLightNum
  920. // 因 control Commander 缺对应 builder,未在此补——调试页对应动作保留旧路径并标注待真机,不臆造字节。
  921. /// <summary>写E方→第 wellSn(1-16) 水平电机位置脉冲(命令 0x12,builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
  922. public void WriteEEPROMhoriMtWellHorHorWait(CustomProtocol custom, int wellSn, int newValue)
  923. {
  924. custom.commandType = Enums.WriteEEPROMhoriMtWell1HoriPos;
  925. custom.logDateTime = DateTime.Now;
  926. custom.commandNumber = commandNumber++;
  927. custom.IsWaitOne = true;
  928. custom.CreateWriteEEPROMhoriMtWellHoriPos(wellSn, newValue);
  929. Enqueue(custom);
  930. taskAutoResetEvent.WaitOne();
  931. return;
  932. }
  933. /// <summary>写E方→垂直电机扫描间隔脉冲(builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
  934. public void WriteEEPROMvertMtScanPluseWait(CustomProtocol custom, int newValue)
  935. {
  936. custom.commandType = Enums.WriteEEPROMvertMtScanPluse;
  937. custom.logDateTime = DateTime.Now;
  938. custom.commandNumber = commandNumber++;
  939. custom.IsWaitOne = true;
  940. custom.CreateWriteEEPROMvertMtScanPluse(newValue);
  941. Enqueue(custom);
  942. taskAutoResetEvent.WaitOne();
  943. return;
  944. }
  945. /// <summary>写E方→舱室进气阀打开时间(builder 与 operate 逐字节一致,地址 00 03 0c)。⚠ 待真机 V-010。</summary>
  946. public void WriteEEPROOpenIntakeTimeWait(CustomProtocol custom, int newValue)
  947. {
  948. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  949. custom.logDateTime = DateTime.Now;
  950. custom.commandNumber = commandNumber++;
  951. custom.IsWaitOne = true;
  952. custom.CreateWriteEEPROOpenIntakeTimeCommand(newValue);
  953. Enqueue(custom);
  954. taskAutoResetEvent.WaitOne();
  955. return;
  956. }
  957. /// <summary>写E方→缓冲瓶进气阀打开时间(builder 与 operate 逐字节一致,地址 00 05 0c)。⚠ 待真机 V-010。</summary>
  958. public void WriteEEPROOpenIntakeTimeBufferWait(CustomProtocol custom, int newValue)
  959. {
  960. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  961. custom.logDateTime = DateTime.Now;
  962. custom.commandNumber = commandNumber++;
  963. custom.IsWaitOne = true;
  964. custom.CreateWriteEEPROOpenIntakeTimeBufferCommand(newValue);
  965. Enqueue(custom);
  966. taskAutoResetEvent.WaitOne();
  967. return;
  968. }
  969. #endregion
  970. #region 调试模式
  971. #endregion
  972. }
  973. }