QSerialPort fails to open "/dev/ttyUSB0" on Ubuntu

6k views Asked by At

I'm trying to connect a micro-controller with my desktop PC via a USB serial cable. The OS of my desktop PC is Ubuntu 13.10. The USB serial cable is TTL-232R-3V3 (FTDI).

Here is my source code. When running the program, it end up in failure. (In this case, "Fail" was printed on console.)

#include <QCoreApplication>
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPortInfo>

QT_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // Example use QSerialPortInfo
    QSerialPort serial("/dev/ttyUSB0");
    if (serial.open(QIODevice::ReadWrite)){
        qDebug() << "Pass\n";
        serial.close();
    } else {
        qDebug() << "Fail\n";
    }

    return a.exec();
}

According to this article, following statement have to be added in .pro file.

QT += serialport

When the USB serial cable is plugged in, it is recognized as "/dev/ttyUSB0" on Ubuntu. It seems to work well.

$dmesg | grep ttyUSB
>>[   27.653383] usb 6-1: FTDI USB Serial Device converter now attached to ttyUSB0

$ls -la | grep ttyUSB
>> crw-rw----   1 root dialout 188,   0 12月 14 17:30 ttyUSB0

In order to avoid permission troubles, my username is added to dialout group by gpasswd command. As a result, I can communicate with the USB serial device on terminal software.

But I cannot resolve the QSerialPort problem. Does anyone have any ideas? Any help would be appreciated.

Thanks in advance!

1

There are 1 answers

0
László Papp On BEST ANSWER

As Frank suggested, you can always get the error diagnostics with errorString(). See our examples for details.

I cannot reproduce the problem, but based on your comment, your serial port was already used by another process, so this means it is not a QtSerialPort issue. Any other software would have had an issue with this, e.g. a new minicom session.

Glad that it works now. :)