Opening serial port with Qt



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


  • Moderators

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



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



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

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



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



  • 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?).



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



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



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



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


Log in to reply
 

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