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 error code 10

QSerialPort open error code 10

Scheduled Pinned Locked Moved Solved General and Desktop
54 Posts 8 Posters 11.0k Views
  • 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.
  • A Offline
    A Offline
    addebito
    wrote on 27 Oct 2020, 17:52 last edited by
    #41

    This is my last test for today....

    • I run the terminal example under Linux (it's works fine)
    • Restart the computer without unplug or switch off the board
    • Run hte new code to get the com state (GetCommState). Thanks @Christian-Ehrlicher
    • Print screen

    615df85b-bccd-430b-ac11-0ff1da60b37c-immagine.png

    • Call SetCommState with these parameters... works under Windows.
    • Power off the board
    • Power on the board
    • Run again my demo and get the com state as before.
    • Apart the usual settings like 9600 8 N 1.... I see that Linux set the port with the RTS Control Enable, and now is not set !!
    • So, I add this line of code
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
    
    • Run again the demo without success....
      setCommState error: 87 "Parameter is not correct."

    This is the used code:

        DWORD desiredAccess = GENERIC_READ | GENERIC_WRITE;
    
        HANDLE handle = ::CreateFile(reinterpret_cast<const wchar_t *>(L"COM5"), desiredAccess, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
        if (handle == INVALID_HANDLE_VALUE)
        {
            // OOPS
            const DWORD error = ::GetLastError();
            qDebug() << "ERROR:" << error;
        }
    
        DCB dcb;
        int systemErrorCode = 0;
    
        // --------------------------------------------------
        // bool QSerialPortPrivate::getDcb(DCB *dcb)
        ::ZeroMemory(&dcb, sizeof(DCB));
        dcb.DCBlength = sizeof(DCB);
    
        if (!::GetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "GetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            return;
        }
    
        // --------------------------------------------------
        // static inline void qt_set_common_props(DCB *dcb)
        dcb.fBinary       = TRUE;
        dcb.fAbortOnError = FALSE;
        dcb.fNull         = FALSE;
        dcb.fErrorChar    = FALSE;
    
        if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE)
            dcb.fDtrControl = DTR_CONTROL_DISABLE;
    
        if (dcb.fRtsControl != RTS_CONTROL_HANDSHAKE)
            dcb.fRtsControl = RTS_CONTROL_DISABLE;
    
        // --------------------------------------------------
        // baud rate
        dcb.BaudRate = QSerialPort::Baud9600;
        /*
        if (!::SetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "BaudRate: setCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            ::CloseHandle(handle);
            return;
        }
        */
    
        // --------------------------------------------------
        // byte size
        dcb.ByteSize = QSerialPort::Data8;
        /*
        if (!::SetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "ByteSize: setCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            ::CloseHandle(handle);
            return;
        }
        */
    
        // --------------------------------------------------
        // parity
        dcb.Parity  = NOPARITY;
        dcb.fParity = FALSE;
        /*
        if (!::SetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "Parity: setCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            ::CloseHandle(handle);
            return;
        }
        */
    
        // --------------------------------------------------
        // stopbit
        dcb.StopBits = ONESTOPBIT;
        /*
        if (!::SetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "StopBit: setCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            ::CloseHandle(handle);
            return;
        }
        */
    
        // --------------------------------------------------
        // flow control
        dcb.fInX         = FALSE;
        dcb.fOutX        = FALSE;
        dcb.fOutxCtsFlow = FALSE;
        if (dcb.fRtsControl == RTS_CONTROL_HANDSHAKE)
            dcb.fRtsControl = RTS_CONTROL_DISABLE;
        //    switch (flowcontrol)
        //    {
        //        case QSerialPort::NoFlowControl:
        //            break;
        //        case QSerialPort::SoftwareControl:
        //            dcb.fInX  = TRUE;
        //            dcb.fOutX = TRUE;
        //            break;
        //        case QSerialPort::HardwareControl:
        //            dcb.fOutxCtsFlow = TRUE;
        //            dcb.fRtsControl  = RTS_CONTROL_HANDSHAKE;
        //            break;
        //        default:
        //            break;
        //    }
    
        dcb.fRtsControl = RTS_CONTROL_ENABLE;
        if (!::SetCommState(handle, &dcb))
        {
            systemErrorCode = ::GetLastError();
            qDebug() << "setCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            ::CloseHandle(handle);
            return;
        }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on 27 Oct 2020, 18:40 last edited by kuzulis
      #42

      If this does not work when you did:

      ...
          if (!::GetCommState(handle, &dcb))
          {
              systemErrorCode = ::GetLastError();
              qDebug() << "GetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
              return;
          }
      ...
      

      and then immediatelly:

      ...
          if (!::SetCommState(handle, &dcb))
          {
              systemErrorCode = ::GetLastError();
              qDebug() << "SetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
              return;
          }
      ...
      

      then it is LOL.

      Then you can install the serial port sniffer application and to sniff the device I/O control call for the SetCommStat. To see, which parameters are passed with this call e.g using the hercules application, and to compare with the qtserialport.

      Most likelly, it is a bug in FW of your device (in USB CDC class implementation). Or, maybe it expect some specific DCB structure (some specific DCB flags).

      Question: Do you have the specific serial port driver for your device, or do you use the 'standard' usbser.sys driver from Windows?

      PS: On Linux there are different drivers implementation.

      A 1 Reply Last reply 28 Oct 2020, 06:53
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Oct 2020, 18:47 last edited by
        #43

        @kuzulis said in QSerialPort open error code 10:

        then it is LOL.

        Correct, that was the reason why I asked him to try it out to see if it's a driver problem :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        K 1 Reply Last reply 27 Oct 2020, 18:49
        0
        • Christian EhrlicherC Christian Ehrlicher
          27 Oct 2020, 18:47

          @kuzulis said in QSerialPort open error code 10:

          then it is LOL.

          Correct, that was the reason why I asked him to try it out to see if it's a driver problem :)

          K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on 27 Oct 2020, 18:49 last edited by kuzulis
          #44

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

          I asked him to try it out to see if it's a driver problem :)

          Most likelly, it is a problem in device FW, not in driver (because I assume that there are used the CDC ACM driver). :)

          1 Reply Last reply
          3
          • K kuzulis
            27 Oct 2020, 18:40

            If this does not work when you did:

            ...
                if (!::GetCommState(handle, &dcb))
                {
                    systemErrorCode = ::GetLastError();
                    qDebug() << "GetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
                    return;
                }
            ...
            

            and then immediatelly:

            ...
                if (!::SetCommState(handle, &dcb))
                {
                    systemErrorCode = ::GetLastError();
                    qDebug() << "SetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
                    return;
                }
            ...
            

            then it is LOL.

            Then you can install the serial port sniffer application and to sniff the device I/O control call for the SetCommStat. To see, which parameters are passed with this call e.g using the hercules application, and to compare with the qtserialport.

            Most likelly, it is a bug in FW of your device (in USB CDC class implementation). Or, maybe it expect some specific DCB structure (some specific DCB flags).

            Question: Do you have the specific serial port driver for your device, or do you use the 'standard' usbser.sys driver from Windows?

            PS: On Linux there are different drivers implementation.

            A Offline
            A Offline
            addebito
            wrote on 28 Oct 2020, 06:53 last edited by
            #45

            @kuzulis said in QSerialPort open error code 10:

            If this does not work when you did:
            ...
            if (!::GetCommState(handle, &dcb))
            {
            systemErrorCode = ::GetLastError();
            qDebug() << "GetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            return;
            }
            ...

            and then immediatelly:
            ...
            if (!::SetCommState(handle, &dcb))
            {
            systemErrorCode = ::GetLastError();
            qDebug() << "SetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
            return;
            }
            ...

            Doesn't work. Look tha values on the right side... ?!?

            cc9a1a16-cdc5-447e-9bbd-82bc8d2dcbbd-immagine.png

            Then you can install the serial port sniffer application and to sniff the device I/O control call for the SetCommStat. To see, which parameters are passed with this call e.g using the hercules application, and to compare with the qtserialport.

            Do you know some good sniffer program that you have already used?

            Most likelly, it is a bug in FW of your device (in USB CDC class implementation). Or, maybe it expect some specific DCB structure (some specific DCB flags).
            Question: Do you have the specific serial port driver for your device, or do you use the 'standard' usbser.sys driver from Windows?

            No, I'm using the standard windows usbser.sys

            O 1 Reply Last reply 28 Oct 2020, 08:13
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 28 Oct 2020, 06:58 last edited by
              #46

              Maybe you can check if the dcb contains a difference when it works (so when it was first started on linux) and when not. Don't know if it helps though.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              A 1 Reply Last reply 28 Oct 2020, 07:02
              0
              • Christian EhrlicherC Christian Ehrlicher
                28 Oct 2020, 06:58

                Maybe you can check if the dcb contains a difference when it works (so when it was first started on linux) and when not. Don't know if it helps though.

                A Offline
                A Offline
                addebito
                wrote on 28 Oct 2020, 07:02 last edited by
                #47

                @Christian-Ehrlicher Thanks but I've already done when, in my previuos post I wrote "This is my last test for today...."

                Christian EhrlicherC 1 Reply Last reply 28 Oct 2020, 07:18
                0
                • A Offline
                  A Offline
                  addebito
                  wrote on 28 Oct 2020, 07:04 last edited by
                  #48

                  Even though I haven't solved it I'd like to give you a big thank you.

                  @Christian-Ehrlicher
                  @J-Hilk
                  @KroMignon
                  @kuzulis
                  @ollarch
                  @Pablo-J-Rogina

                  I'll try to contact the board supplier to investigate if there was some fw changes.

                  1 Reply Last reply
                  2
                  • A addebito
                    28 Oct 2020, 07:02

                    @Christian-Ehrlicher Thanks but I've already done when, in my previuos post I wrote "This is my last test for today...."

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 28 Oct 2020, 07:18 last edited by Christian Ehrlicher
                    #49

                    @addebito Sorry, overread it :)

                    But I think you have enough to ask the supplier for help since it can be simply reproduced with plain WinAPI calls :)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    A 1 Reply Last reply 28 Oct 2020, 07:38
                    1
                    • Christian EhrlicherC Christian Ehrlicher
                      28 Oct 2020, 07:18

                      @addebito Sorry, overread it :)

                      But I think you have enough to ask the supplier for help since it can be simply reproduced with plain WinAPI calls :)

                      A Offline
                      A Offline
                      addebito
                      wrote on 28 Oct 2020, 07:38 last edited by addebito
                      #50

                      @Christian-Ehrlicher Yes you are right !! ....after 2 days of testing!

                      1 Reply Last reply
                      0
                      • A addebito
                        28 Oct 2020, 06:53

                        @kuzulis said in QSerialPort open error code 10:

                        If this does not work when you did:
                        ...
                        if (!::GetCommState(handle, &dcb))
                        {
                        systemErrorCode = ::GetLastError();
                        qDebug() << "GetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
                        return;
                        }
                        ...

                        and then immediatelly:
                        ...
                        if (!::SetCommState(handle, &dcb))
                        {
                        systemErrorCode = ::GetLastError();
                        qDebug() << "SetCommState error:" << systemErrorCode << qt_error_string(systemErrorCode);
                        return;
                        }
                        ...

                        Doesn't work. Look tha values on the right side... ?!?

                        cc9a1a16-cdc5-447e-9bbd-82bc8d2dcbbd-immagine.png

                        Then you can install the serial port sniffer application and to sniff the device I/O control call for the SetCommStat. To see, which parameters are passed with this call e.g using the hercules application, and to compare with the qtserialport.

                        Do you know some good sniffer program that you have already used?

                        Most likelly, it is a bug in FW of your device (in USB CDC class implementation). Or, maybe it expect some specific DCB structure (some specific DCB flags).
                        Question: Do you have the specific serial port driver for your device, or do you use the 'standard' usbser.sys driver from Windows?

                        No, I'm using the standard windows usbser.sys

                        O Offline
                        O Offline
                        ollarch
                        wrote on 28 Oct 2020, 08:13 last edited by
                        #51

                        @addebito look at this https://www.hhdsoftware.com/
                        "Serial Monitor" app is ok.

                        1 Reply Last reply
                        1
                        • K Offline
                          K Offline
                          kuzulis
                          Qt Champions 2020
                          wrote on 28 Oct 2020, 09:11 last edited by
                          #52

                          Also you can use the following software , seems it is free.

                          1 Reply Last reply
                          1
                          • A Offline
                            A Offline
                            addebito
                            wrote on 2 Nov 2020, 12:41 last edited by
                            #53

                            just a follow up... it was a firmware problem.

                            1 Reply Last reply
                            7
                            • C Offline
                              C Offline
                              caicx
                              wrote on 19 Jan 2022, 02:31 last edited by
                              #54

                              @addebito , I have an issue more or less the same as you.

                              I suppose the main issue comes from one program that has opened the serial port and initialed any DCB parameters, when your Qt program opens the device, Qt driver does not reset all DCB data, but some parameters did not suitable for QtSerialPort, so the issue comes up.

                              here is a DCB that first open after the PC reset:
                              ed86f8f0-4fd4-4bd6-b60f-976b8b656153-image.png

                              here is a DCB being opened by another program, it changed XoffLim/XonLim
                              16888819-86c9-4e68-80ca-be497a30a948-image.png

                              so, I write some code clear DCB before Qt open serial port:

                              
                              #if (defined (_WIN32) || defined (_WIN64))
                              void clearSerialPort (const QString &path)
                              {
                                  #include <winbase.h>
                              
                                  QString deviceString = path.startsWith(QLatin1String("COM"))
                                                          ? (QLatin1String("\\\\.\\") + path) : path;
                              
                                  HANDLE handle = ::CreateFile(
                                              reinterpret_cast<const wchar_t*>(deviceString.utf16()),
                                              GENERIC_READ | GENERIC_WRITE,
                                              0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
                                  if (handle == INVALID_HANDLE_VALUE) {
                                      return;
                                  }
                              
                                  DCB dcb;
                                  memset(&dcb, 0, sizeof(DCB)); //::ZeroMemory(dcb, sizeof(dcb));
                                  dcb.DCBlength = sizeof(DCB);
                              
                                  if (::GetCommState(handle, &dcb)) {
                                      dcb.XoffLim = 0;
                                      dcb.XonLim = 0;
                                      ::SetCommState(handle, &dcb);
                                  }
                              
                                  ::CloseHandle(handle);
                              }
                              #endif
                              
                              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