public void Close() { if (_hPort != IntPtr.Zero) { RS232.ClosePort(_hPort); _hPort = IntPtr.Zero; } }
public void Open() { _hPort = RS232.OpenPort(_portNum, new RS232Config { BaudRate = 115200, DataBits = 8, Parity = Parity.None, StopBits = StopBits.One, }); RS232.Purge(_hPort); }
private async Task TransmitLoopAsync(CancellationToken ct) { ct.ThrowIfCancellationRequested(); // transmit led on/off commands while (true) { // Send LED ON command [1] with 0 delimiter Console.WriteLine("tx: LED ON"); RS232.Write(_hPort, new byte[] { 1, 0 }); await Task.Delay(BlinkInterval / 2, ct).ConfigureAwait(false); // Send LED OFF command [2] with 0 delimiter Console.WriteLine("tx: LED OFF"); RS232.Write(_hPort, new byte[] { 2, 0 }); await Task.Delay(BlinkInterval / 2, ct).ConfigureAwait(false); } }
protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) } // TODO: free unmanaged resources (unmanaged objects) and override finalizer if (_hPort != IntPtr.Zero) { RS232.ClosePort(_hPort); _hPort = IntPtr.Zero; } // TODO: set large fields to null _disposedValue = true; } }
private async Task RecieveLoopAsync(CancellationToken ct) { ct.ThrowIfCancellationRequested(); var rxBuffer = new byte[RxBufferSize]; while (true) { await Task.Delay(PollInterval, ct).ConfigureAwait(false); var n = RS232.Read(_hPort, rxBuffer); if (n == 0) { continue; } var s = Encoding.ASCII.GetString(rxBuffer, 0, n); Console.WriteLine("rx: " + s); } }