4

I have an Arduino-based device which connects through USB.

I'd like to detect it from my Qt 4 application, using QExtSerialPort (or whatever necessary), when it's plugged in.

If this weren't possible, I thought I could somehow get a list of the system's port names and just try all of them in search for my Arduino (where I'd implement some kind of handshaking procedure for it to detect it correctly). My concern in this approach is that I'm not sure if a device (for example, printer) would get damaged if I send some kind of handshaking ack at a different baud rate.

So, I don't really know where to start for any of them. Which would be the best approach? How would I implement it?

3 Answers 3

1

I believe you can find list of serial ports on Windows by looking into

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

registry key

Each serial port on a UNIX system has one or more device files (files in the /dev directory) associated with it:

System           Port 1           Port 2
IRIX®            /dev/ttyf1       /dev/ttyf2
HP-UX            /dev/tty1p0      /dev/tty2p0
Solaris®/SunOS®  /dev/ttya        /dev/ttyb
Linux®           /dev/ttyS0       /dev/ttyS1
Digital UNIX®    /dev/tty01       /dev/tty02

more details on serial programing on POSIX systems here

1

Since your device is USB, your UART port will be emulated by some kind of conversor in his hardware. So first you must understand what driver is being used on your system.

The most common SERIAL->USB conversor uses PL2303/PL2301 chip, so it would create a path on /dev, if its the first device, it will appear as "/dev/ttyUSB0", but you may also see the list reading the proc path (like "cat /proc/bus/usb/devices"). Under Windows it usually creates a virtual "COM", just go to device manager and check the port.

When you are sure about how the HW talks to your system, you may use QExtSerialPort for wrapping the system API and talk to the device.

1

Way too hard and too platform specific, using weird Windows Registry keys or rely on hard wired device nodes on Linux.

You are on the right way. Get QextSerialPort or QSerialDevice (which I preffer in my projects, because it got integrated in Qt5), have a look at the examples and simply use it. In both libraries you get some kind of port enumerator class which returns you a list of all configures serial ports. Only platform/device specific settings you will have to do manually (like getting RS485 in half-duplex mode on my current embedded project), but "standard" problems are perfectly encapsulated in a QIODevice implementation.

You can use both QextSerialPort and QSerialDevice like a file. Open it (instead of a filename you specify the device name ie. "COM1" on Windows or "/dev/tty0" on Linux, depending on your configuration) and then read or write like you are doing it with an ordinary QFile, QBuffer, Qwhatever-inherits-from-QIODevice.

If you have any problems opening the port and communicating, don't hesitate to ask! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.