| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035 |
- 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()
- {
- return _channel.OpenPort();
- }
- 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方-->垂直电机清晰位置
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROMvertMtStartPulseWait(CustomProtocol custom)
- {
- custom.commandType = Enums.ReadEEPROMvertMtStartPulse;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.CreateReadEEPROMvertMtStartPulse(1);
- 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;
- }
- #endregion
- #region 调试模式
- #endregion
- }
- }
|