コード例 #1
0
        private static void GenerateSerialInfo()
        {
            string[] installedPortNames = PortHelper.GetPorts();
            bool     nullModemPresent = false;
            string   portName1 = null, portName2 = null, loopbackPortName = null;

            Array.Sort(installedPortNames);
            Console.WriteLine("Installed ports : " + string.Join(",", installedPortNames));

            IList <string> openablePortNames = CheckPortsCanBeOpened(installedPortNames);

            Console.WriteLine("Openable ports  : " + string.Join(",", openablePortNames));

            // Find any pair of ports which are null-modem connected
            // If there is a pair like this, then they take precedence over any other way of identifying two available ports
            for (var firstIndex = 0; firstIndex < openablePortNames.Count && !nullModemPresent; firstIndex++)
            {
                for (var secondIndex = firstIndex + 1; secondIndex < openablePortNames.Count && !nullModemPresent; secondIndex++)
                {
                    var firstPortName  = openablePortNames[firstIndex];
                    var secondPortName = openablePortNames[secondIndex];

                    if (SerialPortConnection.VerifyConnection(firstPortName, secondPortName))
                    {
                        // We have a null modem port
                        portName1        = firstPortName;
                        portName2        = secondPortName;
                        nullModemPresent = true;

                        Console.WriteLine("Null-modem connection from {0} to {1}", firstPortName, secondPortName);
                    }
                }
            }

            if (!nullModemPresent)
            {
                // If we don't have a null-modem connection - check for a loopback connection
                foreach (var portName in openablePortNames)
                {
                    if (SerialPortConnection.VerifyLoopback(portName))
                    {
                        portName1 = loopbackPortName = portName;
                        break;
                    }
                }

                if (portName1 == null)
                {
                    portName1 = openablePortNames.FirstOrDefault();
                }

                portName2 = openablePortNames.FirstOrDefault(name => name != portName1);
            }

            s_localMachineSerialInfo = new LocalMachineSerialInfo(portName1, portName2, loopbackPortName, nullModemPresent);
        }
コード例 #2
0
        private static void GenerateSerialInfo()
        {
            string[] availablePortNames = PortHelper.GetPorts();
            Debug.WriteLine("total ports : " + availablePortNames.Length);
            bool nullModemPresent = false;

            string portName1 = null, portName2 = null, loopbackPortName = null;

            for (int i = 0; i < availablePortNames.Length; ++i)
            {
                SerialPort com = new SerialPort(availablePortNames[i]);

                try
                {
                    com.Open();
                    com.Close();

                    if (null == portName1)
                    {
                        portName1 = availablePortNames[i];
                    }
                    else if (null == portName2)
                    {
                        portName2 = availablePortNames[i];
                        break;
                    }
                }
                catch (Exception) { }
            }

            if (null != portName1 && SerialPortConnection.VerifyLoopback(portName1))
            {
                loopbackPortName = portName1;
            }

            if (null != portName2)
            {
                if (null == loopbackPortName && SerialPortConnection.VerifyLoopback(portName2))
                {
                    loopbackPortName = portName2;
                }

                nullModemPresent = SerialPortConnection.VerifyConnection(portName1, portName2);
            }

            s_localMachineSerialInfo = new LocalMachineSerialInfo(portName1, portName2, loopbackPortName, nullModemPresent);
        }
コード例 #3
0
        private static void GenerateSerialInfo()
        {
            string[] installedPortNames = PortHelper.GetPorts();
            bool     nullModemPresent = false;
            string   portName1 = null, portName2 = null, loopbackPortName = null;

            Array.Sort(installedPortNames);
            PrintInfo("Installed ports : " + string.Join(",", installedPortNames));

            IList <string> openablePortNames = CheckPortsCanBeOpened(installedPortNames);

            PrintInfo("Openable ports  : " + string.Join(",", openablePortNames));

            // Find any pair of ports which are null-modem connected
            // If there is a pair like this, then they take precedence over any other way of identifying two available ports
            for (int firstIndex = 0; firstIndex < openablePortNames.Count && !nullModemPresent; firstIndex++)
            {
                for (int secondIndex = firstIndex + 1; secondIndex < openablePortNames.Count && !nullModemPresent; secondIndex++)
                {
                    string firstPortName  = openablePortNames[firstIndex];
                    string secondPortName = openablePortNames[secondIndex];

                    if (SerialPortConnection.VerifyConnection(firstPortName, secondPortName))
                    {
                        // We have a null modem port
                        portName1        = firstPortName;
                        portName2        = secondPortName;
                        nullModemPresent = true;

                        PrintInfo("Null-modem connection from {0} to {1}", firstPortName, secondPortName);
                    }
                }
            }

            if (!nullModemPresent)
            {
                // If we don't have a null-modem connection - check for a loopback connection
                foreach (string portName in openablePortNames)
                {
                    if (SerialPortConnection.VerifyLoopback(portName))
                    {
                        portName1 = loopbackPortName = portName;
                        break;
                    }
                }

                if (portName1 == null)
                {
                    portName1 = openablePortNames.FirstOrDefault();
                }

                portName2 = openablePortNames.FirstOrDefault(name => name != portName1);
            }

            // See Github issues #15961, #16033, #20764 - hardware tests are currently insufficiently stable on master CI
            if (loopbackPortName == null && !nullModemPresent)
            {
                // We don't have any supporting hardware - disable all the tests which would use just an open port
                PrintInfo("No support hardware - not using serial ports");
                portName1 = portName2 = null;
            }

            PrintInfo("First available port name  : " + portName1);
            PrintInfo("Second available port name : " + portName2);
            PrintInfo("Loopback port name         : " + loopbackPortName);
            PrintInfo("NullModem present          : " + nullModemPresent);

            s_localMachineSerialInfo = new LocalMachineSerialInfo(portName1, portName2, loopbackPortName, nullModemPresent);

            if (portName1 != null)
            {
                // Measure how big a packet we need to write to be sure to see blocking behaviour at a port
                try
                {
                    s_flowControlCapabilities = SerialPortConnection.MeasureFlowControlCapabilities(portName1);
                }
                catch (Exception e)
                {
                    PrintInfo(e.ToString());
                }

                PrintInfo("{0}: Flow capabilities {1}", portName1, s_flowControlCapabilities);
            }
        }