ComBin.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. using ivf_tl_Entity.GlobalEnums;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ivf_tl_Entity.ComEntitys
  9. {
  10. /// <summary>
  11. /// 封装指令发送以及解析线程
  12. /// </summary>
  13. public class ComBin
  14. {
  15. /// <summary>
  16. /// 异常日志
  17. /// 异常、名称、参数
  18. /// </summary>
  19. public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
  20. /// <summary>
  21. /// 错误日志
  22. /// </summary>
  23. public event Action<string, LogEnum> ErrorLogEvent;
  24. /// <summary>
  25. /// 指令记录日志
  26. /// </summary>
  27. public event Action<int, DateTime, string, LogEnum> CommandLogEvent;
  28. /// <summary>
  29. /// 调试信息输出
  30. /// </summary>
  31. public event Action<string> AddMessageInfoEvent;
  32. public ComBin(int houseId, string portName)
  33. {
  34. this.houseId = houseId;
  35. this.portName = portName;
  36. _commandQueue = new Queue<CustomProtocol>();
  37. _channel = new Channel(houseId, portName);
  38. _channel.DataReceived += _channel_DataReceived;
  39. _channel.ExceptionLogEvent += _channel_ExceptionLogEvent;
  40. _channel.ErrorLogEvent += _channel_ErrorLogEvent;
  41. _channel.CommandLogEvent += _channel_CommandLogEvent; ;
  42. StartSendCommandThread();
  43. }
  44. private void _channel_CommandLogEvent(int arg1, DateTime arg2, string arg3, LogEnum arg4)
  45. {
  46. CommandLogEvent?.Invoke(arg1, arg2, arg3, arg4);
  47. }
  48. private void _channel_ErrorLogEvent(string arg1, LogEnum arg2)
  49. {
  50. ErrorLogEvent?.Invoke(arg1, arg2);
  51. }
  52. private void _channel_ExceptionLogEvent(Exception arg1, string arg2, string arg3, LogEnum arg4)
  53. {
  54. ExceptionLogEvent?.Invoke(arg1, arg2, arg3, arg4);
  55. }
  56. public Channel _channel { get; set; } = null;
  57. /// <summary>
  58. /// 是否停止发送指令线程循环
  59. /// </summary>
  60. public bool IsStop = false;
  61. /// <summary>
  62. /// 指令发送失败以后是否无限重发
  63. /// </summary>
  64. public bool IsStopWhile = true;
  65. private int houseId = 0;
  66. private string portName = null;
  67. /// <summary>
  68. /// 命令队列
  69. /// </summary>
  70. private Queue<CustomProtocol> _commandQueue;
  71. /// <summary>
  72. /// 指令超时重发等待毫秒
  73. /// </summary>
  74. private int milliseconds = 1000 * 30;
  75. /// <summary>
  76. /// 发送指令的线程同步控制器
  77. /// </summary>
  78. private AutoResetEvent sendAutoResetEvent = new AutoResetEvent(false);
  79. /// <summary>
  80. /// 运行线程等待指令的同步控制器
  81. /// </summary>
  82. private AutoResetEvent taskAutoResetEvent = new AutoResetEvent(false);
  83. /// <summary>
  84. /// 指令发送锁
  85. /// </summary>
  86. private object SendCommandLock = new object();
  87. /// <summary>
  88. /// 指令计数
  89. /// </summary>
  90. private double commandNumber = 1;
  91. /// <summary>
  92. /// 开启发送指令线程
  93. /// </summary>
  94. public void StartSendCommandThread()
  95. {
  96. Task.Factory.StartNew(() =>
  97. {
  98. Stopwatch SendStopWatch = new Stopwatch();
  99. CustomProtocol customProtocol = null;
  100. while (true)
  101. {
  102. try
  103. {
  104. if (IsStop)
  105. {
  106. return;
  107. }
  108. lock (_commandQueue)
  109. {
  110. if (_commandQueue.Any())
  111. {
  112. customProtocol = _commandQueue.Dequeue();
  113. }
  114. }
  115. if (customProtocol != null)
  116. {
  117. SendStopWatch.Restart();
  118. string Content = $"[{houseId}][{portName}][{customProtocol.commandNumber}][{customProtocol.commandType}][Send:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}]";
  119. if (customProtocol.commandType == Enums.VerticalMotorAbsolute)
  120. {
  121. AddMessageInfoEvent?.Invoke($"[{customProtocol.commandType}][发:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}][垂直电机位置:{customProtocol.VerticalMotorPulse}]");
  122. }
  123. else
  124. {
  125. AddMessageInfoEvent?.Invoke($"[{customProtocol.commandType}][发:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}]");
  126. }
  127. customProtocol.IsSuccess = SendCommand(customProtocol, Content);
  128. string Content11 = $"{Content}[Rec:{StringHelper.ByteToHexStr(customProtocol.receivedBuffer)}]";
  129. if (customProtocol.WaitTime != 0)
  130. {
  131. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[等待{customProtocol.WaitTime}毫秒]", LogEnum.HouseComRecord);
  132. Thread.Sleep(customProtocol.WaitTime);
  133. }
  134. CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[本轮指令总耗时:{StringHelper.TimeToString(SendStopWatch.Elapsed)}][End]", LogEnum.HouseComRecord);
  135. SendStopWatch.Stop();
  136. if (customProtocol.IsWaitOne)
  137. {
  138. taskAutoResetEvent.Set();
  139. }
  140. customProtocol = null;
  141. }
  142. else
  143. {
  144. //CommandLogEvent?.Invoke(houseId, DateTime.Now, $"空指令,等待50毫秒", LogEnum.HouseComRecord);
  145. Thread.Sleep(500);
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. ExceptionLogEvent?.Invoke(ex, $"[{houseId}][{portName}]指令发送线程", null, LogEnum.RunException);
  151. }
  152. }
  153. }, TaskCreationOptions.LongRunning);
  154. }
  155. /// <summary>
  156. /// 指令发送
  157. /// </summary>
  158. /// <param name="customProtocol"></param>
  159. /// <param name="Content"></param>
  160. private bool SendCommand(CustomProtocol customProtocol, string Content)
  161. {
  162. try
  163. {
  164. bool sendRs = _channel.Send(customProtocol);
  165. bool result = sendAutoResetEvent.WaitOne(milliseconds);//延时等待下位机回复
  166. // M8-P3b:单条指令收发为高频,走调试级(落本地文件、默认不入库,见 14§4)。全 try 兜底。
  167. try
  168. {
  169. Aivfo.OperationLog.OperationLogger.Log("串口",
  170. $"指令-{customProtocol.commandType}",
  171. input: Content,
  172. result: result ? "成功" : "失败",
  173. level: Aivfo.OperationLog.OpLogLevel.Debug,
  174. houseSn: houseId);
  175. }
  176. catch { }
  177. return result;
  178. }
  179. catch (Exception ex)
  180. {
  181. ExceptionLogEvent?.Invoke(ex, $"{Content}指令发送", null, LogEnum.RunException);
  182. return false;
  183. }
  184. }
  185. private void _channel_DataReceived(CustomProtocol obj)
  186. {
  187. AddMessageInfoEvent?.Invoke($"[{obj.commandType}][收:{StringHelper.ByteToHexStr(obj.receivedBuffer)}]");
  188. sendAutoResetEvent.Set();
  189. }
  190. /// <summary>
  191. /// 发送封装
  192. /// </summary>
  193. /// <param name="custom"></param>
  194. private void Enqueue(CustomProtocol custom)
  195. {
  196. lock (_commandQueue)
  197. {
  198. _commandQueue.Enqueue(custom);
  199. }
  200. }
  201. public bool OpenPort()
  202. {
  203. // M8-P3b:串口操作级埋点(打开端口入库)。全 try 兜底。
  204. bool rs = _channel.OpenPort();
  205. try
  206. {
  207. Aivfo.OperationLog.OperationLogger.Log("串口", "打开端口",
  208. input: new { houseId, portName },
  209. result: rs ? "成功" : "失败",
  210. houseSn: houseId);
  211. }
  212. catch { }
  213. return rs;
  214. }
  215. public bool ClosePort()
  216. {
  217. // M8-P3b:串口操作级埋点(关闭端口入库)。全 try 兜底。
  218. bool rs = _channel.ClosePort();
  219. try
  220. {
  221. Aivfo.OperationLog.OperationLogger.Log("串口", "关闭端口",
  222. input: new { houseId, portName },
  223. result: rs ? "成功" : "失败",
  224. houseSn: houseId);
  225. }
  226. catch { }
  227. return rs;
  228. }
  229. #region 舱室状态
  230. public bool AutoWait(CustomProtocol custom, bool auto)
  231. {
  232. custom.logDateTime = DateTime.Now;
  233. custom.commandNumber = commandNumber++;
  234. custom.commandType = Enums.AutoAirSwap;
  235. custom.CreateAutoCommand(auto);
  236. custom.IsWaitOne = true;
  237. Enqueue(custom);
  238. taskAutoResetEvent.WaitOne();
  239. if (!custom.IsSuccess) return false;
  240. return custom.receivedBuffer[custom.lenght - 2] == 0;
  241. }
  242. /// <summary>
  243. /// 握手指令
  244. /// </summary>
  245. public int ShakeHandsWait(CustomProtocol custom)
  246. {
  247. //握手
  248. custom.logDateTime = DateTime.Now;
  249. custom.commandNumber = commandNumber++;
  250. custom.commandType = Enums.ShakeHands;
  251. custom.CreateHandCommand();
  252. custom.IsWaitOne = true;
  253. Enqueue(custom);
  254. taskAutoResetEvent.WaitOne();
  255. if (!custom.IsSuccess) return -1;
  256. return Analysiser.ParseShakeHandsCommand(custom.receivedBuffer);
  257. }
  258. /// <summary>
  259. /// 读舱门状态
  260. /// </summary>
  261. /// <param name="custom"></param>
  262. /// <returns></returns>
  263. public State DoorStatusWait(CustomProtocol custom)
  264. {
  265. custom.commandType = Enums.DoorStatus;
  266. custom.logDateTime = DateTime.Now;
  267. custom.commandNumber = commandNumber++;
  268. custom.CreateReadDoorCommand();
  269. custom.IsWaitOne = true;
  270. Enqueue(custom);
  271. taskAutoResetEvent.WaitOne();
  272. if (!custom.IsSuccess) return State.未知;
  273. return Analysiser.ParseDoorStatus(custom.receivedBuffer);
  274. }
  275. /// <summary>
  276. /// 舱室温度
  277. /// </summary>
  278. /// <param name="custom"></param>
  279. /// <returns></returns>
  280. public decimal TemperatureWait(CustomProtocol custom)
  281. {
  282. custom.commandType = Enums.Temperature;
  283. custom.logDateTime = DateTime.Now;
  284. custom.commandNumber = commandNumber++;
  285. custom.CreateReadTemperatureCommand();
  286. custom.IsWaitOne = true;
  287. Enqueue(custom);
  288. taskAutoResetEvent.WaitOne();
  289. if (!custom.IsSuccess) return -1m;
  290. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  291. }
  292. /// <summary>
  293. /// 上盖板温度
  294. /// </summary>
  295. /// <param name="custom"></param>
  296. /// <returns></returns>
  297. public decimal ShangTemperatureWait(CustomProtocol custom)
  298. {
  299. custom.commandType = Enums.Temperature;
  300. custom.logDateTime = DateTime.Now;
  301. custom.commandNumber = commandNumber++;
  302. custom.CreateReadShangTemperatureCommand();
  303. custom.IsWaitOne = true;
  304. Enqueue(custom);
  305. taskAutoResetEvent.WaitOne();
  306. if (!custom.IsSuccess) return -1m;
  307. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  308. }
  309. /// <summary>
  310. /// 玻璃片下方温度
  311. /// </summary>
  312. /// <param name="custom"></param>
  313. /// <returns></returns>
  314. public decimal BoLiTemperatureWait(CustomProtocol custom)
  315. {
  316. custom.commandType = Enums.BoLiTemperature;
  317. custom.logDateTime = DateTime.Now;
  318. custom.commandNumber = commandNumber++;
  319. custom.CreateReadBoLiTemperatureCommand();
  320. custom.IsWaitOne = true;
  321. Enqueue(custom);
  322. taskAutoResetEvent.WaitOne();
  323. if (!custom.IsSuccess) return -1m;
  324. return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
  325. }
  326. /// <summary>
  327. /// 舱室压力
  328. /// </summary>
  329. /// <param name="custom"></param>
  330. /// <returns></returns>
  331. public decimal PressureWait(CustomProtocol custom)
  332. {
  333. custom.commandType = Enums.Pressure;
  334. custom.logDateTime = DateTime.Now;
  335. custom.commandNumber = commandNumber++;
  336. custom.CreateReadPressureCommand();
  337. custom.IsWaitOne = true;
  338. Enqueue(custom);
  339. taskAutoResetEvent.WaitOne();
  340. if (!custom.IsSuccess) return -1m;
  341. return Analysiser.ParsePressureCommand(custom.receivedBuffer);
  342. }
  343. /// <summary>
  344. /// 获取缓冲瓶状态
  345. /// </summary>
  346. /// <returns></returns>
  347. public (decimal, decimal, decimal) BufferBottleState(CustomProtocol custom)
  348. {
  349. custom.commandType = Enums.BufferBottlePressure;
  350. custom.logDateTime = DateTime.Now;
  351. custom.commandNumber = commandNumber++;
  352. custom.CreateBufferBottlePressureCommand();
  353. custom.IsWaitOne = true;
  354. Enqueue(custom);
  355. taskAutoResetEvent.WaitOne();
  356. if (!custom.IsSuccess) return (-1m, -1m, -1m);
  357. return (Analysiser.ParsePressureCommand(custom.receivedBuffer), Analysiser.ParseTLTemperature1Command(custom.receivedBuffer), Analysiser.ParseTLTemperature2Command(custom.receivedBuffer));
  358. }
  359. #endregion
  360. #region 电机
  361. /// <summary>
  362. /// 水平电机复位
  363. /// </summary>
  364. /// <param name="custom"></param>
  365. /// <param name="waitTime"></param>
  366. public bool HorizontalMotorResetWait(CustomProtocol custom, int waitTime)
  367. {
  368. custom.commandType = Enums.HorizontalMotorReset;
  369. custom.logDateTime = DateTime.Now;
  370. custom.commandNumber = commandNumber++;
  371. custom.CreateHorizontalMotorResetCommand();
  372. custom.IsWaitOne = true;
  373. Enqueue(custom);
  374. taskAutoResetEvent.WaitOne();
  375. if (waitTime > 0)
  376. {
  377. Thread.Sleep(waitTime);
  378. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  379. }
  380. return custom.IsSuccess;
  381. }
  382. /// <summary>
  383. /// 水平电机绝对运动
  384. /// </summary>
  385. /// <param name="custom"></param>
  386. /// <param name="waitTime"></param>
  387. /// <param name="zero"></param>
  388. /// <param name="newValue"></param>
  389. public bool HorizontalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int newValue)
  390. {
  391. custom.commandType = Enums.HorizontalMotorAbsolute;
  392. custom.logDateTime = DateTime.Now;
  393. custom.commandNumber = commandNumber++;
  394. custom.HorizontalMotorPulse = newValue;
  395. custom.CreateHorizontalMotorMoveToCommand(newValue);
  396. custom.IsWaitOne = true;
  397. Enqueue(custom);
  398. taskAutoResetEvent.WaitOne();
  399. if (waitTime > 0)
  400. {
  401. Thread.Sleep(waitTime);
  402. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  403. }
  404. return custom.IsSuccess;
  405. }
  406. /// <summary>
  407. /// 水平电机正向运动
  408. /// </summary>
  409. /// <param name="custom"></param>
  410. /// <param name="waitTime"></param>
  411. /// <param name="newValue"></param>
  412. /// <returns></returns>
  413. public bool HorizontalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
  414. {
  415. custom.commandType = Enums.HorizontalMotorForward;
  416. custom.logDateTime = DateTime.Now;
  417. custom.commandNumber = commandNumber++;
  418. custom.HorizontalMotorPulse = newValue;
  419. custom.CreateHorizontalMotorForwardCommand(newValue);
  420. custom.IsWaitOne = true;
  421. Enqueue(custom);
  422. taskAutoResetEvent.WaitOne();
  423. if (waitTime > 0)
  424. {
  425. Thread.Sleep(waitTime);
  426. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  427. }
  428. return custom.IsSuccess;
  429. }
  430. /// <summary>
  431. /// 水平电机反向运动
  432. /// </summary>
  433. /// <param name="custom"></param>
  434. /// <param name="waitTime"></param>
  435. /// <param name="newValue"></param>
  436. /// <returns></returns>
  437. public bool HorizontalMotorBackward(CustomProtocol custom, int waitTime, int newValue)
  438. {
  439. custom.commandType = Enums.HorizontalMotorBackward;
  440. custom.logDateTime = DateTime.Now;
  441. custom.commandNumber = commandNumber++;
  442. custom.HorizontalMotorPulse = newValue;
  443. custom.CreateHorizontalMotorBackwardCommand(newValue);
  444. custom.IsWaitOne = true;
  445. Enqueue(custom);
  446. taskAutoResetEvent.WaitOne();
  447. if (waitTime > 0)
  448. {
  449. Thread.Sleep(waitTime);
  450. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  451. }
  452. return custom.IsSuccess;
  453. }
  454. /// <summary>
  455. /// 读水平电机位置
  456. /// </summary>
  457. public int ReadHorizontalMotorWait(CustomProtocol custom)
  458. {
  459. custom.commandType = Enums.ReadHorizontalMotor;
  460. custom.logDateTime = DateTime.Now;
  461. custom.commandNumber = commandNumber++;
  462. custom.CreateReadHorizontalMotorCommand();
  463. custom.IsWaitOne = true;
  464. Enqueue(custom);
  465. taskAutoResetEvent.WaitOne();
  466. if (!custom.IsSuccess) return -1;
  467. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  468. }
  469. /// <summary>
  470. /// 读垂直电机位置
  471. /// </summary>
  472. public int ReadVerticalMotorWait(CustomProtocol custom)
  473. {
  474. custom.commandType = Enums.ReadVerticalMotor;
  475. custom.logDateTime = DateTime.Now;
  476. custom.commandNumber = commandNumber++;
  477. custom.CreateReadVerticalMotorCommand();
  478. custom.IsWaitOne = true;
  479. Enqueue(custom);
  480. taskAutoResetEvent.WaitOne();
  481. if (!custom.IsSuccess) return -1;
  482. return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
  483. }
  484. /// <summary>
  485. /// 垂直电机复位
  486. /// </summary>
  487. public bool VerticalMotorResetWait(CustomProtocol custom, int waitTime)
  488. {
  489. custom.commandType = Enums.VerticalMotorReset;
  490. custom.logDateTime = DateTime.Now;
  491. custom.commandNumber = commandNumber++;
  492. custom.CreateVerticalMotorResetCommand();
  493. custom.IsWaitOne = true;
  494. Enqueue(custom);
  495. taskAutoResetEvent.WaitOne();
  496. if (waitTime > 0)
  497. {
  498. Thread.Sleep(waitTime);
  499. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  500. }
  501. return custom.IsSuccess;
  502. }
  503. /// <summary>
  504. /// 垂直电机绝对运动
  505. /// </summary>
  506. /// <param name="custom">指令对象</param>
  507. /// <param name="waitTime">电机运动等待时间</param>
  508. /// <param name="currentVer">垂直电机目标位置</param>
  509. /// <param name="currentHor">当前水平电机位置</param>
  510. /// <param name="pictureId">图片编号</param>
  511. /// <param name="well">当前well</param>
  512. /// <param name="focal">当前层数</param>
  513. public bool VerticalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int currentVer, int currentHor, int pictureId, int well, int focal)
  514. {
  515. custom.commandType = Enums.VerticalMotorAbsolute;
  516. custom.logDateTime = DateTime.Now;
  517. custom.commandNumber = commandNumber++;
  518. custom.VerticalMotorPulse = currentVer;
  519. custom.HorizontalMotorPulse = currentHor;
  520. custom.pictureId = pictureId;
  521. custom.well = well;
  522. custom.focal = focal;
  523. custom.CreateVerticalMotorAbsoluteMovementCommand(currentVer);
  524. custom.IsWaitOne = true;
  525. Enqueue(custom);
  526. taskAutoResetEvent.WaitOne();
  527. if (waitTime > 0)
  528. {
  529. Thread.Sleep(waitTime);
  530. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  531. }
  532. return custom.IsSuccess;
  533. }
  534. /// <summary>
  535. /// 垂直电机正向运动
  536. /// </summary>
  537. public bool VerticalMotorForwardWait(CustomProtocol custom, int waitTime,int newValue)
  538. {
  539. custom.commandType = Enums.VerticalMotorForward;
  540. custom.logDateTime = DateTime.Now;
  541. custom.commandNumber = commandNumber++;
  542. custom.CreateVerticalMotorForwardCommand(newValue);
  543. custom.IsWaitOne = true;
  544. Enqueue(custom);
  545. taskAutoResetEvent.WaitOne();
  546. if (waitTime > 0)
  547. {
  548. Thread.Sleep(waitTime);
  549. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  550. }
  551. return custom.IsSuccess;
  552. }
  553. /// <summary>
  554. /// 垂直电机反向运动
  555. /// </summary>
  556. public bool VerticalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
  557. {
  558. custom.commandType = Enums.VerticalMotorBackward;
  559. custom.logDateTime = DateTime.Now;
  560. custom.commandNumber = commandNumber++;
  561. custom.CreateVerticalMotorBackwardCommand(newValue);
  562. custom.IsWaitOne = true;
  563. Enqueue(custom);
  564. taskAutoResetEvent.WaitOne();
  565. if (waitTime > 0)
  566. {
  567. Thread.Sleep(waitTime);
  568. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  569. }
  570. return custom.IsSuccess;
  571. }
  572. #endregion
  573. #region 控制
  574. /// <summary>
  575. /// 舱室补气
  576. /// </summary>
  577. /// <param name="custom"></param>
  578. public bool HouseAerationWait(CustomProtocol custom)
  579. {
  580. custom.commandType = Enums.HouseAeration;
  581. custom.logDateTime = DateTime.Now;
  582. custom.commandNumber = commandNumber++;
  583. custom.CreateHouseAerationCommand();
  584. custom.IsWaitOne = true;
  585. Enqueue(custom);
  586. taskAutoResetEvent.WaitOne();
  587. return custom.IsSuccess;
  588. }
  589. /// <summary>
  590. /// 舱室排气
  591. /// </summary>
  592. /// <param name="custom"></param>
  593. public bool HouseVentWait(CustomProtocol custom)
  594. {
  595. custom.commandType = Enums.HouseVent;
  596. custom.logDateTime = DateTime.Now;
  597. custom.commandNumber = commandNumber++;
  598. custom.CreateHouseVentCommand();
  599. custom.IsWaitOne = true;
  600. Enqueue(custom);
  601. taskAutoResetEvent.WaitOne();
  602. return custom.IsSuccess;
  603. }
  604. /// <summary>
  605. /// 缓冲瓶补气
  606. /// </summary>
  607. /// <param name="custom"></param>
  608. public bool BufferBottleAerationWait(CustomProtocol custom)
  609. {
  610. custom.commandType = Enums.BufferBottleAeration;
  611. custom.logDateTime = DateTime.Now;
  612. custom.commandNumber = commandNumber++;
  613. custom.CreateBufferBottleAerationCommand();
  614. custom.IsWaitOne = true;
  615. Enqueue(custom);
  616. taskAutoResetEvent.WaitOne();
  617. return custom.IsSuccess;
  618. }
  619. /// <summary>
  620. /// 打开舱室进气阀
  621. /// </summary>
  622. /// <param name="custom"></param>
  623. /// <param name="waitTime"></param>
  624. public bool OpenIntakeValveWait(CustomProtocol custom, int waitTime)
  625. {
  626. custom.commandType = Enums.OpenIntakeValve;
  627. custom.logDateTime = DateTime.Now;
  628. custom.commandNumber = commandNumber++;
  629. custom.CreateOpenIntakeValveCommand();
  630. custom.IsWaitOne = true;
  631. Enqueue(custom);
  632. taskAutoResetEvent.WaitOne();
  633. if (waitTime > 0)
  634. {
  635. Thread.Sleep(waitTime);
  636. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  637. }
  638. return custom.IsSuccess;
  639. }
  640. /// <summary>
  641. /// 关闭舱室进气阀
  642. /// </summary>
  643. /// <param name="custom"></param>
  644. /// <param name="waitTime"></param>
  645. public bool CloseIntakeValveWait(CustomProtocol custom, int waitTime)
  646. {
  647. custom.commandType = Enums.CloseIntakeValve;
  648. custom.logDateTime = DateTime.Now;
  649. custom.commandNumber = commandNumber++;
  650. custom.CreateCloseIntakeValveCommand();
  651. custom.IsWaitOne = true;
  652. Enqueue(custom);
  653. taskAutoResetEvent.WaitOne();
  654. if (waitTime > 0)
  655. {
  656. Thread.Sleep(waitTime);
  657. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  658. }
  659. return custom.IsSuccess;
  660. }
  661. /// <summary>
  662. /// 打开舱室排气阀
  663. /// </summary>
  664. /// <param name="custom"></param>
  665. public bool OpenExhaustValveWait(CustomProtocol custom, int waitTime)
  666. {
  667. custom.commandType = Enums.OpenExhaustValve;
  668. custom.logDateTime = DateTime.Now;
  669. custom.commandNumber = commandNumber++;
  670. custom.CreateOpenExhaustValveCommand();
  671. custom.IsWaitOne = true;
  672. Enqueue(custom);
  673. taskAutoResetEvent.WaitOne();
  674. if (waitTime > 0)
  675. {
  676. Thread.Sleep(waitTime);
  677. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  678. }
  679. return custom.IsSuccess;
  680. }
  681. /// <summary>
  682. /// 关闭排气阀
  683. /// </summary>
  684. /// <param name="custom"></param>
  685. /// <param name="waitTime"></param>
  686. public void CloseExhaustValveWait(CustomProtocol custom, int waitTime)
  687. {
  688. custom.commandType = Enums.CloseExhaustValve;
  689. custom.logDateTime = DateTime.Now;
  690. custom.commandNumber = commandNumber++;
  691. custom.CreateCloseExhaustValveCommand();
  692. custom.IsWaitOne = true;
  693. Enqueue(custom);
  694. taskAutoResetEvent.WaitOne();
  695. if (waitTime > 0)
  696. {
  697. Thread.Sleep(waitTime);
  698. AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
  699. }
  700. }
  701. /// <summary>
  702. /// 打开LEd灯
  703. /// </summary>
  704. /// <param name="custom"></param>
  705. public void OpenLEDWait(CustomProtocol custom)
  706. {
  707. custom.commandType = Enums.OpenLED;
  708. custom.logDateTime = DateTime.Now;
  709. custom.commandNumber = commandNumber++;
  710. custom.CreateOpenLEDCommand();
  711. custom.IsWaitOne = true;
  712. Enqueue(custom);
  713. taskAutoResetEvent.WaitOne();
  714. }
  715. /// <summary>
  716. /// 关闭Led灯
  717. /// </summary>
  718. /// <param name="custom"></param>
  719. public void CloseLEDWait(CustomProtocol custom)
  720. {
  721. custom.commandType = Enums.CloseLED;
  722. custom.logDateTime = DateTime.Now;
  723. custom.commandNumber = commandNumber++;
  724. custom.CreateCloseLEDCommand();
  725. custom.IsWaitOne = true;
  726. Enqueue(custom);
  727. taskAutoResetEvent.WaitOne();
  728. }
  729. #endregion
  730. #region 参数
  731. /// <summary>
  732. /// 读取TL仪器编号
  733. /// </summary>
  734. public int ReadTLNumWait(CustomProtocol custom)
  735. {
  736. custom.commandType = Enums.ReadTLNum;
  737. custom.logDateTime = DateTime.Now;
  738. custom.commandNumber = commandNumber++;
  739. custom.CreateReadTLNumCommand();
  740. custom.IsWaitOne = true;
  741. Enqueue(custom);
  742. taskAutoResetEvent.WaitOne();
  743. if (!custom.IsSuccess) return -1;
  744. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  745. }
  746. /// <summary>
  747. /// 获取CCDSN
  748. /// </summary>
  749. public int GetCCDSNWait(CustomProtocol custom)
  750. {
  751. custom.commandType = Enums.GetCCDSN;
  752. custom.logDateTime = DateTime.Now;
  753. custom.commandNumber = commandNumber++;
  754. custom.CreateGetModuleCommand();
  755. custom.IsWaitOne = true;
  756. Enqueue(custom);
  757. taskAutoResetEvent.WaitOne();
  758. if (!custom.IsSuccess) return -1;
  759. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  760. }
  761. /// <summary>
  762. /// 读E方-->水平电机位置
  763. /// </summary>
  764. public int ReadEEPROMhoriMtWellHorHorWait(CustomProtocol custom, int horIndex)
  765. {
  766. custom.commandType = Enums.ReadEEPROMhoriMtWellHor;
  767. custom.logDateTime = DateTime.Now;
  768. custom.commandNumber = commandNumber++;
  769. custom.CreateReadEEPROMhoriMtWellHoriPos(horIndex);
  770. custom.IsWaitOne = true;
  771. Enqueue(custom);
  772. taskAutoResetEvent.WaitOne();
  773. if (!custom.IsSuccess) return -1;
  774. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  775. }
  776. /// <summary>
  777. /// 写E方-->水平电机位置
  778. /// </summary>
  779. /// <param name="custom"></param>
  780. /// <param name="newValue"></param>
  781. public void WriteEEPROMhoriMtWellHorHorWait(CustomProtocol custom,int wellSn, int newValue)
  782. {
  783. custom.commandType = Enums.WriteEEPROMhoriMtWell1HoriPos;
  784. custom.logDateTime = DateTime.Now;
  785. custom.commandNumber = commandNumber++;
  786. custom.IsWaitOne = true;
  787. custom.CreateWriteEEPROMhoriMtWellHoriPos(wellSn,newValue);
  788. Enqueue(custom);
  789. taskAutoResetEvent.WaitOne();
  790. if (!custom.IsSuccess) return;
  791. return;
  792. }
  793. /// <summary>
  794. /// 读E方-->垂直电机清晰位置
  795. /// </summary>
  796. /// <returns></returns>
  797. public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom)
  798. {
  799. custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
  800. custom.logDateTime = DateTime.Now;
  801. custom.commandNumber = commandNumber++;
  802. custom.CreateReadEEPROMvertMtStartPulse(1);
  803. custom.IsWaitOne = true;
  804. Enqueue(custom);
  805. taskAutoResetEvent.WaitOne();
  806. if (!custom.IsSuccess) return -1;
  807. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  808. }
  809. /// <summary>
  810. /// 读E方-->垂直电机间隔脉冲
  811. /// </summary>
  812. /// <returns></returns>
  813. public int ReadEEPROMvertMtScanPluseWait(CustomProtocol custom)
  814. {
  815. custom.commandType = Enums.ReadEEPROMvertMtScanPluse;
  816. custom.logDateTime = DateTime.Now;
  817. custom.commandNumber = commandNumber++;
  818. custom.IsWaitOne = true;
  819. custom.CreateReadEEPROMvertMtScanPluse();
  820. Enqueue(custom);//断层扫描间隔数
  821. taskAutoResetEvent.WaitOne();
  822. if (!custom.IsSuccess) return -1;
  823. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  824. }
  825. /// <summary>
  826. /// 写E方-->垂直电机间隔脉冲
  827. /// </summary>
  828. /// <param name="custom"></param>
  829. /// <param name="newValue"></param>
  830. public void WriteEEPROMvertMtScanPluseWait(CustomProtocol custom, int newValue)
  831. {
  832. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  833. custom.logDateTime = DateTime.Now;
  834. custom.commandNumber = commandNumber++;
  835. custom.IsWaitOne = true;
  836. custom.CreateWriteEEPROMvertMtScanPluse(newValue);
  837. Enqueue(custom);
  838. taskAutoResetEvent.WaitOne();
  839. if (!custom.IsSuccess) return;
  840. return;
  841. }
  842. /// <summary>
  843. /// 读E方-->舱室进气阀打开时间
  844. /// </summary>
  845. /// <returns></returns>
  846. public int ReadEEPROOpenIntakeTimeWait(CustomProtocol custom)
  847. {
  848. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  849. custom.logDateTime = DateTime.Now;
  850. custom.commandNumber = commandNumber++;
  851. custom.IsWaitOne = true;
  852. custom.CreateReadEEPROOpenIntakeTimeCommand();
  853. Enqueue(custom);
  854. taskAutoResetEvent.WaitOne();
  855. if (!custom.IsSuccess) return -1;
  856. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  857. }
  858. /// <summary>
  859. /// 写E方-->舱室进气阀打开时间
  860. /// </summary>
  861. /// <param name="custom"></param>
  862. /// <param name="newValue"></param>
  863. public void WriteEEPROOpenIntakeTimeWait(CustomProtocol custom,int newValue)
  864. {
  865. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  866. custom.logDateTime = DateTime.Now;
  867. custom.commandNumber = commandNumber++;
  868. custom.IsWaitOne = true;
  869. custom.CreateWriteEEPROOpenIntakeTimeCommand(newValue);
  870. Enqueue(custom);
  871. taskAutoResetEvent.WaitOne();
  872. if (!custom.IsSuccess) return;
  873. return;
  874. }
  875. /// <summary>
  876. /// 写E方-->舱室排气阀打开时间
  877. /// </summary>
  878. /// <param name="custom"></param>
  879. /// <param name="newValue"></param>
  880. public void WriteEEPROOpenVentTimeWait(CustomProtocol custom, int newValue)
  881. {
  882. custom.commandType = Enums.WriteEEPROOpenVentTime;
  883. custom.logDateTime = DateTime.Now;
  884. custom.commandNumber = commandNumber++;
  885. custom.IsWaitOne = true;
  886. custom.CreateWriteEEPROOpenVentTimeCommand(newValue);
  887. Enqueue(custom);
  888. taskAutoResetEvent.WaitOne();
  889. if (!custom.IsSuccess) return;
  890. return;
  891. }
  892. /// <summary>
  893. /// 读E方-->舱室排气阀打开时间
  894. /// </summary>
  895. /// <returns></returns>
  896. public int ReadEEPROMVentWait(CustomProtocol custom)
  897. {
  898. custom.commandType = Enums.ReadEEPROOpenVentTime;
  899. custom.logDateTime = DateTime.Now;
  900. custom.commandNumber = commandNumber++;
  901. custom.IsWaitOne = true;
  902. custom.CreateReadEEPROMVentNum();
  903. Enqueue(custom);
  904. taskAutoResetEvent.WaitOne();
  905. if (!custom.IsSuccess) return -1;
  906. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  907. }
  908. /// <summary>
  909. /// 读E方-->缓冲瓶进气阀打开时间
  910. /// </summary>
  911. /// <returns></returns>
  912. public int ReadEEPROOpenIntakeTimeBufferWait(CustomProtocol custom)
  913. {
  914. custom.commandType = Enums.ReadEEPROOpenIntakeTime;
  915. custom.logDateTime = DateTime.Now;
  916. custom.commandNumber = commandNumber++;
  917. custom.IsWaitOne = true;
  918. custom.CreateReadEEPROOpenIntakeTimeBufferCommand();
  919. Enqueue(custom);
  920. taskAutoResetEvent.WaitOne();
  921. if (!custom.IsSuccess) return -1;
  922. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  923. }
  924. /// <summary>
  925. /// 写E方-->缓冲瓶进气阀打开时间
  926. /// </summary>
  927. /// <returns></returns>
  928. public void WriteEEPROOpenIntakeTimeBufferWait(CustomProtocol custom,int newValue)
  929. {
  930. custom.commandType = Enums.WriteEEPROOpenIntakeTime;
  931. custom.logDateTime = DateTime.Now;
  932. custom.commandNumber = commandNumber++;
  933. custom.IsWaitOne = true;
  934. custom.CreateWriteEEPROOpenIntakeTimeBufferCommand(newValue);
  935. Enqueue(custom);
  936. taskAutoResetEvent.WaitOne();
  937. if (!custom.IsSuccess) return;
  938. return;
  939. }
  940. /// <summary>
  941. /// 写E方-->灯光亮度
  942. /// </summary>
  943. /// <returns></returns>
  944. public void WriteEEPROMLightNumWait(CustomProtocol custom, int newValue)
  945. {
  946. custom.commandType = Enums.WriteLightNum;
  947. custom.logDateTime = DateTime.Now;
  948. custom.commandNumber = commandNumber++;
  949. custom.IsWaitOne = true;
  950. custom.CreateWriteEEPROMLightNum(newValue);
  951. Enqueue(custom);
  952. taskAutoResetEvent.WaitOne();
  953. if (!custom.IsSuccess) return;
  954. return;
  955. }
  956. /// <summary>
  957. /// 读E方-->灯光亮度
  958. /// </summary>
  959. /// <returns></returns>
  960. public int ReadEEPROMLightNumWait(CustomProtocol custom)
  961. {
  962. custom.commandType = Enums.LightNum;
  963. custom.logDateTime = DateTime.Now;
  964. custom.commandNumber = commandNumber++;
  965. custom.IsWaitOne = true;
  966. custom.CreateReadEEPROMLightNum();
  967. Enqueue(custom);
  968. taskAutoResetEvent.WaitOne();
  969. if (!custom.IsSuccess) return -1;
  970. return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  971. }
  972. /// <summary>
  973. /// 读E方-->下加热板目标温度
  974. /// </summary>
  975. /// <returns></returns>
  976. public decimal ReadTargetTempWait(CustomProtocol custom)
  977. {
  978. custom.commandType = Enums.ReadTargetTemp;
  979. custom.logDateTime = DateTime.Now;
  980. custom.commandNumber = commandNumber++;
  981. custom.CreateReadTargetTemp();
  982. custom.IsWaitOne = true;
  983. Enqueue(custom);
  984. taskAutoResetEvent.WaitOne();
  985. if (!custom.IsSuccess) return -1m;
  986. var num = Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
  987. return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
  988. }
  989. #endregion
  990. #region 调试模式
  991. #endregion
  992. }
  993. }