protected override void Dispose(bool disposing)
 {
     try {
         if (disposing)
         {
             _port.Close();
         }
     } finally {
         base.Dispose(disposing);
     }
 }
Exemple #2
0
        public CloseResult Close()
        {
            try
            {
                _parser.CommandComplete -= CommandComplete;
                _port.DataReceived      -= DataReceived;

                _worker.DoWork             -= SendCommandAsync;
                _worker.RunWorkerCompleted -= SendCommandAsyncComplete;

                _port.Close();
                _commandQueue.Clear();

                return(CloseResult.Closed);
            }
            catch (InvalidOperationException exception)
            {
                Log.Warn("Cannot close port.", exception);
                return(CloseResult.PortIsNotOpen);
            }
        }
Exemple #3
0
        private void DoWork(object tokenObject)
        {
            var cancellationToken         = (CancellationToken)tokenObject;
            ISerialPortWrapper serialPort = null;

            if (String.IsNullOrEmpty(UserSettings.ComPort))
            {
                _log.Warn("Cannot start the serial sending because the comport is not selected.");
                return;
            }

            frameCounter      = 0;
            blackFrameCounter = 0;

            //retry after exceptions...
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    const int baudRate      = 1000000;
                    string    openedComPort = null;

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        //open or change the serial port
                        if (openedComPort != UserSettings.ComPort)
                        {
                            serialPort?.Close();

                            serialPort = UserSettings.ComPort != "Fake Port"
                                ? (ISerialPortWrapper) new WrappedSerialPort(new SerialPort(UserSettings.ComPort, baudRate))
                                : new FakeSerialPort();

                            try
                            {
                                serialPort.Open();
                            }
                            catch
                            {
                                // useless UnauthorizedAccessException
                            }

                            if (!serialPort.IsOpen)
                            {
                                serialPort = null;

                                //allow the system some time to recover
                                Thread.Sleep(500);
                                continue;
                            }
                            openedComPort = UserSettings.ComPort;
                        }

                        //send frame data
                        var(outputBuffer, streamLength) = GetOutputStream();
                        serialPort.Write(outputBuffer, 0, streamLength);

                        if (++frameCounter == 1024 && blackFrameCounter > 1000)
                        {
                            //there is maybe something wrong here because most frames where black. report it once per run only
                            var settingsJson = JsonConvert.SerializeObject(UserSettings, Formatting.None);
                            _log.Info($"Sent {frameCounter} frames already. {blackFrameCounter} were completely black. Settings= {settingsJson}");
                        }
                        ArrayPool <byte> .Shared.Return(outputBuffer);

                        //ws2812b LEDs need 30 µs = 0.030 ms for each led to set its color so there is a lower minimum to the allowed refresh rate
                        //receiving over serial takes it time as well and the arduino does both tasks in sequence
                        //+1 ms extra safe zone
                        var fastLedTime        = (streamLength - _messagePreamble.Length - _messagePostamble.Length) / 3.0 * 0.030d;
                        var serialTransferTime = streamLength * 10.0 * 1000.0 / baudRate;
                        var minTimespan        = (int)(fastLedTime + serialTransferTime) + 1;

                        Thread.Sleep(minTimespan);
                    }
                }
                catch (OperationCanceledException)
                {
                    _log.Debug("OperationCanceledException catched. returning.");

                    return;
                }
                catch (Exception ex)
                {
                    if (ex.GetType() != typeof(AccessViolationException) && ex.GetType() != typeof(UnauthorizedAccessException))
                    {
                        _log.Debug(ex, "Exception catched.");
                    }

                    //to be safe, we reset the serial port
                    if (serialPort != null && serialPort.IsOpen)
                    {
                        serialPort.Close();
                    }
                    serialPort?.Dispose();
                    serialPort = null;

                    //allow the system some time to recover
                    Thread.Sleep(500);
                }
                finally
                {
                    if (serialPort != null && serialPort.IsOpen)
                    {
                        serialPort.Close();
                        serialPort.Dispose();
                    }
                }
            }
        }