/// <summary> /// /// </summary> /// <param name="sp">端口</param> /// <param name="endLine">是否需要加上结束符发送</param> public void Run(SerialPort sp, bool endLine) { try { Console.WriteLine("计时开始"); sp.WriteLine(SendStr + (endLine ? "\r\n" : "")); StartTime = DateTime.Now.Ticks; } catch (Exception E) { this.PortERROR1 = E.Message; return; } AtCommEventArgs e = new AtCommEventArgs(true); PortSendEvent?.Invoke(this, e); //添加超时机制 Task.Factory.StartNew(() => { Console.WriteLine("计时开始"); while (!cancleaction.IsCancellationRequested) { if ((DateTime.Now.Ticks - startTime) / 10000000.0 > timeOut) { Console.WriteLine("超时"); if (!this.CommState) { this.IsTimeOut = true; } cancleaction.Cancel(); Console.WriteLine("计时结束"); } } }); }
//激活发送事件 public void RaisePortSendEvent(bool sendFlag) { AtCommEventArgs e = new AtCommEventArgs(sendFlag); PortSendEvent?.Invoke(this, e); }