PDA

View Full Version : Can I use QtSerialPort to detect if a serial device is connected/disconnected?



Kammersgaard
24th July 2012, 07:29
Hey!
I'm writing code for a button that communicates through a serial port. I have used QtSerialPort which have been great. Now I want to see if I can make my program automatically detect if my device is connected/disconnected? I hope to use QtSerialPort but otherwise I open to other solutions.
Thanks!

MrShahi
24th July 2012, 08:11
I don't think so Qt provide support for device interaction. To interact with device use OS native api.

kuzulis
24th July 2012, 08:13
For the serial port in general not possible to determine whether connected to it a device.

Only if the device is special and uses additional lines such as RTS, DTR and so forth, that is, puts these lines in "High",
then we can define the connected device by reading the state of these lines dtr(), rts().

Also, you can determine whether the device is connected - to periodically send requests to it, and if it does not respond - it is not connected.

Kammersgaard
24th July 2012, 09:16
Thanks! Don't think it support those lines. It only got read and write pins and is very simple. I'll try the periodical requests. Thanks for quick reponse!

Added after 52 minutes:

I realized that I that the port I'm using (COM7) disappears from the availablePorts() list when my device is unplugged. Also a call to isValid() returns false. But know I just check every 10 seconds and if the device is replugged before I check COM7 is in the list a it is valid. This means that I have to send a request and see if there is a response. I was wondering if I instead could do something so that I get a warning at once when the device is unplugged? Can I somehow call a slot function?

kuzulis
24th July 2012, 10:31
You can take a code from previous "older" version of parent of this library:

https://gitorious.org/qserialdevice/qserialdevice/trees/master/src/qserialdeviceenumerator

from a class SerialDeviceEnumerator. Here was implemented a notification when plug/unplug/disable/enable action,
with return a list of available devices.

Or you can polling availablePorts() with msecs intervals.

You decide what to choose.