using ivf_tl_Entity.CameraEntitys; using ivf_tl_Entity.DebugEntitys; using ivf_tl_Entity.GlobalEntitys; using ivf_tl_Entity.GlobalEnums; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ivf_tl_Entity.ComEntitys { /// /// 开机获取串口信息 /// public class SerialBin { public event Action TLLogEvent; public event Action ExceptionLogEvent; public event Action HouseLogEvent; public Dictionary CCDidSn = new Dictionary(); public List SerialModels = new List(); public List HouseEEPROInfos = new List(); private List errorlist = new List(); public int TLNum = -1; public int dayLighting = -1; private object lockObject = new object(); /// /// 获取相机对应的ccdid /// public List UpdataCamera() { try { CCDidSn.Clear(); Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { int ccdid = i; tasks[ccdid] = Task.Run(() => { GetCameraSn(ccdid); }); } Task.WaitAll(tasks); return errorlist; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, "获取ccdid异常", LogEnum.RunException); lock (lockObject) { errorlist.Add($"获取ccdid异常"); } return errorlist; } } private void GetCameraSn(int i) { try { int InitNumber = -1; int GetSnNumber = -1; string SnNumber = null; int UnInitNumber = -1; Camera Camera = new Camera(i, 100, 100, 100); for (int j = 0; j < 3; j++) { InitNumber = Camera.Init(); TLLogEvent?.Invoke($"第{j + 1}次{i}号相机初始化结果:{InitNumber}", LogEnum.RunRecord); if (InitNumber == 0) { GetSnNumber = Camera.GetNumbet(); SnNumber = Camera.NumBer; TLLogEvent?.Invoke($"获取序列号结果:{GetSnNumber};序列号:{SnNumber}", LogEnum.RunRecord); UnInitNumber = Camera.UnInit(); TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord); break; } else { UnInitNumber = Camera.UnInit(); TLLogEvent?.Invoke($"第{j + 1}次{i}号相机卸载结果:{UnInitNumber}", LogEnum.RunRecord); } } if (SnNumber == null) { lock (lockObject) { CCDidSn.Add(i, SnNumber); } } else { lock (lockObject) { if (CCDidSn.ContainsValue(SnNumber)) { errorlist.Add($"ccdid:{i};ccdsn:{SnNumber}"); } else { CCDidSn.Add(i, SnNumber); } } } } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, $"获取{i}号相机ccdSn异常", LogEnum.RunException); lock (lockObject) { errorlist.Add($"获取{i}号相机ccdSn异常"); } } } //List Houses = new List(); public List Start(string autoFcous) { try { string[] ArryPort = SerialPort.GetPortNames(); int portNum = ArryPort.Length; if (portNum > 0) { List listTask = new List(); for (int i = 0; i < portNum; i++) { string portName = ArryPort[i]; if (portName == "COM1" || portName == "COM2") { continue; } if (portName.Contains("COM")) { listTask.Add(Task.Run(() => { GetHouseInfo(portName, autoFcous); })); } } Task.WaitAll(listTask.ToArray()); } return errorlist; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常", LogEnum.RunException); lock (lockObject) { errorlist.Add($"获取端口信息异常:{ex.Message}"); } return errorlist; } } private void GetHouseInfo(string portName,string autoFocus) { try { //下加热板目标温度 //舱室和缓冲瓶进气阀打开时间 //ccdSN //ccdid HouseInitData serialModel = new HouseInitData(); serialModel.housePort = portName; var custom = new CustomProtocol(); ComBin comBin = new ComBin(0, portName); comBin.IsStopWhile = false; comBin.CommandLogEvent += HouseLogEvent; comBin.ErrorLogEvent += TLLogEvent; comBin.ExceptionLogEvent += ComBin_ExceptionLogEvent; bool isOpenPort = comBin.OpenPort(); TLLogEvent?.Invoke($"打开端口{portName}结果:{isOpenPort}", LogEnum.RunRecord); if (!isOpenPort) { errorlist.Add($"串口打开失败"); goto CC; } if(autoFocus == "1") { if (!comBin.AutoWait(custom, false)) { errorlist.Add($"{portName}关闭自动换气失败"); goto CC; } } serialModel.houseSn = comBin.ShakeHandsWait(custom); if (!custom.IsSuccess) { errorlist.Add($"{portName}握手失败"); goto CC; } lock (lockObject) { var itemModel = SerialModels.FirstOrDefault(x => x.houseSn == serialModel.houseSn); if (itemModel != null) { errorlist.Add($"从{serialModel.housePort}串口获取到的模块号{serialModel.houseSn}和{itemModel.housePort}串口获取到的模块号{itemModel.houseSn}重复"); goto CC; } } if (serialModel.houseSn == 11) { TLNum = comBin.ReadTLNumWait(custom); if (!custom.IsSuccess) goto CC; dayLighting = comBin.ReadEEPROMLightNumWait(custom); if (!custom.IsSuccess) goto CC; serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeBufferWait(custom); if (!custom.IsSuccess) goto CC; serialModel.ccdSn = "-1"; lock (lockObject) { SerialModels.Add(serialModel); } } else { serialModel.temperatureLowerHeatingPlate = comBin.ReadTargetTempWait(custom); if (!custom.IsSuccess) goto CC; serialModel.inletValveOpeningTime = comBin.ReadEEPROOpenIntakeTimeWait(custom); if (!custom.IsSuccess) goto CC; serialModel.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom); if (!custom.IsSuccess) goto CC; var currentCCDSN = comBin.GetCCDSNWait(custom).ToString(); if (!custom.IsSuccess) goto CC; lock (lockObject) { var itemModel = SerialModels.FirstOrDefault(x => x.ccdSn == currentCCDSN); if (itemModel == null) { if (CCDidSn.ContainsValue(currentCCDSN)) { serialModel.ccdId = CCDidSn.FirstOrDefault(x => x.Value == currentCCDSN).Key; } else { errorlist.Add($"相机列表中不存在舱室的CCDSN{currentCCDSN}"); } serialModel.ccdSn = currentCCDSN; SerialModels.Add(serialModel); } else { errorlist.Add($"从{serialModel.houseSn}号模块的{serialModel.housePort}串口获取到的{currentCCDSN}和{itemModel.ccdSn}模块{itemModel.housePort}串口的{itemModel.ccdSn}重复"); } } HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo(); houseEEPROInfo.houseSn = serialModel.houseSn; houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15); if (!custom.IsSuccess) goto CC; houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16); if (!custom.IsSuccess) goto CC; houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom); if (!custom.IsSuccess) goto CC; lock (lockObject) { HouseEEPROInfos.Add(houseEEPROInfo); } } CC: comBin.IsStop = true; var ClosePort = comBin.ClosePort(); TLLogEvent?.Invoke($"关闭端口{portName}结果:{ClosePort}", LogEnum.RunRecord); } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, $"获取端口信息异常{portName}", LogEnum.RunException); lock (lockObject) { errorlist.Add($"获取端口信息异常{portName}:{ex.Message}"); } } } /// /// 读取舱室E方数据 /// public List HousesEEPRO() { try { int num = SerialModels.Count; List tasks = new List(); foreach (var item in SerialModels) { if (item.houseSn == 11) { continue; } tasks.Add(Task.Run(() => { HouseEEPRO(item); })); } Task.WaitAll(tasks.ToArray()); return errorlist; } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, $"获取舱室E方信息", LogEnum.RunException); lock (lockObject) { errorlist.Add($"获取舱室E方信息异常:{ex.Message}"); } return errorlist; } } private void HouseEEPRO(HouseInitData serialModel) { //1-16号水平电机位置 //垂直电机间隔脉冲 //E方清晰地址 try { HouseEEPROInfo houseEEPROInfo = new HouseEEPROInfo(); houseEEPROInfo.houseSn = serialModel.houseSn; var custom = new CustomProtocol(); ComBin comBin = new ComBin(serialModel.houseSn, serialModel.housePort); comBin.IsStopWhile = false; comBin.CommandLogEvent += HouseLogEvent; comBin.ErrorLogEvent += TLLogEvent; comBin.ExceptionLogEvent += ComBin_ExceptionLogEvent; for (int i = 0; i < 3; i++) { if (i != 0) TLLogEvent?.Invoke($"第{i + 1}次关闭端口{serialModel.housePort}结果:{comBin.ClosePort()}", LogEnum.RunRecord); bool isOpenPort = comBin.OpenPort(); HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]打开端口结果:{isOpenPort}", LogEnum.HouseInfo); if (!isOpenPort) { continue; } houseEEPROInfo.hwell1 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 1); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell2 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 2); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell3 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 3); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell4 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 4); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell5 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 5); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell6 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 6); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell7 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 7); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell8 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 8); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell9 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 9); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell10 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 10); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell11 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 11); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell12 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 12); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell13 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 13); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell14 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 14); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell15 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 15); if (!custom.IsSuccess) continue; houseEEPROInfo.hwell16 = comBin.ReadEEPROMhoriMtWellHorHorWait(custom, 16); if (!custom.IsSuccess) continue; //houseEEPROInfo.verticalMotorSpacePulse = comBin.ReadEEPROMvertMtScanPluseWait(custom); //if (!custom.IsSuccess) continue; houseEEPROInfo.eepromClearPosition = comBin.ReadEEPROMvertMtStartPulseWait(custom); if (!custom.IsSuccess) continue; lock (lockObject) { HouseEEPROInfos.Add(houseEEPROInfo); } break; } comBin.IsStop = true; var ClosePort = comBin.ClosePort(); HouseLogEvent?.Invoke(serialModel.houseSn, DateTime.Now, $"[{serialModel.houseSn}][{serialModel.housePort}]关闭端口结果:{ClosePort}", LogEnum.HouseInfo); } catch (Exception ex) { ExceptionLogEvent?.Invoke(ex, $"[{serialModel.houseSn}][{serialModel.housePort}]获取舱室E方信息", LogEnum.RunException); lock (lockObject) { errorlist.Add($"[{serialModel.houseSn}][{serialModel.housePort}]获取舱室E方信息异常:{ex.Message}"); } } } private void ComBin_ExceptionLogEvent(Exception arg1, string arg2, string arg3, LogEnum arg4) { ExceptionLogEvent?.Invoke(arg1, $"{arg2}{arg3}", arg4); } } }