Exemplo n.º 1
0
        public void Connect()
        {
            PortNames = GetPortsNames();
            if (PortNames == null)
            {
                return;
            }
            for (int i = 0; i < PortNames.Count(); i++)
            {
                PortHandle = new SerialPort(PortNames[i], _baudRate)
                {
                    DataBits = 8,
                    Parity   = Parity.None,
                    StopBits = StopBits.One
                };
                ReadEnabled = true;
                try
                {
                    PortHandle.Open();
                }
                catch (Exception e)
                {
                    ConnectionFailure?.Invoke(e);
                }

                ReadTimer.Change(0, PolyConstants.USBTimerInterval);
                CurrentPort = i;
                break;
            }
        }
Exemplo n.º 2
0
 public Task StartReconnection()
 {
     reconnectCancellationSource = new CancellationTokenSource();
     return(Task.Factory.StartNew(async() =>
     {
         reconnectCancellationSource.Token.ThrowIfCancellationRequested();
         while (true)
         {
             if (reconnectCancellationSource.Token.IsCancellationRequested)
             {
                 reconnectCancellationSource.Token.ThrowIfCancellationRequested();
             }
             if (ConnectionChallengeFunction == null)
             {
                 throw new ConfigurationException("Unsetted challenge function for reconnection.");
             }
             if (!await ConnectionChallengeFunction(this))
             {
                 StoptListener();
                 ConnectionFailure?.Invoke(this, DateTime.UtcNow);
                 await ProtocolStream.Reconnect();
                 startListener();
             }
             await Task.Delay(ConnectionDelay);
         }
     }, reconnectCancellationSource.Token));
 }