Beispiel #1
0
 public void Close()
 {
     if (_hPort != IntPtr.Zero)
     {
         RS232.ClosePort(_hPort);
         _hPort = IntPtr.Zero;
     }
 }
Beispiel #2
0
        public void Open()
        {
            _hPort = RS232.OpenPort(_portNum, new RS232Config
            {
                BaudRate = 115200,
                DataBits = 8,
                Parity   = Parity.None,
                StopBits = StopBits.One,
            });

            RS232.Purge(_hPort);
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        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;
            }
        }
Beispiel #5
0
        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);
            }
        }