Exemple #1
0
        /// <summary>
        /// Begins detecting all serial devices to determine if they are GPS devices.
        /// All detection is done asynchronously, so this method returns immediately.
        /// Use the <see cref="WaitForDetection()"/> method if you need to block until detection is completed.
        /// </summary>
        /// <param name="omitColonSuffix">If set to <see langword="true"/>, the colon character (":") will be omitted from the port names.
        /// This is required on some systems in order for the device to be detected properly.</param>
        private static void BeginSerialDetection(bool omitColonSuffix)
        {
            if (AllowSerialConnections)
            {
                Debug.WriteLine("Detecting serial devices", DEBUG_CATEGORY);

                // Begin detection for each of the known serial devices
                int count = SerialDevices.Count;
                for (int index = 0; index < count; index++)
                {
                    SerialDevice device = _serialDevices[index];

                    if (omitColonSuffix && device.Port.EndsWith(":", StringComparison.Ordinal))
                    {
                        // Remove the colon suffix from the port name
                        string newName = device.Port.Substring(0, device.Port.Length - 1);
                        if (!string.IsNullOrEmpty(newName))
                        {
                            RenameDevice(device, newName);
                        }
                    }

                    device.BeginDetection();
                }

                /* If we're performing "exhaustive" detection, ports are scanned
                 * even if there's no evidence they actually exist.  This can happen in rare
                 * cases, such as when a PCMCIA GPS device is plugged in and fails to create
                 * a registry entry.
                 */
                if (_allowExhaustiveSerialPortScanning)
                {
                    Debug.WriteLine("Scanning all serial ports", DEBUG_CATEGORY);

                    // Try all ports from COM0: up to the maximum port number
                    for (int index = 0; index <= _maximumSerialPortNumber; index++)
                    {
                        // Is this port already being checked?
                        bool alreadyBeingScanned = false;
                        foreach (SerialDevice t in _serialDevices)
                        {
                            if (t.PortNumber.Equals(index))
                            {
                                // Yes.  Don't test it again
                                alreadyBeingScanned = true;
                                break;
                            }
                        }

                        // If it's already being scanned, skip to the next port
                        if (alreadyBeingScanned)
                            continue;

                        // Build the port name
                        string portName = "COM" + index;
                        if (!omitColonSuffix)
                            portName += ":";

                        // This is a new device.  Scan it
                        SerialDevice exhaustivePort = new SerialDevice(portName);
                        Debug.WriteLine("Checking " + portName + " for GPS device", DEBUG_CATEGORY);
                        exhaustivePort.BeginDetection();
                    }
                }
            }
        }