public void EnhancedSerialConnection_OpenAndDoubleClose()
 {
     var connection = new EnhancedSerialConnection();
     connection.Open();
     connection.Close();
     connection.Close();
 }
예제 #2
0
        private static ISerialConnection FindConnection(Func <ArduinoSession, bool> isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates)
        {
            for (int x = portNames.Length - 1; x >= 0; x--)
            {
                foreach (SerialBaudRate rate in baudRates)
                {
                    try
                    {
                        using (var connection = new EnhancedSerialConnection(portNames[x], rate))
                        {
                            using (var session = new ArduinoSession(connection, 100))
                            {
                                Debug.WriteLine("{0}:{1}; ", portNames[x], (int)rate);

                                if (isDeviceAvailable(session))
                                {
                                    return(new EnhancedSerialConnection(portNames[x], rate));
                                }
                            }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Port is not available.
                        Debug.WriteLine("{0} NOT AVAILABLE; ", portNames[x]);
                        break;
                    }
                    catch (TimeoutException)
                    {
                        // Baudrate or protocol error.
                    }
                }
            }
            return(null);
        }
 public void EnhancedSerialConnection_Constructor_WithParameters()
 {
     var connection = new EnhancedSerialConnection("COM1", SerialBaudRate.Bps_115200);
     Assert.AreEqual(100, connection.ReadTimeout);
     Assert.AreEqual(100, connection.WriteTimeout);
     Assert.AreEqual(115200, connection.BaudRate);
 }
예제 #4
0
        private static ISerialConnection FindConnection(Func <ArduinoSession, bool> isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates)
        {
            bool found = false;

            for (int x = portNames.Length - 1; x >= 0; x--)
            {
                foreach (SerialBaudRate rate in baudRates)
                {
                    try
                    {
                        using (var connection = new EnhancedSerialConnection(portNames[x], rate))
                        {
                            using (var session = new ArduinoSession(connection, 100))
                            {
#if TRACE
                                Debug.WriteLine("{0}:{1}; ", portNames[x], (int)rate);
#endif
                                if (isDeviceAvailable(session))
                                {
                                    found = true;
                                }
                            }
                        }

                        if (found)
                        {
                            return(new EnhancedSerialConnection(portNames[x], rate));
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Port is not available.
#if TRACE
                        Debug.WriteLine("{0} NOT AVAILABLE; ", portNames[x]);
#endif
                        break;
                    }
                    catch (TimeoutException)
                    {
                        // Baudrate or protocol error.
                    }
                    catch (IOException ex)
                    {
#if TRACE
                        Debug.WriteLine($"HResult 0x{ex.HResult:X} - {ex.Message}");
#endif
                    }
                }
            }
            return(null);
        }
예제 #5
0
        private static ISerialConnection FindConnection(Func<ArduinoSession, bool> isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates)
        {
            for (int x = portNames.Length - 1; x >= 0; x--)
            {
                foreach (SerialBaudRate rate in baudRates)
                {
                    try
                    {
                        using (var connection = new EnhancedSerialConnection(portNames[x], rate))
                        {
                            using (var session = new ArduinoSession(connection, 100))
                            {
                                Debug.WriteLine("{0}:{1}; ", portNames[x], (int)rate);

                                if (isDeviceAvailable(session))
                                    return new EnhancedSerialConnection(portNames[x], rate);
                            }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Port is not available.
                        Debug.WriteLine("{0} NOT AVAILABLE; ", portNames[x]);
                        break;
                    }
                    catch (TimeoutException)
                    {
                        // Baudrate or protocol error.
                    }
                }
            }
            return null;
        }