//发送字节数据 private void sendBytesWithInfo(object obj) { while (sendThreaIsRunning) { while (!queueSendBuffer.IsEmpty) { BytesWithInfo bi = null; bool result = queueSendBuffer.TryDequeue(out bi); //Console.WriteLine("queue count : " + queueSendBuffer.Count); if (!result || null == bi) { continue; } try { serial.Write(bi.bytes, 0, bi.bytes.Length); eventSended?.Invoke(this, new ByteEventArgs(DateTime.Now, bi)); } catch (Exception e) { CommStateEventArgs args = new CommStateEventArgs(); args.opened = false; args.message = "发送异常, 请检查串口是否正常 : " + e.Message; eventStateChanged?.Invoke(this, args); } Thread.Sleep(5); } Thread.Sleep(10); } }
/// <summary> /// 发送字节数据, 开启线程发送 /// </summary> /// <param name="by"></param> public void sendMessageWithInfo(byte[] by, string info) { //Console.WriteLine("comm id:" + info); BytesWithInfo bi = new BytesWithInfo(by, info); queueSendBuffer.Enqueue(bi); //if (!sendThreaIsRunning) { // sendThreaIsRunning = true; // ThreadPool.QueueUserWorkItem(new WaitCallback(sendBytesWithInfo)); //} }
//发送字节数据 private void sendBytes(object obj) { BytesWithInfo bi = obj as BytesWithInfo; bi.bytes = obj as byte[]; try { serial.Write(bi.bytes, 0, bi.bytes.Length); eventSended?.Invoke(this, new ByteEventArgs(DateTime.Now, bi)); } catch (Exception e) { CommStateEventArgs args = new CommStateEventArgs(); args.opened = false; args.message = "发送异常, 请检查串口是否正常"; eventStateChanged?.Invoke(this, args); } }
public ByteEventArgs(DateTime time, BytesWithInfo bi) { this.time = time; this.bi = bi; }
public ByteEventArgs(BytesWithInfo bi) { this.bi = bi; }