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 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); } }