private bool NewPortScan()
        {
            // Then see if port list has changed
            var newPorts = NewPortInList();

            if (!newPorts.Any())
            {
                return(false);
            }

            //TODO: 4s - practical delay for Leonardo board, probably for other boards will be different. Need to investigate more on this.
            const int waitTime = 4000;

            Log(1, "New port(s) " + string.Join(",", newPorts) + " detected, wait for " + (waitTime / 1000.0) + "s before attempt to connect.");

            // Wait a bit before new port will be available then try to connect
            Thread.Sleep(waitTime);

            // Quickly run through most used ports
            var commonBaudRates = DeviceScanBaudRateSelection
                ? SerialUtils.CommonBaudRates
                : new[] { _serialTransport.CurrentSerialSettings.BaudRate };

            foreach (var portName in newPorts)
            {
                // Get baud rates collection
                var baudRateCollection = DeviceScanBaudRateSelection
                    ? SerialUtils.GetSupportedBaudRates(portName)
                    : new[] { _serialTransport.CurrentSerialSettings.BaudRate };

                // First add commonBaudRates available
                var sortedBaudRates = commonBaudRates.Where(baudRateCollection.Contains).ToList();
                // Then add other BaudRates
                sortedBaudRates.AddRange(baudRateCollection.Where(baudRate => !commonBaudRates.Contains(baudRate)));

                foreach (var currentBaudRate in sortedBaudRates)
                {
                    // Stop scanning if state was changed
                    if (ConnectionManagerMode != Mode.Scan)
                    {
                        return(false);
                    }

                    DeviceStatus status = TryConnection(portName, currentBaudRate);
                    if (status == DeviceStatus.Available)
                    {
                        return(true);
                    }
                    if (status == DeviceStatus.IdentityMismatch)
                    {
                        break;                                          // break the loop and continue to next port.
                    }
                }
            }

            return(false);
        }
        private List <string> NewPortInList()
        {
            var currentPorts = SerialUtils.GetPortNames();
            var newPorts     = currentPorts.Except(AvailableSerialPorts).ToList();

            // Actualize ports collection
            AvailableSerialPorts = currentPorts;

            return(newPorts);
        }
 private void UpdateAvailablePorts()
 {
     AvailableSerialPorts = SerialUtils.GetPortNames();
 }
        private bool ThoroughScan()
        {
            Log(1, "Performing thorough scan.");

            // Then try if last stored connection can be opened
            if (PersistentSettings && TryConnection(_serialConnectionManagerSettings.Port, _serialConnectionManagerSettings.BaudRate) == DeviceStatus.Available)
            {
                return(true);
            }

            // Slowly walk through
            foreach (var portName in AvailableSerialPorts)
            {
                // Get baud rates collection
                var baudRateCollection = DeviceScanBaudRateSelection
                    ? SerialUtils.GetSupportedBaudRates(portName)
                    : new[] { _serialTransport.CurrentSerialSettings.BaudRate };

                //  Now loop through baud rate collection
                if (baudRateCollection.Any())
                {
                    Log(1, "Trying serial port " + portName + " using " + baudRateCollection.Length + " baud rate(s).");

                    foreach (var baudRate in baudRateCollection)
                    {
                        // Stop scanning if state was changed
                        if (ConnectionManagerMode != Mode.Scan)
                        {
                            return(false);
                        }

                        DeviceStatus status = TryConnection(portName, baudRate);
                        if (status == DeviceStatus.Available)
                        {
                            return(true);
                        }
                        if (status == DeviceStatus.IdentityMismatch)
                        {
                            break;                                          // break the loop and continue to next port.
                        }
                    }
                }

                // If port list has changed, interrupt scan and test new ports first
                if (NewPortScan())
                {
                    return(true);
                }
            }

            if (!AvailableSerialPorts.Any())
            {
                // Need to check for new ports if current ports list is empty
                if (NewPortScan())
                {
                    return(true);
                }

                // Add small delay to reduce of Quick->Thorough->Quick->Thorough scan attempts - 400ms here + 100ms in main loop = ~500ms
                Thread.Sleep(400);
            }

            return(false);
        }
Example #5
0
 /// <summary> Queries if a current port exists. </summary>
 /// <returns> true if it succeeds, false if it fails. </returns>
 private bool PortExists()
 {
     return(SerialUtils.PortExists(_serialPort.PortName));
 }