Qt World Summit: Submit your Presentation

QSerialPort::open causing QSerialPort::NotOpen error although returning True and working fine

  • Hello,
    in my app I'm using a serial port like this:


    Opening the port like this returns True like expected and I'm able to work with the port later. However this function call causes emitting of a QSerial::errorOccurred signal with an error QSerialPort::NotOpen ("This error occurs when an operation is executed that can only be successfully performed if the device is open."). I commented out every other function call which works with the port, left just these two statements and an error handling slot. The signal is still emitted, which makes no sense to me. According to the docs, an error occures only if open returns False. I'm using Qt 5.10. Can anyone explain this behaviour to me?

  • Lifetime Qt Champion

    Could you try with
    and see if that also says it ?

  • @mrjj Yes, I tried to catch the NotOpen error in the example and it's emitted there as well. I also tried to catch an error like this:

    if (port_->open(QIODevice::ReadWrite)) {
            qDebug() << port_->errorString();
            return true;

    This prints "Unknown error".

  • Qt Champions 2020

    1. Is it UnknownError or NotOpenError?
    2. What is OS do you use (version, kernel)?
    3. What serial port (type, chip) do you use?

  • @sykac
    QSerialPort::open() return true on success. Your code writes out errorString() in this (successful) case, so "Unknown error" would not be unexpected?

  • @kuzulis If I'm catching the errorOccured signal in my error handling slot (slot with a switch for every QSerialPort error), it's catched as NotOpen error. If I check the error like this:

    if (port_->open(QIODevice::ReadWrite)) {
            qDebug() << port_->errorString() << port_->error();
            return true;

    It returns "Unknown error" QSerialPort::SerialPortError<NoError>.

    I'm using Win7, kernel version 6.1.7601.23915, USB serial port (COM6) (USB 2.0). I don't know much about the device itself, only that I had to manually install fdti driver for it (downloaded form the official pages).

    I would expect "No error", not "Unknown error". But what bothers me more is this NotOpen error.

  • Moderators

    Hi, @sykac

    please show us more of your code, this seems to me, like you have a local QSerialPort *port_ variable that overwrites the pointer to the member variable you, may have.

    That would explain why the port opens succesfully but, when you write to it, your try to access the one thats not open.

  • @sykac
    Given that the open() did not return false, I'd expect errorString() just as much to return "some undefined left over value" as your expected "No error", as i don't see the docs state what it will return if no error has occurred.

    For your problem, though, have a look at https://stackoverflow.com/questions/34780535/qserialport-error-signal-on-open-but-open-returns-true. Maybe the "not open" is something internal?

    Also http://doc.qt.io/qt-5/qserialport.html#error-prop:

    The error code is set to the default QSerialPort::NoError after a call to clearError()

    Perhaps call clearError() before you start?

    Finally, you could not establish the errorOccurred slot until after the open(), as I don't think you actually need it till the port has been opened?

  • @J-Hilk
    Actually there isn't much to show, I really commented out everything else. With the object creation and connections this is it:

    serial_ = new QSerialPort(this);
    connect(serial_, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));   //the slot is empty
    connect(serial_, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));  // a switch for every error with no real functionality, just printing the case value
    if (serial_->open(QIODevice::ReadWrite)) {
            qDebug() << serial_->errorString() << serial_->error();
            return true;

    And that's all, I don't access the port object anywhere else in the code. I also run the example from an above comment, the NotOpen error occures also there.

    Actually when I add clearError(), the NotOpen error fired twice. But I checked the links you posted and played with the switch in the errorHandle slot a little bit and I discovered that QSerialPort::NoError and QSerialPort::NotOpen have the same enum value which is zero. If I implement the switch with both of them, it won't build saying "case value 0 already used". So I gues it's some kind of bug in Qt? Or how can this be explained? Because this is the cause of my problem, NoError is fired as NotOpen error. I noticed it only now because I didn't have NoError in the switch before as I didn't want to handle NoError.

  • So I found what the issue was. It is not a NotOpen enum, but NotOpenError enum. The NotOpen enum actually exists but stands for something else and its value is zero. Quite a stupid mistake caused by autocompleter :-) But thanks anyway.

  • @sykac
    I don't know what you mean. http://doc.qt.io/qt-5/qserialport.html#SerialPortError-enum shows QSerialPort::NoError == 0 and QSerialPort::NotOpenError == 13. I don't see any QSerialPort::NotOpen.

  • Moderators

    thats from QIODevice, the base class


  • @J.Hilk Indeed. My reply crossed with the OP's last one explaining where he was at.

  • @JonB
    the NotOpen enum is from QIODevice::OpenModeFlag. I accidentally used it in my switch as QSerialPort::NotOpen (suggested by autocompleter), which compiled ok, but caused this problem.

Log in to reply