Exemplo n.º 1
0
        public async Task Send(BaseDirective item, int reSendTimes = 0)
        {
            try
            {
                _waitForFeedbackDirectives.TryAdd(item.DirectiveId, new WaitForFeedBack(DateTime.Now, item, reSendTimes));

                var directiveData = _protocolProvider.GenerateDirectiveBuffer(item);
                //断线重连
                if (_serialPort.Status == SerialPortStatus.Initialled)
                {
                    await _serialPort.Open(SerialEnum.LowerComputer);
                }

                if (_waitForFeedbackDirectives.ContainsKey(item.DirectiveId) && _serialPort.Status == SerialPortStatus.Opened)
                {
                    _serialPort.Send(directiveData, _cancelTokenSource.Token);
                }
            }
            catch (CustomException)
            {
                LogFactory.Create().Info("send error");
            }
            catch (TaskCanceledException)
            {
                LogFactory.Create().Info("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 开始测试所有通道
        /// </summary>
        /// <returns></returns>
        public bool Start(string channelNumber = null)
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();//打开串口
                }
                LogHelper.WriteInfoLog(string.Format("开始测试所有通道"));
                bool ret = command.Start(channelNumber); //寻找和组合start指令,在command.cs文件中//当系统下发了启动指令后

                if (ret)                                 //启动指令发送成功的时候,启动readThread线程,监控串口传来的数据
                {
                    LogHelper.WriteInfoLog("启动指令发送成功,启动readThread线程,监控串口传来的数据");
                    readThread.Start();
                }
                return(ret);

                // return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                LogHelper.WriteInfoLog("开始测试所有通道失败:" + ex.Message);
                return(false);
            }
        }
Exemplo n.º 3
0
        public async Task SendData(string msg)
        {
            var x = Encoding.UTF8.GetBytes(msg);
            var p = new CancellationTokenSource();

            // 第一次加载
            if (_serial.Status == SerialPortStatus.Initialled)
            {
                await _serial.Open(SerialEnum.Sim);
            }

            if (_serial.Status == SerialPortStatus.Opened)
            {
                _serial.Send(x.Concat(new byte[] { 0x0D, 0x0A }).ToArray(), p.Token);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 开始从硬件查询数据
        /// </summary>
        public void Start()
        {
            try
            {
                if (!reading)
                {
                    readThread = new Thread(ReadData);
                    serial.Open();

                    readThread.Start();//启动一个线程监控串口传来的数据
                    reading = true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                LogHelper.WriteInfoLog("开始从硬件查询数据:" + ex.Message);
                MessageBoxWindow.ShowDialog("操作失败");
            }
        }