Ejemplo n.º 1
0
        // Responds to USB PnP (Plug and Play) devices when they are plugged into the computer, unplugged from the computer, or modified
        private void SerialInterface_USBPnPDeviceChanged(object sender, EventArrivedEventArgs e)
        {
            Console.WriteLine("USB PnP device changed");

            //Console.WriteLine(((ManagementBaseObject)(((EventArrivedEventArgs)e).Context))["TargetInstance"].ToString());

            ManagementBaseObject device = (ManagementBaseObject)e.NewEvent["TargetInstance"];  // Is this right?
            string wclass = e.NewEvent.SystemProperties["__Class"].Value.ToString();
            string port   = GetPortName(device);

            //Console.WriteLine(wclass);
            //Console.WriteLine(port);
            //Console.WriteLine(Arduino?.Port?.PortName == port);

            string wop = string.Empty;

            switch (wclass)
            {
            case "__InstanceModificationEvent":
                wop = "modified";
                break;

            case "__InstanceCreationEvent":
                wop = "created";
                break;

            case "__InstanceDeletionEvent":
                wop = "deleted";
                break;

            default:
                wop = "error";
                break;
            }

            if (Arduino?.Port?.PortName == port)  // If the added/removed device is the LCA Arduino in use
            {
                Console.WriteLine("The LCA arduino device on port {0} that you were using was {1}.", port, wop);

                if (wop == "created")
                {
                    throw new InvalidOperationException("Cannot create an arduino device that is already created and in use. ");
                }
                else if (wop == "deleted")
                {
                    // Handle errors here. Code to stop from writing to port or start writing, or whatever?
                    // Or are there no errors to handle?
                    Arduino = null; // This also updates the LCA Arduino list and invokes the event ArduinoChanged
                }
            }
            else if (LCAArduinos.ToList().Exists(a => a?.Port?.PortName == port))  // If the added/removed device was an LCA Arduino not in use
            {
                Console.WriteLine("An LCA arduino device on port {0} that you were not using was {1}.", port, wop);

                if (wop == "deleted")
                {
                    // This part is untested
                    // Want to remove the other ArduinoBoard object from the list without disturbing the currently in-use Arduino
                    int remove_index      = LCAArduinos.ToList().FindIndex(a => a?.Port?.PortName == port);
                    int old_arduino_index = _Arduino;
                    // If this condition is true, Arduino's position in the LCAArduinos list will change after removal:
                    if (remove_index < old_arduino_index && _Arduino != -1)
                    {
                        LCAArduinos.RemoveAt(remove_index);
                        _Arduino--;
                    }
                }
            }
            else  // The added/removed device was not an LCA Arduino (an LCA Arduino device previously known by the program)
            {
                Console.WriteLine("A non-LCA-Arduino device on port {0} was {1}.", port, wop);

                if (wop == "created" && IsGenuineArduino(device))  // A new arduino device was added!
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(2000)); // Give it a little time before trying to connect. Want the Arduino to go through its setup routine first.
                    ActivateArduino(device);                       // Checks if it is an LCA arduino device in the LCAArduinos list (which it shouldn't be), and adds it to LCAArduinos if it is not
                }
            }

            Console.WriteLine("Done with USB PnP device change.");
        }