SerialBin.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using DBEntity;
  2. using ivf_tl_CameraHelper;
  3. using IvfTl.Control.Entity;
  4. using IvfTl.Control.Entity.GlobalEnums;
  5. using IvfTl.Control.Entity.InitEntitys;
  6. using ivf_tl_SerialHelper.Util;
  7. using IvfTl.Hardware.Impl;
  8. using NetTaste;
  9. using Newtonsoft.Json;
  10. using System.IO.Ports;
  11. namespace ivf_tl_Com
  12. {
  13. /// <summary>
  14. /// 开机获取串口信息
  15. /// </summary>
  16. public class SerialBin
  17. {
  18. public event Action<string, LogEnum> TLLogEvent;
  19. public event Action<Exception, string, 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异常", null, 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. // M1-03 HAL: 开机枚举相机 SN 也向 HAL 借用唯一相机实例(不再 new,避免与采集端句柄争用)。
  67. // 注:HAL.ScanDevices 已做相同的相机 SN 枚举+CCDSN 配对,初始化后应以 HAL 发现结果为准;
  68. // 此处保留旧枚举作为过渡,但句柄统一交 HAL 持有(13 §④ / M1-03 步骤4 范围控制)。
  69. Camera Camera = (Camera)HardwareAccessLayer.Instance.GetCamera(i).RawCamera;
  70. for (int j = 0; j < 3; j++)
  71. {
  72. InitNumber = Camera.Init();
  73. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机初始化结果:{InitNumber}", LogEnum.RunRecord);
  74. if (InitNumber == 0)
  75. {
  76. GetSnNumber = Camera.GetNumbet();
  77. SnNumber = Camera.NumBer;
  78. TLLogEvent?.Invoke($"获取序列号结果:{GetSnNumber};序列号:{SnNumber}", LogEnum.RunRecord);
  79. UnInitNumber = Camera.UnInit();
  80. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord);
  81. break;
  82. }
  83. else
  84. {
  85. UnInitNumber = Camera.UnInit();
  86. TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord);
  87. }
  88. }
  89. if (SnNumber == null)
  90. {
  91. lock (lockObject)
  92. {
  93. CCDidSn.Add(i, SnNumber);
  94. }
  95. }
  96. else
  97. {
  98. lock (lockObject)
  99. {
  100. if (CCDidSn.ContainsValue(SnNumber))
  101. {
  102. errorlist.Add($"ccdid:{i};ccdsn:{SnNumber}");
  103. }
  104. else
  105. {
  106. CCDidSn.Add(i, SnNumber);
  107. }
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. ExceptionLogEvent?.Invoke(ex, $"获取{i}号相机ccdSn异常", null, LogEnum.RunException);
  114. lock (lockObject)
  115. {
  116. errorlist.Add($"获取{i}号相机ccdSn异常");
  117. }
  118. }
  119. }
  120. List<house> Houses = new List<house>();
  121. public List<string> Start()
  122. {
  123. try
  124. {
  125. string[] ArryPort = SerialPort.GetPortNames();
  126. int portNum = ArryPort.Length;
  127. if (portNum > 0)
  128. {
  129. List<Task> listTask = new List<Task>();
  130. for (int i = 0; i < portNum; i++)
  131. {
  132. string portName = ArryPort[i];
  133. if (portName == "COM1" || portName == "COM2")
  134. {
  135. continue;
  136. }
  137. if (portName.Contains("COM"))
  138. {
  139. listTask.Add(Task.Run(() =>
  140. {
  141. GetHouseInfo(portName);
  142. }));
  143. }
  144. }
  145. Task.WaitAll(listTask.ToArray());
  146. }
  147. return errorlist;
  148. }
  149. catch (Exception ex)
  150. {
  151. ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常", null, LogEnum.RunException);
  152. lock (lockObject)
  153. {
  154. errorlist.Add($"获取端口信息异常:{ex.Message}");
  155. }
  156. return errorlist;
  157. }
  158. }
  159. private void GetHouseInfo(string portName)
  160. {
  161. try
  162. {
  163. //下加热板目标温度
  164. //仓室和缓冲瓶进气阀打开时间
  165. //ccdSN
  166. //ccdid
  167. HouseInitData serialModel = new HouseInitData();
  168. serialModel.housePort = portName;
  169. var custom = new CustomProtocol();
  170. custom.commandSource = CommandSource.GetModuleThread;
  171. // M1-03 HAL: 开机枚举舱信息向 HAL 借用唯一 ComBin(不再 new,杜绝与采集端同口重复 Open)。
  172. ComBin comBin = (ComBin)HardwareAccessLayer.Instance.GetSerial(0, portName).RawComBin;
  173. comBin.IsStopWhile = false;
  174. comBin.CommandLogEvent += HouseLogEvent;
  175. comBin.ErrorLogEvent += TLLogEvent;
  176. comBin.ExceptionLogEvent += ExceptionLogEvent;
  177. bool isOpenPort = comBin.OpenPort();
  178. TLLogEvent?.Invoke($"打开端口{portName}结果:{isOpenPort}", LogEnum.RunRecord);
  179. if (!isOpenPort) goto CC;
  180. serialModel.houseSn = comBin.ShakeHandsWait(custom);
  181. if (!custom.IsSuccess) goto CC;
  182. lock (lockObject)
  183. {
  184. var itemModel = SerialModels.FirstOrDefault(x => x.houseSn == serialModel.houseSn);
  185. if (itemModel != null)
  186. {
  187. errorlist.Add($"从{serialModel.housePort}串口获取到的模块号{serialModel.houseSn}和{itemModel.housePort}串口获取到的模块号{itemModel.houseSn}重复");
  188. goto CC;
  189. }
  190. }
  191. if (serialModel.houseSn == 11)
  192. {
  193. TLNum = comBin.ReadTLNumWait(custom);
  194. if (!custom.IsSuccess) goto CC;
  195. dayLighting = comBin.ReadEEPROMLightNumWait(custom);
  196. if (!custom.IsSuccess) goto CC;
  197. serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeBufferWait(custom);
  198. if (!custom.IsSuccess) goto CC;
  199. serialModel.ccdSn = "-1";
  200. lock (lockObject)
  201. {
  202. SerialModels.Add(serialModel);
  203. }
  204. }
  205. else
  206. {
  207. serialModel.temperatureLowerHeatingPlate = comBin.ReadTargetTempWait(custom);
  208. if (!custom.IsSuccess) goto CC;
  209. serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeWait(custom);
  210. if (!custom.IsSuccess) goto CC;
  211. serialModel.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom);
  212. if (!custom.IsSuccess) goto CC;
  213. var currentCCDSN = comBin.GetCCDSNWait(custom).ToString();
  214. if (!custom.IsSuccess) goto CC;
  215. lock (lockObject)
  216. {
  217. var itemModel = SerialModels.FirstOrDefault(x => x.ccdSn == currentCCDSN);
  218. if (itemModel == null)
  219. {
  220. if(CCDidSn.ContainsValue(currentCCDSN))
  221. {
  222. serialModel.ccdId = CCDidSn.FirstOrDefault(x => x.Value == currentCCDSN).Key;
  223. }
  224. else
  225. {
  226. errorlist.Add($"相机列表中不存在仓室的CCDSN{currentCCDSN}");
  227. }
  228. serialModel.ccdSn = currentCCDSN;
  229. SerialModels.Add(serialModel);
  230. }
  231. else
  232. {
  233. errorlist.Add($"从{serialModel.houseSn}号模块的{serialModel.housePort}串口获取到的{currentCCDSN}和{itemModel.ccdSn}模块{itemModel.housePort}串口的{itemModel.ccdSn}重复");
  234. }
  235. }
  236. HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo();
  237. houseEEPROInfo.houseSn = serialModel.houseSn;
  238. houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1);
  239. if (!custom.IsSuccess) goto CC;
  240. houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2);
  241. if (!custom.IsSuccess) goto CC;
  242. houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3);
  243. if (!custom.IsSuccess) goto CC;
  244. houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4);
  245. if (!custom.IsSuccess) goto CC;
  246. houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5);
  247. if (!custom.IsSuccess) goto CC;
  248. houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6);
  249. if (!custom.IsSuccess) goto CC;
  250. houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7);
  251. if (!custom.IsSuccess) goto CC;
  252. houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8);
  253. if (!custom.IsSuccess) goto CC;
  254. houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9);
  255. if (!custom.IsSuccess) goto CC;
  256. houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10);
  257. if (!custom.IsSuccess) goto CC;
  258. houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11);
  259. if (!custom.IsSuccess) goto CC;
  260. houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12);
  261. if (!custom.IsSuccess) goto CC;
  262. houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13);
  263. if (!custom.IsSuccess) goto CC;
  264. houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14);
  265. if (!custom.IsSuccess) goto CC;
  266. houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15);
  267. if (!custom.IsSuccess) goto CC;
  268. houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16);
  269. if (!custom.IsSuccess) goto CC;
  270. houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom);
  271. if (!custom.IsSuccess) goto CC;
  272. lock (lockObject)
  273. {
  274. HouseEEPROInfos.Add(houseEEPROInfo);
  275. }
  276. }
  277. CC:
  278. comBin.IsStop = true;
  279. var ClosePort = comBin.ClosePort();
  280. TLLogEvent?.Invoke($"关闭端口{portName}结果:{ClosePort}", LogEnum.RunRecord);
  281. }
  282. catch (Exception ex)
  283. {
  284. ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常{portName}", null, LogEnum.RunException);
  285. lock (lockObject)
  286. {
  287. errorlist.Add($"获取端口信息异常{portName}:{ex.Message}");
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// 读取仓室E方数据
  293. /// </summary>
  294. public List<string> HousesEEPRO()
  295. {
  296. try
  297. {
  298. int num = SerialModels.Count;
  299. List<Task> tasks = new List<Task>();
  300. foreach (var item in SerialModels)
  301. {
  302. if (item.houseSn == 11)
  303. {
  304. continue;
  305. }
  306. tasks.Add(Task.Run(() =>
  307. {
  308. HouseEEPRO(item);
  309. }));
  310. }
  311. Task.WaitAll(tasks.ToArray());
  312. return errorlist;
  313. }
  314. catch (Exception ex)
  315. {
  316. ExceptionLogEvent?.Invoke(ex, $"获取仓室E方信息", null, LogEnum.RunException);
  317. lock (lockObject)
  318. {
  319. errorlist.Add($"获取仓室E方信息异常:{ex.Message}");
  320. }
  321. return errorlist;
  322. }
  323. }
  324. private void HouseEEPRO(HouseInitData serialModel)
  325. {
  326. //1-16号水平电机位置
  327. //垂直电机间隔脉冲
  328. //E方清晰地址
  329. try
  330. {
  331. HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo();
  332. houseEEPROInfo.houseSn = serialModel.houseSn;
  333. var custom = new CustomProtocol();
  334. custom.commandSource = CommandSource.GetModuleThread;
  335. // M1-03 HAL: 向 HAL 借用唯一 ComBin(不再 new,杜绝与采集端同口重复 Open)。
  336. ComBin comBin = (ComBin)HardwareAccessLayer.Instance.GetSerial(serialModel.houseSn, serialModel.housePort).RawComBin;
  337. comBin.IsStopWhile = false;
  338. comBin.CommandLogEvent += HouseLogEvent;
  339. comBin.ErrorLogEvent += TLLogEvent;
  340. comBin.ExceptionLogEvent += ExceptionLogEvent;
  341. for (int i = 0; i < 3; i++)
  342. {
  343. if (i != 0) TLLogEvent?.Invoke($"第{i + 1}次关闭端口{serialModel.housePort}结果:{comBin.ClosePort()}", LogEnum.RunRecord);
  344. bool isOpenPort = comBin.OpenPort();
  345. HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]打开端口结果:{isOpenPort}", LogEnum.HouseInfo);
  346. if (!isOpenPort)
  347. {
  348. continue;
  349. }
  350. houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1);
  351. if (!custom.IsSuccess) continue;
  352. houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2);
  353. if (!custom.IsSuccess) continue;
  354. houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3);
  355. if (!custom.IsSuccess) continue;
  356. houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4);
  357. if (!custom.IsSuccess) continue;
  358. houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5);
  359. if (!custom.IsSuccess) continue;
  360. houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6);
  361. if (!custom.IsSuccess) continue;
  362. houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7);
  363. if (!custom.IsSuccess) continue;
  364. houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8);
  365. if (!custom.IsSuccess) continue;
  366. houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9);
  367. if (!custom.IsSuccess) continue;
  368. houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10);
  369. if (!custom.IsSuccess) continue;
  370. houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11);
  371. if (!custom.IsSuccess) continue;
  372. houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12);
  373. if (!custom.IsSuccess) continue;
  374. houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13);
  375. if (!custom.IsSuccess) continue;
  376. houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14);
  377. if (!custom.IsSuccess) continue;
  378. houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15);
  379. if (!custom.IsSuccess) continue;
  380. houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16);
  381. if (!custom.IsSuccess) continue;
  382. //houseEEPROInfo.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom);
  383. //if (!custom.IsSuccess) continue;
  384. houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom);
  385. if (!custom.IsSuccess) continue;
  386. lock (lockObject)
  387. {
  388. HouseEEPROInfos.Add(houseEEPROInfo);
  389. }
  390. break;
  391. }
  392. comBin.IsStop = true;
  393. var ClosePort = comBin.ClosePort();
  394. HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]关闭端口结果:{ClosePort}", LogEnum.HouseInfo);
  395. }
  396. catch (Exception ex)
  397. {
  398. ExceptionLogEvent?.Invoke(ex, $"[{serialModel.houseSn}][{serialModel.housePort}]获取仓室E方信息", null, LogEnum.RunException);
  399. lock (lockObject)
  400. {
  401. errorlist.Add($"[{serialModel.houseSn}][{serialModel.housePort}]获取仓室E方信息异常:{ex.Message}");
  402. }
  403. }
  404. }
  405. }
  406. }