コード例 #1
0
ファイル: Tlc5940.cs プロジェクト: rmichela/WindowLights
 public Tlc5940(ISerialPortWrapper port, string portName, int numTlcs)
 {
     _port = port;
     _port.Open(portName);
     _numTlcs = numTlcs;
     TlcGsData = new byte[_numTlcs*24];
 }
コード例 #2
0
ファイル: ProtocolController.cs プロジェクト: radu-v/mppt-cli
        private ProtocolController(ISerialPortWrapper serialPort, ILogger logger)
        {
            CommPort = serialPort;

            this.logger = logger;

            ParametersList = new List <McmParameter>();
        }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HartCommunicationLite"/> class.
        /// </summary>
        /// <param name="comPort">The COM port.</param>
        /// <param name="maxNumberOfRetries">The max number of retries.</param>
        public HartCommunicationLite(string comPort, int maxNumberOfRetries)
        {
            MaxNumberOfRetries   = maxNumberOfRetries;
            PreambleLength       = 10;
            Timeout              = TimeSpan.FromSeconds(4);
            AutomaticZeroCommand = true;

            _port   = new SerialPortWrapper(comPort, 1200, Parity.Odd, 8, StopBits.One);
            _worker = new BackgroundWorker();
        }
コード例 #4
0
 internal BlueSoleilSerialPortNetworkStream(ISerialPortWrapper port, UInt32 hConn,
                                            BluesoleilClient cli, BluesoleilFactory factory)
     : base(port, cli)
 {
     _hConn = hConn;
     // (Don't need Cli?)
     _factory = factory;
     //
     _state             = State.Connected;
     port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
 }
コード例 #5
0
ファイル: ProtocolController.cs プロジェクト: radu-v/mppt-cli
        public static async Task <ProtocolController> CreateAsync(ISerialPortWrapper serialPort, ILogger logger)
        {
            var protocolController = new ProtocolController(serialPort, logger);

            protocolController.ConfigureCommPort();
            protocolController.LoadDefaultParametersList();
            await protocolController.LoadParametersListAsync();

            await protocolController.SaveParametersListAsync();

            return(protocolController);
        }
コード例 #6
0
 public void SetUp()
 {
     _serialPort = Substitute.For<ISerialPortWrapper>();
     _communicationInterface = new CommunicationInterfaceImp(_serialPort);
 }
コード例 #7
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();
                    }
                }
            }
        }
コード例 #8
0
 internal SerialPortNetworkStream(ISerialPortWrapper port,
                                  IBluetoothClient cli)
     : base(port.BaseStream)
 {
     _port = port;
 }
コード例 #9
0
 public CommunicationInterfaceImp(ISerialPortWrapper serialPort)
 {
     _port = serialPort;
 }