Пример #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);
            }
        }
        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 == null)
                {
                    serialPort = await SerialCreater.Instance.Create(SerialEnum.LowerComputer);

                    if (serialPort != null)
                    {
                        serialPort.ReceiveHandler += SpHelper_ReceiveHandler;
                    }
                    else
                    {
                        Debug.WriteLine("LowerComputer is null");
                    }
                }

                if (serialPort != null)
                {
                    await serialPort.Open();

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId))
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException)
            {
                Debug.WriteLine("send error");
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
Пример #3
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 == null)
                {
                    serialPort = await getSerialPort();
                }

                if (serialPort != null)
                {
                    // 运行过程中串口断开了
                    if (serialPort.serialPort == null)
                    {
                        serialPort.ReceiveHandler -= SpHelper_ReceiveHandler;
                        serialPort = await getSerialPort();
                    }

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId) && serialPort != null)
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException e)
            {
                Debug.WriteLine("send error->" + e.Message);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }