public void Shutdown() { sendQueue.ShutdownGracefully(); recvQueue.ShutdownGracefully(); socket.Shutdown(SocketShutdown.Send); SenderTask.Wait(); ReceiverTask.Wait(); }
/// <summary> /// Send frame bytes to serial, and wait respond. /// </summary> /// <param name="bytes">Bytes to sent</param> /// <exception cref="SerialDeviceException"> If timeout or exceed maximum number of retries.</exception> public Task SendFrame(SerialCommandFrame frame) { if (_senderTasks.Count > MaxNumQueuedTask) { throw new SerialDeviceException($"Too many frames ({_senderTasks.Count}) queued!"); } SenderTask task = new SenderTask(frame); if (!ValidFrameBytes(task.BytesToSend)) { throw new ArgumentException("Invalid frame bytes!"); } _senderTasks.Enqueue(task); _threadTrigger.Set(); return(task.AwaitSource.Task); }
public void Disconnect() { CancellationSource.Cancel(); if (SenderTask != null) { SenderTask.Wait(); SenderTask = null; } if (ReceiverTask != null) { ReceiverTask.Wait(); ReceiverTask = null; } if (Socket != null) { Socket.Close(); Socket = null; } }