| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- using ivf_tl_Entity.GlobalEnums;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ivf_tl_Entity.ComEntitys
- {
- /// <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<string> AddMessageInfoEvent;
- 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>
- /// 开启发送指令线程
- /// </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)}]";
- if (customProtocol.commandType == Enums.VerticalMotorAbsolute)
- {
- AddMessageInfoEvent?.Invoke($"[{customProtocol.commandType}][发:{StringHelper.ByteToHexStr(customProtocol.sendBuffer)}][垂直电机位置:{customProtocol.VerticalMotorPulse}]");
- }
- else
- {
- AddMessageInfoEvent?.Invoke($"[{customProtocol.commandType}][发:{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(500);
- }
- }
- 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
- {
- bool sendRs = _channel.Send(customProtocol);
- bool result = sendAutoResetEvent.WaitOne(milliseconds);//延时等待下位机回复
- // M8-P3b:单条指令收发为高频,走调试级(落本地文件、默认不入库,见 14§4)。全 try 兜底。
- try
- {
- Aivfo.OperationLog.OperationLogger.Log("串口",
- $"指令-{customProtocol.commandType}",
- input: Content,
- result: result ? "成功" : "失败",
- level: Aivfo.OperationLog.OpLogLevel.Debug,
- houseSn: houseId);
- }
- catch { }
- return result;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, $"{Content}指令发送", null, LogEnum.RunException);
- return false;
- }
- }
- private void _channel_DataReceived(CustomProtocol obj)
- {
- AddMessageInfoEvent?.Invoke($"[{obj.commandType}][收:{StringHelper.ByteToHexStr(obj.receivedBuffer)}]");
- sendAutoResetEvent.Set();
- }
- /// <summary>
- /// 发送封装
- /// </summary>
- /// <param name="custom"></param>
- private void Enqueue(CustomProtocol custom)
- {
- lock (_commandQueue)
- {
- _commandQueue.Enqueue(custom);
- }
- }
- public bool OpenPort()
- {
- // M8-P3b:串口操作级埋点(打开端口入库)。全 try 兜底。
- bool rs = _channel.OpenPort();
- try
- {
- Aivfo.OperationLog.OperationLogger.Log("串口", "打开端口",
- input: new { houseId, portName },
- result: rs ? "成功" : "失败",
- houseSn: houseId);
- }
- catch { }
- return rs;
- }
- public bool ClosePort()
- {
- // M8-P3b:串口操作级埋点(关闭端口入库)。全 try 兜底。
- bool rs = _channel.ClosePort();
- try
- {
- Aivfo.OperationLog.OperationLogger.Log("串口", "关闭端口",
- input: new { houseId, portName },
- result: rs ? "成功" : "失败",
- houseSn: houseId);
- }
- catch { }
- return rs;
- }
- #region 舱室状态
- 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>
- 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);
- AddMessageInfoEvent?.Invoke($"等待{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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 水平电机正向运动
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- /// <param name="newValue"></param>
- /// <returns></returns>
- 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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 水平电机反向运动
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="waitTime"></param>
- /// <param name="newValue"></param>
- /// <returns></returns>
- public bool HorizontalMotorBackward(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);
- AddMessageInfoEvent?.Invoke($"等待{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);
- AddMessageInfoEvent?.Invoke($"等待{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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 垂直电机正向运动
- /// </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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- return custom.IsSuccess;
- }
- /// <summary>
- /// 垂直电机反向运动
- /// </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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- return custom.IsSuccess;
- }
- #endregion
- #region 控制
- /// <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 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);
- AddMessageInfoEvent?.Invoke($"等待{waitTime}毫秒");
- }
- 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);
- AddMessageInfoEvent?.Invoke($"等待{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);
- AddMessageInfoEvent?.Invoke($"等待{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);
- AddMessageInfoEvent?.Invoke($"等待{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>
- /// <param name="custom"></param>
- /// <param name="newValue"></param>
- 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();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <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>
- /// <param name="custom"></param>
- /// <param name="newValue"></param>
- public void WriteEEPROMvertMtScanPluseWait(CustomProtocol custom, int newValue)
- {
- custom.commandType = Enums.WriteEEPROOpenIntakeTime;
- custom.logDateTime = DateTime.Now;
- custom.commandNumber = commandNumber++;
- custom.IsWaitOne = true;
- custom.CreateWriteEEPROMvertMtScanPluse(newValue);
- Enqueue(custom);
- taskAutoResetEvent.WaitOne();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <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>
- /// <param name="custom"></param>
- /// <param name="newValue"></param>
- 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();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <summary>
- /// 写E方-->舱室排气阀打开时间
- /// </summary>
- /// <param name="custom"></param>
- /// <param name="newValue"></param>
- 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();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <summary>
- /// 读E方-->舱室排气阀打开时间
- /// </summary>
- /// <returns></returns>
- 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方-->缓冲瓶进气阀打开时间
- /// </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 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();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <summary>
- /// 写E方-->灯光亮度
- /// </summary>
- /// <returns></returns>
- 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();
- if (!custom.IsSuccess) return;
- return;
- }
- /// <summary>
- /// 读E方-->灯光亮度
- /// </summary>
- /// <returns></returns>
- public int ReadEEPROMLightNumWait(CustomProtocol custom)
- {
- custom.commandType = Enums.LightNum;
- 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);
- }
- /// <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);
- }
- #endregion
- #region 调试模式
- #endregion
- }
- }
|