How to detect if a QCanBusDevice has been physically disconnected?
-
I'm using QCanBusDevice to interface with a Peak Can-Usb adapter.
How can I tell if the device has been removed, i.e. the usb adapter has been physically disconnected from my computer? I looked at all methods QCanBusDevice offers, but none of them tell me if the device is still physically connected.
- QCanBusDevice::state() remains in the ConnectedState (this is probably the most counterintuitive)
- QCanBusDevice::hasBusStatus() remains true
- neither QCanBusDevice::error() nor QCanBusDevice::errorString() report an error
- QCanBusDevice::errorOccured() is not emitted
It seems like the QCanBusDevice doesnt notice at all that adapter is no longer there.
The only thing that seems to change (and what I'm using right now) is the QCanBusDevice::deviceInfo(), which goes blank when you remove the adapter. Still, this seems rather hacky, so I'm wondering if I'm doing something unintended or if this feature is simply not implemented in QCanBusDevice (or PeakCanBackend to be specific) -
Hi @__mk__ ,
there is indeed no dedicated API for that problem currently.
What comes closest is
QCanBusDevice::busStatus(), but that will currently (most likely) returnCanBusStatus::Unknownas there is no special enum for that case.But I'll take your post as feature request and see if I can add something. I'm not completely sure if that fits into
busStatus()or if a separateddeviceStatus()would be better. I'm open for suggestions.Note:
QCanBusDevice::hasBusStatus()just tells you if the device supports the functionbusStatus(), it is not the actualbusStatus().Regards
-
I've added a task for that: https://qt-project.atlassian.net/browse/QTBUG-151640
-
I've added a task for that: https://qt-project.atlassian.net/browse/QTBUG-151640
@aha_1980 Thanks for taking care of this!
I think a method like
deviceState()ordeviceType()would be appropriate and offer more flexibility for the different CAN plugins. I wouldn't changeQCanBusDevice::state()orbusStatus(), they have a different meaning.I have noticed that the Peak USB devices actually fall back to a virtual CAN interface the moment you disconnect the usb device (the virtual interface is called "PCANLight_USB_16" in PCAN-View). This might explain why no errors are reported and the QCanBusDevice remains connected - the virtual interface is still working. This automatic fallback seems to be an intended feature of the peak driver.
Ideally there'd be a
DeviceTypeenum with values likeVirtualandPhysicalto represent what kind of device it currently is. But this switching behaviour seems pretty specific to peak devices, so perhaps somebody has an idea for a more general approach.
Ideally-ideally this change would also be emitted as a signal :)