| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- 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
- {
- /// <summary>
- /// 开机获取串口信息
- /// </summary>
- public class SerialBin
- {
- public event Action<string, LogEnum> TLLogEvent;
- public event Action<Exception, string, LogEnum> ExceptionLogEvent;
- public event Action<int, DateTime, string, LogEnum> HouseLogEvent;
- public Dictionary<int, string> CCDidSn = new Dictionary<int, string>();
- public List<HouseInitData> SerialModels = new List<HouseInitData>();
- public List<HouseEEPROInfo> HouseEEPROInfos = new List<HouseEEPROInfo>();
- private List<string> errorlist = new List<string>();
- public int TLNum = -1;
- public int dayLighting = -1;
- private object lockObject = new object();
- /// <summary>
- /// 获取相机对应的ccdid
- /// </summary>
- public List<string> 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<house> Houses = new List<house>();
- public List<string> Start(string autoFcous)
- {
- try
- {
- string[] ArryPort = SerialPort.GetPortNames();
- int portNum = ArryPort.Length;
- if (portNum > 0)
- {
- List<Task> listTask = new List<Task>();
- 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}");
- }
- }
- }
- /// <summary>
- /// 读取舱室E方数据
- /// </summary>
- public List<string> HousesEEPRO()
- {
- try
- {
- int num = SerialModels.Count;
- List<Task> tasks = new List<Task>();
- 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);
- }
- }
- }
|