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. Opening serial port with Qt
Qt 6.11 is out! See what's new in the release blog

Opening serial port with Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 4.8k Views 2 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.
  • B Offline
    B Offline
    Bremenpl
    wrote on last edited by
    #1

    I am having problems with opening serial port in Qt. The error message I receive after failed attempt to open the port is UnsupportedOperationError. I would like to point out couple things:

    • The port I am trying to open is implemented in an ARM MCU using USB (CDC),
    • I can open the port in other application (like teraterm or realterm),
    • I was able to open the port in my Qt application just a minute a go. I added some code in the ARM device (not concerning the USB functionality at all) and I couldnt open the port in Qt anymore.
    • I tested that I can send/ receive the data on the port opened in other program (ie. realterm),
    • The port works in other applications, but not Qt (tried the terminal example from Qt creator and it cannot open the port as well),

    I have come across this behaviour in the past, but did not have time to go further into it. Now it is blocking me. What could be the case here with opening the port? Does qt put any additional requirements to it? I would really appreciate all help. I am running qt on windows machine.

    lprzenioslo.zut.edu.pl

    K 1 Reply Last reply
    0
    • B Bremenpl

      I am having problems with opening serial port in Qt. The error message I receive after failed attempt to open the port is UnsupportedOperationError. I would like to point out couple things:

      • The port I am trying to open is implemented in an ARM MCU using USB (CDC),
      • I can open the port in other application (like teraterm or realterm),
      • I was able to open the port in my Qt application just a minute a go. I added some code in the ARM device (not concerning the USB functionality at all) and I couldnt open the port in Qt anymore.
      • I tested that I can send/ receive the data on the port opened in other program (ie. realterm),
      • The port works in other applications, but not Qt (tried the terminal example from Qt creator and it cannot open the port as well),

      I have come across this behaviour in the past, but did not have time to go further into it. Now it is blocking me. What could be the case here with opening the port? Does qt put any additional requirements to it? I would really appreciate all help. I am running qt on windows machine.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Bremenpl

      Did you reboot the ARM device?
      It sounds to me like some application is still blocking your serial port or the previous run did not properly close the serial port.

      Vote the answer(s) that helped you to solve your issue(s)

      B 1 Reply Last reply
      0
      • K koahnig

        @Bremenpl

        Did you reboot the ARM device?
        It sounds to me like some application is still blocking your serial port or the previous run did not properly close the serial port.

        B Offline
        B Offline
        Bremenpl
        wrote on last edited by Bremenpl
        #3

        @koahnig As the matter of the fact, at the moment my situation looks like this: I reset the ARM device all the time. The USB init routine is at the beggining. Every SECOND time (reset) I can connect to the device using Qt application. I can however connect every time from other apps. Im not sure either this is an initialization issue in the ARM device. Is there any way to "clear" the used devices in Qt, or anything? I am not sure how does the mechanism in windows works underneath, but it seems to be something in Qt app. I always close the port in Qt before resetting the device.

        Edit: Also I have checked out the code for open method in qserialport.cpp:

        /*!
            \reimp
        
            Opens the serial port using OpenMode \a mode, and then returns true if
            successful; otherwise returns false and sets an error code which can be
            obtained by calling the error() method.
        
            \note The method returns false if opening the port is successful, but could
            not set any of the port settings successfully. In that case, the port is
            closed automatically not to leave the port around with incorrect settings.
        
            \warning The \a mode has to be QIODevice::ReadOnly, QIODevice::WriteOnly,
            or QIODevice::ReadWrite. Other modes are unsupported.
        
            \sa QIODevice::OpenMode, setPort()
        */
        bool QSerialPort::open(OpenMode mode)
        {
            Q_D(QSerialPort);
        
            if (isOpen()) {
                d->setError(QSerialPortErrorInfo(QSerialPort::OpenError));
                return false;
            }
        
            // Define while not supported modes.
            static const OpenMode unsupportedModes = Append | Truncate | Text | Unbuffered;
            if ((mode & unsupportedModes) || mode == NotOpen) {
                d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, tr("Unsupported open mode")));
                return false;
            }
        
            clearError();
            if (!d->open(mode))
                return false;
        
            QIODevice::open(mode);
            return true;
        }
        

        I cannot find out why is the open func returning false. Also the error msg doesnt really match anything because my error message is: "QSerialPort::SerialPortError(UnsupportedOperationError)"
        From the code it seems I should get additionally open error string or unsupported open mode string...

        lprzenioslo.zut.edu.pl

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by kuzulis
          #4

          my error message is: "QSerialPort::SerialPortError(UnsupportedOperationError)"

          It is NOT an error message, it is error code. Error message returned by serialPort->errorString().

          B 1 Reply Last reply
          1
          • K kuzulis

            my error message is: "QSerialPort::SerialPortError(UnsupportedOperationError)"

            It is NOT an error message, it is error code. Error message returned by serialPort->errorString().

            B Offline
            B Offline
            Bremenpl
            wrote on last edited by
            #5

            @kuzulis You are right, I will also check the string tommorow morning.

            lprzenioslo.zut.edu.pl

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by kuzulis
              #6

              BTW, take a look on RTS/DTR handling of your CDC ACM firmware, maybe it is reason... I suspect, that your FW wrongly handles some of ioctrl's calls, that lead to Windows's ERROR_INVALID_PARAMETER error (as I understand) . You need to debug a place where it happens.

              In common case there are three places in opening sequence: setup DCB, setup COMMTIMEOUTS, and setup SetCommMask function (if we talk about windows). And plus one additional (first place) - it is CreateFile function, where the serial port opens in overlapped mode (maybe your firmware does not support it?).

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bremenpl
                wrote on last edited by
                #7

                The errorString is: "Parametr jest niepoprawny." Which in english stands for "Parameter is invalid"

                @kuzulis Thank you for your answer. I dont think there is any handling of RTS/DTR in the CDC code... The problem is that the port sometimes can be opened and sometimes not (but only in Qt application). I can always open the port in other apps.

                lprzenioslo.zut.edu.pl

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by kuzulis
                  #8

                  Need to debug it in any case on your side (you even can provide a TeamViewer acces, and then I can try to help :) ) ...

                  B 1 Reply Last reply
                  1
                  • K kuzulis

                    Need to debug it in any case on your side (you even can provide a TeamViewer acces, and then I can try to help :) ) ...

                    B Offline
                    B Offline
                    Bremenpl
                    wrote on last edited by
                    #9

                    @kuzulis That would be really great with teamviewer. The problem is that at the moment I cant get the error to occur- I can always connect... I could this morning though. Could we set the time fo which we could connect with teamviewer?

                    lprzenioslo.zut.edu.pl

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kuzulis
                      Qt Champions 2020
                      wrote on last edited by
                      #10

                      Could we set the time fo which we could connect with teamviewer?

                      I'm available today up to 19:00 by MSK, just ping me.

                      1 Reply Last reply
                      0

                      • Login

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