Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort::open causing QSerialPort::NotOpen error although returning True and working fine
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sykac
    wrote on last edited by sykac
    #1

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

    port_->setPortName(name);
    port_->open(QIODevice::ReadWrite)
    

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Could you try with
      http://doc.qt.io/qt-5/qtserialport-terminal-example.html
      and see if that also says it ?

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sykac
        wrote on last edited by
        #3

        @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".

        JonBJ 1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #4
          1. Is it UnknownError or NotOpenError?
          2. What is OS do you use (version, kernel)?
          3. What serial port (type, chip) do you use?
          1 Reply Last reply
          1
          • S sykac

            @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".

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sykac
              wrote on last edited by sykac
              #6

              @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).

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

              J.HilkJ JonBJ 2 Replies Last reply
              0
              • S sykac

                @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).

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

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                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.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • S sykac

                  @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).

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

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @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?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sykac
                    wrote on last edited by
                    #9

                    @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
                    
                    serial_->setPortName(port_);
                    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.

                    @JonB
                    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.

                    JonBJ 1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      sykac
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      1
                      • S sykac

                        @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
                        
                        serial_->setPortName(port_);
                        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.

                        @JonB
                        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.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        @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.

                        J.HilkJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @JonB
                          thats from QIODevice, the base class

                          http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum
                          0_1531315395889_f583d925-2010-4a5e-b904-6aea67762b6c-image.png


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @JonB
                            thats from QIODevice, the base class

                            http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum
                            0_1531315395889_f583d925-2010-4a5e-b904-6aea67762b6c-image.png

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sykac
                              wrote on last edited by
                              #14

                              @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.

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved