[SOLVED] QSerialPort fails opening with "File exists" error message

  • Hi all,

    I am developing a custom embedded device. This device runs Buildroot linux with Qt 5.3. It features an AVR-MCU (Arduino 1284p) that is connected to the main processor (that runs Linux and Qt) via a serial connection (RX, TX). The Arduino-MCU simple reads the serial port for commands, executes them (i.e. read temperature sensor) and prints the result.

    In the linux shell I can run cat /dev/ttyS3 to read the data the Arduino has written and I can send bytes by calling echo 'Hello Arduino' >> /dev/ttyS3 for example. That works fine.

    If I want to use QSerialPort by calling:

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        qDebug() << info.portName();
    m_serialPort = new QSerialPort("ttyS3");
    if (m_serialPort->open(QIODevice::ReadWrite))
        qDebug() << "Serial Port opened ttyS3";
        qDebug() << "Failed to open ttyS3 serial port";
        qDebug() << m_serialPort->errorString();
        qDebug() << m_serialPort->error();

    I receive the error message "File exists" (errorString) and the error-ID is 2. I am running the Qt application as root. http://superuser.com/questions/569255/how-to-quickly-determine-which-process-if-any-listens-to-a-serial-port lists a few linux commands that identify the process that has the serial port in use. Non of the examples (lsof /dev/ttyS3 nor fuser -n file /dev/ttyS3) lists a process. So I am pretty sure the serial port is not in use.

    If I try the source code with ttyS2 everything works fine (besides the fact that nothing useful is connected to ttyS2 of course ;-)).

    Anyone has an idea what's going wrong?

    Thanks a lot.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Just an idea: did you check the permissions on /dev/ttyS3 ?

  • Most likely that "File exists" goes from the tty-locker (from QLockFile), because the ttyS3-lock file exists. It is probable that this issue belongs to this bug: https://bugreports.qt.io/browse/QTBUG-36983

    So, you can:

    1. Try to use a latest version of QtSerialPort
    2. Just find out the lock-file and to remove it before opening.
    3. That is better - it is do not start the application with a root privileges. You should try to create an user, and to add his to "dialout" grop or similar (depends on your base distro).

  • AWESEOME! Thank you very much. 2) did the trick. I found a file /var/lock/LCK..ttyS3. After removing it the code posted above works fine. I will now try 1) or implement a patch myself.

    Thanks a lot. You made my day.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.