SerialBin.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using ivf_tl_Entity.CameraEntitys;
  2. using ivf_tl_Entity.DebugEntitys;
  3. using ivf_tl_Entity.GlobalEntitys;
  4. using ivf_tl_Entity.GlobalEnums;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ivf_tl_Entity.ComEntitys
  12. {
  13. /// <summary>
  14. /// 开机获取串口信息
  15. /// </summary>
  16. public class SerialBin
  17. {
  18. public event Action<string, LogEnum> TLLogEvent;
  19. public event Action<Exception, string, LogEnum> ExceptionLogEvent;
  20. public event Action<int, DateTime, string, LogEnum> HouseLogEvent;
  21. public Dictionary<int, string> CCDidSn = new Dictionary<int, string>();
  22. public List<HouseInitData> SerialModels = new List<HouseInitData>();
  23. public List<HouseEEPROInfo> HouseEEPROInfos = new List<HouseEEPROInfo>();
  24. private List<string> errorlist = new List<string>();
  25. public int TLNum = -1;
  26. public int dayLighting = -1;
  27. private object lockObject = new object();
  28. /// <summary>
  29. /// 获取相机对应的ccdid
  30. /// </summary>
  31. public List<string> UpdataCamera()
  32. {
  33. try
  34. {
  35. CCDidSn.Clear();
  36. Task[] tasks = new Task[10];
  37. for (int i = 0; i < 10; i++)
  38. {
  39. int ccdid = i;
  40. tasks[ccdid] = Task.Run(() =>
  41. {
  42. GetCameraSn(ccdid);
  43. });
  44. }
  45. Task.WaitAll(tasks);
  46. return errorlist;
  47. }
  48. catch (Exception ex)
  49. {
  50. ExceptionLogEvent?.Invoke(ex, "获取ccdid异常", LogEnum.RunException);
  51. lock (lockObject)
  52. {
  53. errorlist.Add($"获取ccdid异常");
  54. }
  55. return errorlist;
  56. }
  57. }
  58. private void GetCameraSn(int i)
  59. {
  60. try
  61. {
  62. int InitNumber = -1;
  63. int GetSnNumber = -1;
  64. string SnNumber = null;
  65. int UnInitNumber = -1;
  66. Camera Camera = new Camera(i, 100, 100, 100);
  67. for (int j = 0; j < 3; j++)
  68. {
  69. InitNumber = Camera.Init();
  70. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机初始化结果:{InitNumber}", LogEnum.RunRecord);
  71. if (InitNumber == 0)
  72. {
  73. GetSnNumber = Camera.GetNumbet();
  74. SnNumber = Camera.NumBer;
  75. TLLogEvent?.Invoke($"获取序列号结果:{GetSnNumber};序列号:{SnNumber}", LogEnum.RunRecord);
  76. UnInitNumber = Camera.UnInit();
  77. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord);
  78. break;
  79. }
  80. else
  81. {
  82. UnInitNumber = Camera.UnInit();
  83. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord);
  84. }
  85. }
  86. if (SnNumber == null)
  87. {
  88. lock (lockObject)
  89. {
  90. CCDidSn.Add(i, SnNumber);
  91. }
  92. }
  93. else
  94. {
  95. lock (lockObject)
  96. {
  97. if (CCDidSn.ContainsValue(SnNumber))
  98. {
  99. errorlist.Add($"ccdid:{i};ccdsn:{SnNumber}");
  100. }
  101. else
  102. {
  103. CCDidSn.Add(i, SnNumber);
  104. }
  105. }
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. ExceptionLogEvent?.Invoke(ex, $"获取{i}号相机ccdSn异常", LogEnum.RunException);
  111. lock (lockObject)
  112. {
  113. errorlist.Add($"获取{i}号相机ccdSn异常");
  114. }
  115. }
  116. }
  117. //List<house> Houses = new List<house>();
  118. public List<string> Start(string autoFcous)
  119. {
  120. try
  121. {
  122. string[] ArryPort = SerialPort.GetPortNames();
  123. int portNum = ArryPort.Length;
  124. if (portNum > 0)
  125. {
  126. List<Task> listTask = new List<Task>();
  127. for (int i = 0; i < portNum; i++)
  128. {
  129. string portName = ArryPort[i];
  130. if (portName == "COM1" || portName == "COM2")
  131. {
  132. continue;
  133. }
  134. if (portName.Contains("COM"))
  135. {
  136. listTask.Add(Task.Run(() =>
  137. {
  138. GetHouseInfo(portName, autoFcous);
  139. }));
  140. }
  141. }
  142. Task.WaitAll(listTask.ToArray());
  143. }
  144. return errorlist;
  145. }
  146. catch (Exception ex)
  147. {
  148. ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常", LogEnum.RunException);
  149. lock (lockObject)
  150. {
  151. errorlist.Add($"获取端口信息异常:{ex.Message}");
  152. }
  153. return errorlist;
  154. }
  155. }
  156. private void GetHouseInfo(string portName,string autoFocus)
  157. {
  158. try
  159. {
  160. //下加热板目标温度
  161. //舱室和缓冲瓶进气阀打开时间
  162. //ccdSN
  163. //ccdid
  164. HouseInitData serialModel = new HouseInitData();
  165. serialModel.housePort = portName;
  166. var custom = new CustomProtocol();
  167. ComBin comBin = new ComBin(0, portName);
  168. comBin.IsStopWhile = false;
  169. comBin.CommandLogEvent += HouseLogEvent;
  170. comBin.ErrorLogEvent += TLLogEvent;
  171. comBin.ExceptionLogEvent += ComBin_ExceptionLogEvent;
  172. bool isOpenPort = comBin.OpenPort();
  173. TLLogEvent?.Invoke($"打开端口{portName}结果:{isOpenPort}", LogEnum.RunRecord);
  174. if (!isOpenPort)
  175. {
  176. errorlist.Add($"串口打开失败");
  177. goto CC;
  178. }
  179. if(autoFocus == "1")
  180. {
  181. if (!comBin.AutoWait(custom, false))
  182. {
  183. errorlist.Add($"{portName}关闭自动换气失败");
  184. goto CC;
  185. }
  186. }
  187. serialModel.houseSn = comBin.ShakeHandsWait(custom);
  188. if (!custom.IsSuccess)
  189. {
  190. errorlist.Add($"{portName}握手失败");
  191. goto CC;
  192. }
  193. lock (lockObject)
  194. {
  195. var itemModel = SerialModels.FirstOrDefault(x => x.houseSn == serialModel.houseSn);
  196. if (itemModel != null)
  197. {
  198. errorlist.Add($"从{serialModel.housePort}串口获取到的模块号{serialModel.houseSn}和{itemModel.housePort}串口获取到的模块号{itemModel.houseSn}重复");
  199. goto CC;
  200. }
  201. }
  202. if (serialModel.houseSn == 11)
  203. {
  204. TLNum = comBin.ReadTLNumWait(custom);
  205. if (!custom.IsSuccess) goto CC;
  206. dayLighting = comBin.ReadEEPROMLightNumWait(custom);
  207. if (!custom.IsSuccess) goto CC;
  208. serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeBufferWait(custom);
  209. if (!custom.IsSuccess) goto CC;
  210. serialModel.ccdSn = "-1";
  211. lock (lockObject)
  212. {
  213. SerialModels.Add(serialModel);
  214. }
  215. }
  216. else
  217. {
  218. serialModel.temperatureLowerHeatingPlate = comBin.ReadTargetTempWait(custom);
  219. if (!custom.IsSuccess) goto CC;
  220. serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeWait(custom);
  221. if (!custom.IsSuccess) goto CC;
  222. serialModel.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom);
  223. if (!custom.IsSuccess) goto CC;
  224. var currentCCDSN = comBin.GetCCDSNWait(custom).ToString();
  225. if (!custom.IsSuccess) goto CC;
  226. lock (lockObject)
  227. {
  228. var itemModel = SerialModels.FirstOrDefault(x => x.ccdSn == currentCCDSN);
  229. if (itemModel == null)
  230. {
  231. if (CCDidSn.ContainsValue(currentCCDSN))
  232. {
  233. serialModel.ccdId = CCDidSn.FirstOrDefault(x => x.Value == currentCCDSN).Key;
  234. }
  235. else
  236. {
  237. errorlist.Add($"相机列表中不存在舱室的CCDSN{currentCCDSN}");
  238. }
  239. serialModel.ccdSn = currentCCDSN;
  240. SerialModels.Add(serialModel);
  241. }
  242. else
  243. {
  244. errorlist.Add($"从{serialModel.houseSn}号模块的{serialModel.housePort}串口获取到的{currentCCDSN}和{itemModel.ccdSn}模块{itemModel.housePort}串口的{itemModel.ccdSn}重复");
  245. }
  246. }
  247. HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo();
  248. houseEEPROInfo.houseSn = serialModel.houseSn;
  249. houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1);
  250. if (!custom.IsSuccess) goto CC;
  251. houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2);
  252. if (!custom.IsSuccess) goto CC;
  253. houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3);
  254. if (!custom.IsSuccess) goto CC;
  255. houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4);
  256. if (!custom.IsSuccess) goto CC;
  257. houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5);
  258. if (!custom.IsSuccess) goto CC;
  259. houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6);
  260. if (!custom.IsSuccess) goto CC;
  261. houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7);
  262. if (!custom.IsSuccess) goto CC;
  263. houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8);
  264. if (!custom.IsSuccess) goto CC;
  265. houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9);
  266. if (!custom.IsSuccess) goto CC;
  267. houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10);
  268. if (!custom.IsSuccess) goto CC;
  269. houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11);
  270. if (!custom.IsSuccess) goto CC;
  271. houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12);
  272. if (!custom.IsSuccess) goto CC;
  273. houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13);
  274. if (!custom.IsSuccess) goto CC;
  275. houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14);
  276. if (!custom.IsSuccess) goto CC;
  277. houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15);
  278. if (!custom.IsSuccess) goto CC;
  279. houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16);
  280. if (!custom.IsSuccess) goto CC;
  281. houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom);
  282. if (!custom.IsSuccess) goto CC;
  283. lock (lockObject)
  284. {
  285. HouseEEPROInfos.Add(houseEEPROInfo);
  286. }
  287. }
  288. CC:
  289. comBin.IsStop = true;
  290. var ClosePort = comBin.ClosePort();
  291. TLLogEvent?.Invoke($"关闭端口{portName}结果:{ClosePort}", LogEnum.RunRecord);
  292. }
  293. catch (Exception ex)
  294. {
  295. ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常{portName}", LogEnum.RunException);
  296. lock (lockObject)
  297. {
  298. errorlist.Add($"获取端口信息异常{portName}:{ex.Message}");
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 读取舱室E方数据
  304. /// </summary>
  305. public List<string> HousesEEPRO()
  306. {
  307. try
  308. {
  309. int num = SerialModels.Count;
  310. List<Task> tasks = new List<Task>();
  311. foreach (var item in SerialModels)
  312. {
  313. if (item.houseSn == 11)
  314. {
  315. continue;
  316. }
  317. tasks.Add(Task.Run(() =>
  318. {
  319. HouseEEPRO(item);
  320. }));
  321. }
  322. Task.WaitAll(tasks.ToArray());
  323. return errorlist;
  324. }
  325. catch (Exception ex)
  326. {
  327. ExceptionLogEvent?.Invoke(ex, $"获取舱室E方信息", LogEnum.RunException);
  328. lock (lockObject)
  329. {
  330. errorlist.Add($"获取舱室E方信息异常:{ex.Message}");
  331. }
  332. return errorlist;
  333. }
  334. }
  335. private void HouseEEPRO(HouseInitData serialModel)
  336. {
  337. //1-16号水平电机位置
  338. //垂直电机间隔脉冲
  339. //E方清晰地址
  340. try
  341. {
  342. HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo();
  343. houseEEPROInfo.houseSn = serialModel.houseSn;
  344. var custom = new CustomProtocol();
  345. ComBin comBin = new ComBin(serialModel.houseSn, serialModel.housePort);
  346. comBin.IsStopWhile = false;
  347. comBin.CommandLogEvent += HouseLogEvent;
  348. comBin.ErrorLogEvent += TLLogEvent;
  349. comBin.ExceptionLogEvent += ComBin_ExceptionLogEvent;
  350. for (int i = 0; i < 3; i++)
  351. {
  352. if (i != 0) TLLogEvent?.Invoke($"第{i + 1}次关闭端口{serialModel.housePort}结果:{comBin.ClosePort()}", LogEnum.RunRecord);
  353. bool isOpenPort = comBin.OpenPort();
  354. HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]打开端口结果:{isOpenPort}", LogEnum.HouseInfo);
  355. if (!isOpenPort)
  356. {
  357. continue;
  358. }
  359. houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1);
  360. if (!custom.IsSuccess) continue;
  361. houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2);
  362. if (!custom.IsSuccess) continue;
  363. houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3);
  364. if (!custom.IsSuccess) continue;
  365. houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4);
  366. if (!custom.IsSuccess) continue;
  367. houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5);
  368. if (!custom.IsSuccess) continue;
  369. houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6);
  370. if (!custom.IsSuccess) continue;
  371. houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7);
  372. if (!custom.IsSuccess) continue;
  373. houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8);
  374. if (!custom.IsSuccess) continue;
  375. houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9);
  376. if (!custom.IsSuccess) continue;
  377. houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10);
  378. if (!custom.IsSuccess) continue;
  379. houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11);
  380. if (!custom.IsSuccess) continue;
  381. houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12);
  382. if (!custom.IsSuccess) continue;
  383. houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13);
  384. if (!custom.IsSuccess) continue;
  385. houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14);
  386. if (!custom.IsSuccess) continue;
  387. houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15);
  388. if (!custom.IsSuccess) continue;
  389. houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16);
  390. if (!custom.IsSuccess) continue;
  391. //houseEEPROInfo.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom);
  392. //if (!custom.IsSuccess) continue;
  393. houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom);
  394. if (!custom.IsSuccess) continue;
  395. lock (lockObject)
  396. {
  397. HouseEEPROInfos.Add(houseEEPROInfo);
  398. }
  399. break;
  400. }
  401. comBin.IsStop = true;
  402. var ClosePort = comBin.ClosePort();
  403. HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]关闭端口结果:{ClosePort}", LogEnum.HouseInfo);
  404. }
  405. catch (Exception ex)
  406. {
  407. ExceptionLogEvent?.Invoke(ex, $"[{serialModel.houseSn}][{serialModel.housePort}]获取舱室E方信息", LogEnum.RunException);
  408. lock (lockObject)
  409. {
  410. errorlist.Add($"[{serialModel.houseSn}][{serialModel.housePort}]获取舱室E方信息异常:{ex.Message}");
  411. }
  412. }
  413. }
  414. private void ComBin_ExceptionLogEvent(Exception arg1, string arg2, string arg3, LogEnum arg4)
  415. {
  416. ExceptionLogEvent?.Invoke(arg1, $"{arg2}{arg3}", arg4);
  417. }
  418. }
  419. }