| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- using IvfTl.Control.Entity;
- using IvfTl.Control.Entity.GlobalEnums;
- using ivf_tl_UtilHelper;
- using System.Diagnostics;
- namespace ivf_tl_SerialHelper.Util
- {
- /// <summary>
- /// 封装指令发送以及解析线程
- /// </summary>
- public class ComBin
- {
- /// <summary>
- /// 异常日志
- /// 异常、名称、参数
- /// </summary>
- public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
- /// <summary>
- /// 错误日志
- /// </summary>
- public event Action<string, LogEnum> ErrorLogEvent;
- /// <summary>
- /// 指令记录日志
- /// </summary>
- public event Action<int, DateTime, string, LogEnum> CommandLogEvent;
- /// <summary>
- /// 串口状态
- /// </summary>
- public event Action<int> ComStateEvent;
- public ComBin(int houseId, string portName)
- {
- this.houseId = houseId;
- this.portName = portName;
- _commandQueue = new Queue<CustomProtocol>();
- _channel = new Channel(houseId, portName);
- _channel.DataReceived += _channel_DataReceived;
- _channel.ExceptionLogEvent += _channel_ExceptionLogEvent;
- _channel.ErrorLogEvent += _channel_ErrorLogEvent;
- _channel.CommandLogEvent += _channel_CommandLogEvent; ;
- StartSendCommandThread();
- }
- private void _channel_CommandLogEvent(int arg1, DateTime arg2, string arg3, LogEnum arg4)
- {
- CommandLogEvent?.Invoke(arg1, arg2, arg3, arg4);
- }
- private void _channel_ErrorLogEvent(string arg1, LogEnum arg2)
- {
- ErrorLogEvent?.Invoke(arg1, arg2);
- }
- private void _channel_ExceptionLogEvent(Exception arg1, string arg2, string arg3, LogEnum arg4)
- {
- ExceptionLogEvent?.Invoke(arg1, arg2, arg3, arg4);
- }
- public Channel _channel { get; set; } = null;
- /// <summary>
- /// 是否停止发送指令线程循环
- /// </summary>
- public bool IsStop = false;
- /// <summary>
- /// 指令发送失败以后是否无限重发
- /// </summary>
- public bool IsStopWhile = true;
- private int houseId = 0;
- private string portName = null;
- /// <summary>
- /// 命令队列
- /// </summary>
- private Queue<CustomProtocol> _commandQueue;
- /// <summary>
- /// 指令超时重发等待毫秒
- /// </summary>
- private int milliseconds = 1000 * 30;
- /// <summary>
- /// 发送指令的线程同步控制器
- /// </summary>
- private AutoResetEvent sendAutoResetEvent = new AutoResetEvent(false);
- /// <summary>
- /// 运行线程等待指令的同步控制器
- /// </summary>
- private AutoResetEvent taskAutoResetEvent = new AutoResetEvent(false);
- /// <summary>
- /// 指令发送锁
- /// </summary>
- private object SendCommandLock = new object();
- /// <summary>
- /// 指令计数
- /// </summary>
- private double commandNumber = 1;
- /// <summary>
- /// 串口状态 0正常 1异常
- /// </summary>
- private int ComState = -1;
- /// <summary>
- /// 开启发送指令线程
- /// </summary>
- public void StartSendCommandThread()
- {
- Task.Factory.StartNew(() =>
- {
- Stopwatch SendStopWatch = new Stopwatch();
- CustomProtocol customProtocol = null;
- while (true)
- {
- try
- {
- if (IsStop)
- {
- return;
- }
- lock (_commandQueue)
- {
- if (_commandQueue.Any())
- {
- customProtocol = _commandQueue.Dequeue();
- }
- }
- if (customProtocol != null)
- {
- SendStopWatch.Restart();
- string Content = $"[{houseId}][{portName}][{customProtocol.commandNumber}][{customProtocol.commandType}][Send:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}]";
- customProtocol.IsSuccess = SendCommand(customProtocol, Content);
- string Content11 = $"{Content}[Rec:{StringHelper.ByteToHexStr(customProtocol.receivedBuffer)}]";
- if (customProtocol.WaitTime != 0)
- {
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[等待{customProtocol.WaitTime}毫秒]", LogEnum.HouseComRecord);
- Thread.Sleep(customProtocol.WaitTime);
- }
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content11}[本轮指令总耗时:{StringHelper.TimeToString(SendStopWatch.Elapsed)}][End]", LogEnum.HouseComRecord);
- SendStopWatch.Stop();
- if (customProtocol.IsWaitOne)
- {
- taskAutoResetEvent.Set();
- }
- customProtocol = null;
- }
- else
- {
- //CommandLogEvent?.Invoke(houseId, DateTime.Now, $"空指令,等待50毫秒", LogEnum.HouseComRecord);
- Thread.Sleep(50);
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, $"[{houseId}][{portName}]指令发送线程", null, LogEnum.RunException);
- }
- }
- }, TaskCreationOptions.LongRunning);
- }
- /// <summary>
- /// 指令发送
- /// </summary>
- /// <param name="customProtocol"></param>
- /// <param name="Content"></param>
- private bool SendCommand(CustomProtocol customProtocol, string Content)
- {
- try
- {
- //超时重发3次后表示通讯中断
- var result = false;
- bool isopen = true;
- bool sendRs = false;
- do
- {
- for (int i = 0; i < 3; i++)
- {
- if (isopen)
- {
- sendRs = _channel.Send(customProtocol);
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次发送结果:{sendRs}]", LogEnum.HouseComRecord);
- }
- if (sendRs)
- {
- result = sendAutoResetEvent.WaitOne(milliseconds);//延时等待下位机回复
- if (result)
- {
- break;
- }
- else
- {
- sendRs = false;
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}第{i + 1}次等待超时", LogEnum.HouseComRecord);
- ErrorLogEvent?.Invoke($"{Content}第{i + 1}次等待超时", LogEnum.RunError);
- if (i != 2)
- {
- isopen = _channel.ReopenPort();
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次重新打开串口结果:{isopen}]", LogEnum.HouseComRecord);
- if (!isopen)
- {
- ErrorLogEvent?.Invoke($"{Content}第{i + 1}次重新打开串口失败", LogEnum.RunError);
- }
- }
- }
- }
- else
- {
- ErrorLogEvent?.Invoke($"{Content}第{i + 1}次发送失败", LogEnum.RunError);
- if (i != 2)
- {
- Thread.Sleep(milliseconds);
- isopen = _channel.ReopenPort();
- CommandLogEvent?.Invoke(houseId, DateTime.Now, $"{Content}[第{i + 1}次重新打开串口结果:{isopen}]", LogEnum.HouseComRecord);
- if (!isopen)
- {
- ErrorLogEvent?.Invoke($"{Content}第{i + 1}次重新打开串口失败", LogEnum.RunError);
- }
- }
- }
- }
- //ErrorLogEvent?.Invoke($"{result}、{IsStopWhile}", LogEnum.RunError);
- if (!result && !IsStopWhile)
- {
- return false;
- }
- ComlossAlarm(result);
- } while (!result);
- return true;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, $"{Content}指令发送", null, LogEnum.RunException);
- return false;
- }
- }
- private void _channel_DataReceived(CustomProtocol obj)
- {
- sendAutoResetEvent.Set();
- }
- /// <summary>
- /// 发送封装
- /// </summary>
- /// <param name="custom"></param>
- private void Enqueue(CustomProtocol custom)
- {
- lock (_commandQueue)
- {
- _commandQueue.Enqueue(custom);
- }
- }
- public bool OpenPort()
- {
- bool ok = _channel.OpenPort();
- // 借用复用死锁修复(D1-08):HAL.ScanDevices 扫描每口后经 SerialChannelImpl.Close() 置 IsStop=true,
- // 令发送线程 while(IsStop)return 永久退出;采集端 serialBin.Start() 随后借用同一缓存 ComBin、
- // 直接调本方法重开端口(返回 True)再 ShakeHandsWait 入队握手指令,但发送线程已死、无人出队处理,
- // ShakeHandsWait 的 taskAutoResetEvent.WaitOne()(无超时)→ 永久死锁(旧合并 operate 僵尸即卡此)。
- // 故:重开端口成功且发送线程处于停止态时,复位 IsStop 并重启发送线程(SendCommandLock 防并发双起)。
- if (ok)
- {
- lock (SendCommandLock)
- {
- if (IsStop)
- {
- IsStop = false;
- StartSendCommandThread();
- }
- }
- }
- return ok;
- }
- public bool ClosePort()
- {
- return _channel.ClosePort();
- }
- /// <summary>
- /// 通讯报警
- /// </summary>
- /// <param name="result"></param>
- public void ComlossAlarm(bool result)
- {
- if(!result)
- {
- if(ComState != 1)
- {
- ComState = 1;
- ComStateEvent?.Invoke(1);
- }
- return;
- }
- if (ComState == 0) return;
- ComState = 0;
- ComStateEvent?.Invoke(0);
- }
- #region 仓室状态
- /// <summary>
- /// 握手指令
- /// </summary>
- public int ShakeHandsWait(CustomProtocol custom)
- {
- //握手
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.commandType = Enums.ShakeHands;
- custom.CreateHandCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseShakeHandsCommand(custom.receivedBuffer);
- }
- /// <summary>
- /// 读仓门状态
- /// </summary>
- /// <param name="custom"></param>
- /// <returns></returns>
- public State DoorStatusWait(CustomProtocol custom)
- {
- custom.commandType = Enums.DoorStatus;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadDoorCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return State.未知;
- return Analysiser.ParseDoorStatus(custom.receivedBuffer);
- }
- /// <summary>
- /// 仓室温度
- /// </summary>
- /// <param name="custom"></param>
- /// <returns></returns>
- public decimal TemperatureWait(CustomProtocol custom)
- {
- custom.commandType = Enums.Temperature;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadTemperatureCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1m;
- return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
- }
- /// <summary>
- /// 上盖板温度
- /// </summary>
- /// <param name="custom"></param>
- /// <returns></returns>
- public decimal ShangTemperatureWait(CustomProtocol custom)
- {
- custom.commandType = Enums.Temperature;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadShangTemperatureCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1m;
- return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
- }
- /// <summary>
- /// 玻璃片下方温度
- /// </summary>
- /// <param name="custom"></param>
- /// <returns></returns>
- public decimal BoLiTemperatureWait(CustomProtocol custom)
- {
- custom.commandType = Enums.BoLiTemperature;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadBoLiTemperatureCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1m;
- return Analysiser.ParseTemperatureCommand(custom.receivedBuffer);
- }
- /// <summary>
- /// 仓室压力
- /// </summary>
- /// <param name="custom"></param>
- /// <returns></returns>
- public decimal PressureWait(CustomProtocol custom)
- {
- custom.commandType = Enums.Pressure;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadPressureCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1m;
- return Analysiser.ParsePressureCommand(custom.receivedBuffer);
- }
- /// <summary>
- /// 获取缓冲瓶状态
- /// </summary>
- /// <returns></returns>
- public (decimal, decimal, decimal) BufferBottleState(CustomProtocol custom)
- {
- custom.commandType = Enums.BufferBottlePressure;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateBufferBottlePressureCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return (-1m,-1m,-1m);
- return (Analysiser.ParsePressureCommand(custom.receivedBuffer), Analysiser.ParseTLTemperature1Command(custom.receivedBuffer), Analysiser.ParseTLTemperature2Command(custom.receivedBuffer));
- }
- #endregion
- #region 电机
- /// <summary>
- /// 水平电机复位
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- public bool HorizontalMotorResetWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.HorizontalMotorReset;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateHorizontalMotorResetCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 水平电机绝对运动
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- /// <param name="zero"></param>
- /// <param name="newValue"></param>
- public bool HorizontalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int newValue)
- {
- custom.commandType = Enums.HorizontalMotorAbsolute;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.HorizontalMotorPulse = newValue;
- custom.CreateHorizontalMotorMoveToCommand(newValue);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 水平电机正向运动(相对)
- /// 忠实搬运 operate 侧 ComBin.HorizontalMotorForwardWait(命令构造器 control 侧已存在)。
- /// </summary>
- public bool HorizontalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
- {
- custom.commandType = Enums.HorizontalMotorForward;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.HorizontalMotorPulse = newValue;
- custom.CreateHorizontalMotorForwardCommand(newValue);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 水平电机反向运动(相对)
- /// 忠实搬运 operate 侧 ComBin.HorizontalMotorBackward(命令构造器 control 侧已存在)。
- /// </summary>
- public bool HorizontalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
- {
- custom.commandType = Enums.HorizontalMotorBackward;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.HorizontalMotorPulse = newValue;
- custom.CreateHorizontalMotorBackwardCommand(newValue);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 读水平电机位置
- /// </summary>
- public int ReadHorizontalMotorWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadHorizontalMotor;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadHorizontalMotorCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
- }
- /// <summary>
- /// 读垂直电机位置
- /// </summary>
- public int ReadVerticalMotorWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadVerticalMotor;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadVerticalMotorCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseCurrentMotor(custom.receivedBuffer);
- }
- /// <summary>
- /// 垂直电机复位
- /// </summary>
- public bool VerticalMotorResetWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.VerticalMotorReset;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateVerticalMotorResetCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 垂直电机绝对运动
- /// </summary>
- /// <param name="custom">指令对象</param>
- /// <param name="waitTime">电机运动等待时间</param>
- /// <param name="currentVer">垂直电机目标位置</param>
- /// <param name="currentHor">当前水平电机位置</param>
- /// <param name="pictureId">图片编号</param>
- /// <param name="well">当前well</param>
- /// <param name="focal">当前层数</param>
- public bool VerticalMotorAbsoluteWait(CustomProtocol custom, int waitTime, int currentVer, int currentHor, int pictureId, int well, int focal)
- {
- custom.commandType = Enums.VerticalMotorAbsolute;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.VerticalMotorPulse = currentVer;
- custom.HorizontalMotorPulse = currentHor;
- custom.pictureId = pictureId;
- custom.well = well;
- custom.focal = focal;
- custom.CreateVerticalMotorAbsoluteMovementCommand(currentVer);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 垂直电机正向运动(相对)
- /// 忠实搬运 operate 侧 ComBin.VerticalMotorForwardWait(命令构造器 control 侧已存在)。
- /// </summary>
- public bool VerticalMotorForwardWait(CustomProtocol custom, int waitTime, int newValue)
- {
- custom.commandType = Enums.VerticalMotorForward;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateVerticalMotorForwardCommand(newValue);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 垂直电机反向运动(相对)
- /// 忠实搬运 operate 侧 ComBin.VerticalMotorBackwardWait(命令构造器 control 侧已存在)。
- /// </summary>
- public bool VerticalMotorBackwardWait(CustomProtocol custom, int waitTime, int newValue)
- {
- custom.commandType = Enums.VerticalMotorBackward;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateVerticalMotorBackwardCommand(newValue);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- #endregion
- #region 控制
- /// <summary>
- /// 自动换气开关
- /// 忠实搬运 operate 侧 ComBin.AutoWait(命令构造器/枚举 control 侧本任务已补)。
- /// </summary>
- public bool AutoWait(CustomProtocol custom, bool auto)
- {
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.commandType = Enums.AutoAirSwap;
- custom.CreateAutoCommand(auto);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return false;
- return custom.receivedBuffer[custom.lenght - 2] == 0;
- }
- /// <summary>
- /// 仓室补气
- /// </summary>
- /// <param name="custom"></param>
- public bool HouseAerationWait(CustomProtocol custom)
- {
- custom.commandType = Enums.HouseAeration;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateHouseAerationCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return custom.IsSuccess;
- }
- /// <summary>
- /// 仓室排气
- /// </summary>
- /// <param name="custom"></param>
- public bool HouseVentWait(CustomProtocol custom)
- {
- custom.commandType = Enums.HouseVent;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateHouseVentCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return custom.IsSuccess;
- }
- /// <summary>
- /// 缓冲瓶补气
- /// </summary>
- /// <param name="custom"></param>
- public bool BufferBottleAerationWait(CustomProtocol custom)
- {
- custom.commandType = Enums.BufferBottleAeration;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateBufferBottleAerationCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return custom.IsSuccess;
- }
- /// <summary>
- /// 关闭仓室进气阀
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- public bool CloseIntakeValveWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.CloseIntakeValve;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateCloseIntakeValveCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 打开仓室排气阀
- /// </summary>
- /// <param name="custom"></param>
- public bool OpenExhaustValveWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.OpenExhaustValve;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateOpenExhaustValveCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 打开舱室进气阀
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- /// <returns></returns>
- public bool OpenIntakeValveWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.OpenIntakeValve;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateOpenIntakeValveCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 关闭排气阀
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- public void CloseExhaustValveWait(CustomProtocol custom, int waitTime)
- {
- custom.commandType = Enums.CloseExhaustValve;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateCloseExhaustValveCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (waitTime > 0)
- {
- Thread.Sleep(waitTime);
- }
- }
- /// <summary>
- /// 打开LEd灯
- /// </summary>
- /// <param name="custom"></param>
- public void OpenLEDWait(CustomProtocol custom)
- {
- custom.commandType = Enums.OpenLED;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateOpenLEDCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- }
- /// <summary>
- /// 关闭Led灯
- /// </summary>
- /// <param name="custom"></param>
- public void CloseLEDWait(CustomProtocol custom)
- {
- custom.commandType = Enums.CloseLED;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateCloseLEDCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- }
- #endregion
- #region 参数
- /// <summary>
- /// 读取TL仪器编号
- /// </summary>
- public int ReadTLNumWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadTLNum;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadTLNumCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 获取CCDSN
- /// </summary>
- public int GetCCDSNWait(CustomProtocol custom)
- {
- custom.commandType = Enums.GetCCDSN;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateGetModuleCommand();
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->水平电机位置
- /// </summary>
- public int ReadEEPROMhoriMtWellHorHorWait(CustomProtocol custom,int horIndex)
- {
- custom.commandType = Enums.ReadEEPROMhoriMtWellHor;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadEEPROMhoriMtWellHoriPos(horIndex);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->垂直电机清晰位置(Z对焦零点)。well=1..16 选该 well 的 EEPROM 槽(地址 0x08+4*(well-1));
- /// 默认 well=1 保持舱级单值老调用(SerialBin eepromClearPosition)行为不变。
- /// M-06:builder 早已支持 case 1-16,此前硬编码 (1) 致 ReadWellFocusZeroWait 忽略 well 恒读 well-1;
- /// 真机实证各 well 焦点零点 EEPROM 真分槽且不同(舱8/舱9 raw 抓包),改为按 well 读。
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom, int well = 1)
- {
- custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadEEPROMvertMtStartPulse(well);
- custom.IsWaitOne = true;
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->垂直电机间隔脉冲
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROMvertMtScanPluseWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadEEPROMvertMtScanPluse;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateReadEEPROMvertMtScanPluse();
- Enqueue( custom);//断层扫描间隔数
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->仓室进气阀打开时间
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROOpenIntakeTimeWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadEEPROOpenIntakeTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateReadEEPROOpenIntakeTimeCommand();
- Enqueue( custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->缓冲瓶进气阀打开时间
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROOpenIntakeTimeBufferWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadEEPROOpenIntakeTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateReadEEPROOpenIntakeTimeBufferCommand();
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>
- /// 读E方-->下加热板目标温度
- /// </summary>
- /// <returns></returns>
- public decimal ReadTargetTempWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadTargetTemp;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadTargetTemp();
- custom.IsWaitOne = true;
- Enqueue( custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1m;
- var num = Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- return Math.Round(num / 100.00m, 2, MidpointRounding.AwayFromZero);
- }
- /// <summary>
- /// 读E方-->灯光亮度
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROMLightNumWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadLightNum;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateReadEEPROMLightNum();
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- // ── M1-B2 写E方调试动作包装(operate 调试页接入 lease.Serial 走本类,杜绝调试页 new ComBin/开第二个物理口)──
- // 字节级一致性:T1.1 子代理已逐字节核实下列 4 个 Create* builder 与 operate 端完全相同
- // (命令码 0x12 + 相同 well 地址表/固定地址 + 小端 pulse + 同 CreateORC 校验),故发送字节安全。
- // ⚠ 待真机(V-010):写E方为破坏性操作;control 端 CustomProtocolLength 对 0x12 期望回包长度与 operate 不同,
- // "写成功判定/回包等待"语义需真机核对(不影响发出的命令字节)。WriteEEPROOpenVentTime/WriteLightNum
- // 因 control Commander 缺对应 builder,未在此补——调试页对应动作保留旧路径并标注待真机,不臆造字节。
- /// <summary>写E方→第 wellSn(1-16) 水平电机位置脉冲(命令 0x12,builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROMhoriMtWellHorHorWait(CustomProtocol custom, int wellSn, int newValue)
- {
- custom.commandType = Enums.WriteEEPROMhoriMtWell1HoriPos;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROMhoriMtWellHoriPos(wellSn, newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- /// <summary>写E方→垂直电机扫描间隔脉冲(builder 与 operate 逐字节一致)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROMvertMtScanPluseWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteEEPROMvertMtScanPluse;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROMvertMtScanPluse(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- /// <summary>写E方→舱室进气阀打开时间(builder 与 operate 逐字节一致,地址 00 03 0c)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROOpenIntakeTimeWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteEEPROOpenIntakeTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROOpenIntakeTimeCommand(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- /// <summary>写E方→缓冲瓶进气阀打开时间(builder 与 operate 逐字节一致,地址 00 05 0c)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROOpenIntakeTimeBufferWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteEEPROOpenIntakeTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROOpenIntakeTimeBufferCommand(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- // ── M-01/M-02/M-03 补齐:排气阀时间写/读、灯光亮度写(builder 与 operate 逐字节一致)──
- /// <summary>写E方→舱室排气阀打开时间(M-01,builder 地址 00 03 08,与 operate 一致)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROOpenVentTimeWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteEEPROOpenVentTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROOpenVentTimeCommand(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- /// <summary>读E方→舱室排气阀打开时间(M-02,builder 地址 00 03 08)。失败返回 -1。</summary>
- public int ReadEEPROMVentWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadEEPROOpenVentTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateReadEEPROMVentNum();
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return -1;
- return Analysiser.ParseEEPROMPulse(custom.receivedBuffer);
- }
- /// <summary>写E方→灯光亮度(M-03,builder 地址 00 05 34,与 operate 一致)。⚠ 待真机 V-010。</summary>
- public void WriteEEPROMLightNumWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteLightNum;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROMLightNum(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- return;
- }
- #endregion
- #region 调试模式
- #endregion
- }
- }
|