How can I detect usb removed when serial port is open?
-
There is a datalogger that measures temperature and humidity. I am reading the recorded temperature and humidity data using QSerialPort. I want to terminate the serial port connection if the usb is removed from my computer while my Serial Port is open. How can I detect that the USB has been removed? I would be glad if you help me in this regard..
-
Hi @TheCeylann,
Seems like you want to watch for QSerialPort::ResourceError errors (among others):
QSerialPort::ResourceErrorAn I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.So perhaps something like:
connect(&serialPort, &QSerialPort::errorOccurred, this, [](const QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError) { // handle possible unplugged event. } });Cheers.
-
The
ResourceErrormay trigger or may not trigger, it depends on the OS, on the driver type and version.A best way is to write an own class
watcherbased on theSetupAPIon Windows, or theUdevon Linux, (but the MacOSX has something similar, I think), which will subscribe to the detach events for the desired device class (USB or SEIRIAL). You can use a GOOGLE to search how to use that APIs.