Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort open error code 10



  • Hi at all,
    I'm back to work on a project that I started 2 month ago and now I have some problem during the communication throght serial com port.

    When I open it I always get the error 10. (QSerialPort::UnsupportedOperationError)

    But this code on this project worked well.
    I try to open the port by another program like "hercules" and I can open and communicate with the device without any problems...

    5008ad2d-8234-4573-9a26-0509aa8bcd60-immagine.png

    When I run my application I still get the error code 10 when I call

    m_serial->open(QIODevice::ReadWrite)
    

    This is the code:

        m_portName = "COM11";
        m_serial = new QSerialPort();
        m_serial->setPortName(m_portName);
        m_serial->setBaudRate(QSerialPort::Baud115200);
        m_serial->setParity(QSerialPort::NoParity);
        m_serial->setStopBits(QSerialPort::OneStop);
        m_serial->setDataBits(QSerialPort::Data8);
        m_serial->setFlowControl(QSerialPort::NoFlowControl);
    
        if (!m_serial->open(QIODevice::ReadWrite))
        {
            qDebug() << QString("Can't open %1, error code %2").arg(m_portName).arg(m_serial->error()) << m_serial->errorString();
            emit signalError(tr("Can't open %1, error code %2").arg(m_portName).arg(m_serial->error()));
            return;
        }
    
    

    Windows 10
    Qt 5.14.2

    Do you have any suggestion?
    Thank you so much.



  • @addebito said in QSerialPort open error code 10:

    qDebug() << QString("Can't open %1, error code %2").arg(m_portName).arg(m_serial->error()) << m_serial->errorString();

    could you please show the actual output of this statement?



  • Yes of course @Pablo-J-Rogina

    "Can't open COM11, error code 10" "Parametro non corretto."

    "Parametro non corretto" it's written in Italian language.
    Parameter isn't correct.



  • @addebito said in QSerialPort open error code 10:

    "Can't open COM11, error code 10"

    Error 10 is QSerialPort::UnsupportedOperationError, are you sure the SerialPort is not already opened/used by another process?


  • Qt Champions 2020

    @addebito said in QSerialPort open error code 10:

    UnsupportedOperationError

    It is:

        case ERROR_INVALID_PARAMETER:
            error.errorCode = QSerialPort::UnsupportedOperationError;
            break;
    

    Maybe it is QSP bug, but also maybe, it is a bug in the serial-port HW firmware (if you use own USB/Serial implementation, and, e.g. it does not handle some USB class-specific callbacks). E.g. such as 'overlapped' operations, the RTS/CTS pins support and do on.



  • @KroMignon Yes of course, I've done some test and the result is the same.
    With hercules, I open the port and I can communicate without any problem.
    After that I close the port and close Hercules, run my QT program but doesn't open the port and give me the error 10.



  • @addebito said in QSerialPort open error code 10:

    After that I close the port and close Hercules, run my QT program but doesn't open the port and give me the error 10.

    I don't know if it is relevant, but did you check if serial port is detected by Qt?

        for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
        {
            qDebug() << "find serial port: " << serialPortInfo.portName() << '"/" << serialPortInfo.description();
        }
    
    


  • @KroMignon I've tried now.

    find serial port: "COM5" / "Dispositivo seriale USB"

    "Dispositivo seriale USB" means "USB serial device"



  • @addebito said in QSerialPort open error code 10:

    find serial port: "COM5" / "Dispositivo seriale USB"

    Maybe that is your problem: "COM5" != "COM11"
    Is it the same serial port?



  • @KroMignon I'm sorry, yes during the test I have also try to change the port number but the result is the same.
    Now I'm using the same port configured as COM5.
    The computer is my laptop and there is only this com port.



  • @addebito said in QSerialPort open error code 10:

    I'm sorry, yes during the test I have also try to change the port number but the result is the same.
    Now I'm using the same port configured as COM5.

    I am sorry, but I am not sure to understand you.
    Do you still have same error, even when using serial port detected with QSerialPortInfo::availablePorts()?



  • Yes @KroMignon, I've the same error and I dont know how to resolve...

    find serial port: "COM5" / "Dispositivo seriale USB"
    "Can't open COM5, error code 10" "Parametro non corretto."

    I can download the source of QSerialPort and try to debug when I open it.
    It could help me to investigate the real problem?


  • Lifetime Qt Champion

    Try to remove the setFoo() calls before open to see if it helps. If so re-add them until the error occurs again.



  • Hi @Christian-Ehrlicher, what you means with setFoo() ?
    Did you mean setFlowControl ?

    I've try to remove it before open the com port but I get the same result.

    "Can't open COM5, error code 10" "Parametro non corretto."


  • Lifetime Qt Champion

    @addebito said in QSerialPort open error code 10:

    Did you mean setFlowControl ?

    all setXXX() - functions



  • @Christian-Ehrlicher Nope, same error.

        for (const auto &serialPortInfo : QSerialPortInfo::availablePorts())
        {
            qDebug() << "find serial port: " << serialPortInfo.portName() << "/" << serialPortInfo.description();
        }
    
        m_portName = "COM5";
        m_serial   = new QSerialPort();
        m_serial->setPortName(m_portName);
        //    m_serial->setBaudRate(QSerialPort::Baud115200);
        //    m_serial->setParity(QSerialPort::NoParity);
        //    m_serial->setStopBits(QSerialPort::OneStop);
        //    m_serial->setDataBits(QSerialPort::Data8);
        //    m_serial->setFlowControl(QSerialPort::NoFlowControl);
    
        if (!m_serial->open(QIODevice::ReadWrite))
        {
            qDebug() << QString("Can't open %1, error code %2").arg(m_portName).arg(m_serial->error()) << m_serial->errorString();
            emit signalError(tr("Can't open %1, error code %2\n%3").arg(m_portName).arg(m_serial->error()).arg(m_serial->errorString()));
            return;
        }
    

    find serial port: "COM5" / "Dispositivo seriale USB"
    "Can't open COM5, error code 10" "Parametro non corretto."


  • Lifetime Qt Champion

    Ok, was just a guess... looks like you have to download the debug symbols and debug into QSerialPort.
    Do you have maybe another USB-serial converter for testing?


  • Qt Champions 2020

    You can try the following WinAPI code at first:

        QByteArray systemLocation = "COM5";
        DWORD desiredAccess = GENERIC_READ | GENERIC_WRITE;
    
        HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t*>(systemLocation.utf16()),
                                  desiredAccess, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
        if (handle == INVALID_HANDLE_VALUE) {
            // OOPS
            const DWORD error = ::GetLastError();
            qDebug() << "ERROR:" << error;
        }
    

    ti see what happens.


  • Qt Champions 2020

    I can download the source of QSerialPort and try to debug when I open it.
    It could help me to investigate the real problem?

    Yes, of course.



  • @kuzulis I get the error 2.... file doesn't exist ??
    the error code 2 is really file not exixt ??

    Screenshot from Windows device manager:

    17ebc648-69aa-4e80-8854-e00d61ee7412-immagine.png

    This is the code.

        DWORD desiredAccess = GENERIC_READ | GENERIC_WRITE;
    
        HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t *>("COM5"), desiredAccess, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
        if (handle == INVALID_HANDLE_VALUE)
        {
            // OOPS
            const DWORD error = ::GetLastError();
            qDebug() << "ERROR:" << error;
        }
    


  • @Christian-Ehrlicher said in QSerialPort open error code 10:

    Do you have maybe another USB-serial converter for testing?

    No, is not a USB serial converter but is a USB connected directly on the device/board.
    I can ask my customer to give me back the fisrt board (the one that I starting to work with).
    It could be the board or firmware yes.... but why with Hercules I can communicate without problems ??


  • Lifetime Qt Champion

    @addebito said in QSerialPort open error code 10:

    reinterpret_cast<const wchar_t *>("COM5")

    And this is wrong. Either use the solution from @kuzulis or pass a proper utf-16 encoded string: L"COM5"



  • @Christian-Ehrlicher you are right but there isn't a call ".utf16()" in QByteArray class so I've replaced with a mistake.

    Now the CreateFile with L"COM5" works. I don't get any error.


  • Lifetime Qt Champion

    @addebito said in QSerialPort open error code 10:

    QByteArray

    Because it should be QString :)

    Ok, then you have to debug by yourself, don't see what should go wrong within QSerialPort code nor can I test it locally.



  • @Christian-Ehrlicher said in QSerialPort open error code 10:

    Ok, then you have to debug by yourself, don't see what should go wrong within QSerialPort code nor can I test it locally.

    Yes but is not as easy as I thought.
    I've run the maintenance QT software and I've check if the source was installed.

    60e27300-e95c-4640-8d1c-b0a0236db1a3-immagine.png

    I've created a simple demo project and added this lines inside the .pro

    include(C:/Qt/5.14.2/Src/qtserialport/src/serialport/serialport-lib.pri)
    

    I have to add also these lines to resolve some issues

    INCLUDEPATH += "C:/Qt/5.14.2/Src/qtbase/include/QtCore/5.14.2/QtCore"
    INCLUDEPATH += "C:/Qt/5.14.2/Src/qtbase/include/QtCore/5.14.2"
    INCLUDEPATH += "C:/Qt/5.14.2/msvc2017_64/include/QtCore/5.14.2"
    

    But now, when I compile I get this error:

    38197876-cda2-4183-8712-84ff8cd559d2-immagine.png

    definition of dllimport static data member not allowed

    I think there is something wrong in my approach...
    How should i do to debug QSerialPort class?
    thank you for your time and your patience.


  • Lifetime Qt Champion

    I don't understand why you try to compile QSerialPort by yourself. Install the debug symbols and sources, compile your app with debug informations, add a breakpoint on QSP::open() call and step into the call.



  • @Christian-Ehrlicher what you means with "Install the debug symbols and sources"?

    I'm in debug with a breakpoint but if try to step into by F11...

    d7196e67-9508-42a5-a240-0ec3910a5d33-immagine.png

    b2c95cad-6693-42e3-a10a-da1530295797-immagine.png



  • @addebito said in QSerialPort open error code 10:

    what you means with "Install the debug symbols and sources"?

    With Maintenance Tool, select "Sources" and "Qt Debug Information Files" for the select Qt Version:
    3ca5c8cb-9646-4d2a-a8c7-bc957ad0ebda-image.png

    Then build your project in "Debug" and start debugging.



  • @KroMignon I've already installed but I don't see anything when I step into...
    I see only the debugger on line 118 (qflags.h) and after F11 again the debugger return on my mainwindow.cpp.

    a95dbbc9-6205-48f1-90f4-0f9cb11f83b1-immagine.png


  • Moderators

    @addebito
    did you add the sources inside QtCreator ? In the Debugger section

    ba3478a3-4772-421f-8c3d-9e8d7d9a717b-image.png


  • Lifetime Qt Champion

    @addebito said in QSerialPort open error code 10:

    I see only the debugger on line 118 (qflags.h) and after F11 again the debugger return on my mainwindow.cpp.

    That's correct since first the flags ctor is called, then hit F11 again to step into the function call.



  • @J-Hilk Thank you. Now I can step into.

    Now I found the line when the class call "return false"

    45d0652c-96b3-43f6-8962-92b606046882-immagine.png

    d3a5f4c4-94a2-4d1e-b958-2bc603f74529-immagine.png

    92c63d68-d6d6-4c30-af72-dae2dd900a31-immagine.png

    SetCommState return 87


  • Moderators

    Well, if I'm not mistaken, then thats the error code for an invalid parameter 🤷‍♂️

    What Windows version do you use ? Is it up to date ? The windows Api changes regularly



  • @J-Hilk

    https://docs.microsoft.com/it-it/windows/win32/debug/system-error-codes--0-499-?redirectedfrom=MSDN

    Yes, from msdn
    87 The parameter is incorrect.

    What kind of parameters ??
    Ohh Windows...
    I'm definitely being roasted.

    Windows 10 home
    Auto update activated
    QT 5.14.2

    Any other suggestions?


  • Qt Champions 2020

    @addebito said in QSerialPort open error code 10:

    Any other suggestions?

    Build qtserialport from sources (just download the sources, open in QtCreator and re-build all targets, include examples). And then from the QtCreator run the terminal-example under the debugger, open your serial port and try to debug as usual.. Then, try to comments-out step-by-step the functions like qt_set_{common|baudrate...}, and then re-build qtserialport and try again.. And then you can look wich function brokes the work.

    For this, you can temporary rename the qtserialportd.dll in your installed Qt path.. In this case will be used another dll compiled in qtserialport build directory (make sure that this directory is in `run environment' PATH of your project settings in QtCreator).



  • Thank you @kuzulis , I'll try.

    I've done another test, same hardware and same computer but with Linux Ubuntu 20.04.1 LTS.
    No problem, I can open the "ttyACM0" write and receive data.


  • Lifetime Qt Champion

    Since you can already call CreateFile() simply go further and call GetCommState/SetCommState and set the parameters one by one according to what Qt does and see which one fails. Maybe first try to call SetCommState without modifying the DCB structure at all.



  • @kuzulis I've try to recompile step by step and try with the qt_set_{...} commented-out but... nope I've commented all the 5 lines without good result.

    87e5b56b-a1cc-4937-b6dc-4a700e593a3c-immagine.png

    BUT ....unbelievable...
    I've try more than one time.
    If I run the terminal example under Linux, everything works fine, after that if I restart the computer without power off the board and run Windows instead Linux.... The terminal example works !!!
    If I switch OFF and after, switch ON again the board the terminal example under Windows going back to fail !!

    So, follow step by step my tests

    1. run my lapton on Linux partition and run the Terminal Example

    1af54b33-b8fe-465b-b7fb-baa84532412a-immagine.png

    1. Restart the computer, run the Windows partition and run the Terminal Example

    32adcec7-bdae-4484-a012-d137e5349aeb-immagine.png

    1. Switch off/on the board, the Terminal example doesn't work.

    97b3a833-9564-4b5f-bb6b-0d6ce2ee9758-immagine.png

    1. If I run Hercules... it's still working

    979cb9a3-4e04-4c4d-bf1f-aeaec3ca109c-immagine.png



  • Can you try to remove the USB device, plug it again and start your software?
    If if fails, could you try to delete the COM controller from Windows devices and force Windows to search for new devices?



  • @ollarch thank you for your suggestion but doesn't work

    Same error:
    "Can't open COM5, error code 10" "Parametro non corretto."

    • I've unistall the COM driver in Windows device manager.
    • Unplug the COM in the same USB.
    • Windows reinstall the drivers.
    • Run my application but doesn't works.

    I've tried the same procedure with another USB port.

    Now I'm trying the last @Christian-Ehrlicher suggestion.


Log in to reply