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); } }
public async Task SendData(string msg) { var x = Encoding.UTF8.GetBytes(msg); var p = new CancellationTokenSource(); if (serialPort == null) { serialPort = await SerialCreater.Instance.Create(SerialEnum.Sim); if (serialPort == null) { Debug.WriteLine("sim is null"); return; } serialPort.ReceiveHandler += SerialPort_ReceiveHandler; } if (serialPort != null) { await serialPort.Open(); await serialPort.Send(x.Concat(new byte[] { 0x0D, 0x0A }).ToArray(), p.Token); } }