Reputation: 1064
A few years ago, I wrote a little program in PyQt5 which monitored changes in the output from QSerialPortInfo.availablePorts()
to produce a live information display, showing devices which are connected to USB ports, and accessible to the user (so the USB mouse and keyboard are not listed). I have tested this program with success on two Linux machines and one MacBook.
When I wrote this program, I had only one kind of device that interested me, Nordic nRF52 development boards. I might connect two or three of them to one host computer at a time, adding and removing them on the fly. The dev boards had no power switches, so you could only disconnect them by unplugging the USB cable. The host computer OS recognized the disconnect event, and freed up the corresponding port.
In a current test using my program, I have a different device, a Kiprim programmable power supply which can supposedly be controlled using the SCPI protocol over the USB connection at the back of the unit. I just tried to find this device using my PyQt5 serial port scanner. My program finds the device and reserves a serial port for it when the USB cable is connected. It frees up the serial port when the USB cable is unplugged. So far, so good.
Unlike the Nordic development boards, the Kiprim power supply also has a power switch. If I turn the switch off when the power supply is connected over USB, the host does not free up a serial port. I would want to recognize that the data connection to the power supply has been lost, even if the cable is still attached. QSerialPortInfo.availablePorts()
does not tell me that.
Why are unplugging the USB cable and powering off the device being recognized as two distinct events? What is the software and/or hardware explanation? Is this issue unique to Qt (PyQt, PyQt5) or is this a more universal issue, either defined by the OS or by a USB standard? And finally: what is the recommended way for the host computer to detect the power-off event?
Thanks for your advice!
Upvotes: 1
Views: 140
Reputation: 87486
The USB host provides some power to USB devices through the USB cable which can be use to power a microcontroller. So the external power supply is not required for a USB device to function (your wired mouse and keyboards don't have them for instance). Therefore, when the external power supply is switched off, the USB interface of the device can still function and there is no guarantee that your operating system will detect any kind of change.
This really doesn't have anything to do with Qt. You can look in your Device Manager (Windows) or run lsusb (Linux) to see if the operating system is still recognizing the device.
You might be able to detect whether the external power supply is present by sending a "Get Status" request to the device. This is specified in section 9.4.5 of the USB 2.0 specification. The device returns a bit named "Self powered". If that doesn't work, you should consult the documentation for your specific device to see if there is a way to detect the external power supply.
Upvotes: 2