Exemplo n.º 1
0
        public static void EnterEchoMode(MeadowSerialDevice meadow)
        {
            if (meadow == null)
            {
                Console.WriteLine("No current device");
                return;
            }

            if (meadow.SerialPort == null && meadow.Socket == null)
            {
                Console.WriteLine("No current serial port or socket");
                return;
            }

            meadow.Initialize(true);
        }
Exemplo n.º 2
0
 public static async Task <MeadowSerialDevice> GetMeadowForSerialPort(string serialPort) //, bool verbose = true)
 {
     try
     {
         if (_connections.ContainsKey(serialPort))
         {
             _connections[serialPort].Dispose();
             _connections.Remove(serialPort);
             Thread.Sleep(1000);
         }
         var meadow = new MeadowSerialDevice(serialPort);
         meadow.Initialize();
         _connections.Add(serialPort, meadow);
         return(meadow);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 3
0
        //returns null if we can't detect a Meadow board
        public static async Task <MeadowSerialDevice> GetMeadowForSerialPort(string serialPort) //, bool verbose = true)
        {
            var maxRetries = 15;
            var attempt    = 0;

            MeadowSerialDevice meadow = null;

connect:

            if (CurrentDevice?.SerialPort != null && CurrentDevice.SerialPort.IsOpen)
            {
                CurrentDevice.SerialPort.Dispose();
            }

            try
            {
                meadow = CurrentDevice = new MeadowSerialDevice(serialPort);
                meadow.Initialize();
                await meadow.GetDeviceInfo().ConfigureAwait(false);

                return(meadow);
            }
            catch (Exception ex)
            {
                // sometimes the serial port needs time to reset
                if (attempt++ < maxRetries)
                {
                    await Task.Delay(500).ConfigureAwait(false);

                    goto connect;
                }
                else
                {
                    return(null);
                }
            }
        }