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. Unable to read from QSerialPort using MSVC compiler
QtWS25 Last Chance

Unable to read from QSerialPort using MSVC compiler

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportmingwmsvcread serial
25 Posts 5 Posters 3.7k 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.
  • T Offline
    T Offline
    Turi
    wrote on last edited by
    #1

    Hi,
    I'm developing an application to communicate via serial using master/slave architectures with an application running on Linux. My application is the master and runs on Windows 10 and the application on Linux is slave. I'm using the QSerialPort class to do this.
    My proble is: if I use the mingw compiler the communication works fine, but if I use the MSVC compiler with the same code, my application is able to write on serial port but is unable to read the data.

    Here a snippet of code where use the serial.

    qint64 pBytesWritten = serial->write(dataToSend);
    serial->flush();
    
    if (pBytesWritten == dataToSend.length()) {
            qDebug().nospace().noquote() << "Write ok";
        QThread::msleep(50);
        if (readMsg() == E_OK) {
            qDebug().nospace().noquote() << "Read ok";
        } else {
            qDebug().nospace().noquote() << "Read not ok";
        }
    

    readMsg function:

    bool readMsg(){
        bool res = serial->waitForReadyRead(ReceptionTimeout);
        QByteArray recBuff;
        if (res)
            recBuff = serial->readAll();
        .
        .
        .
        return res;
    }
    

    The output is always:

    Write ok
    Read not ok
    
    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • T Turi

      Hi,
      I'm developing an application to communicate via serial using master/slave architectures with an application running on Linux. My application is the master and runs on Windows 10 and the application on Linux is slave. I'm using the QSerialPort class to do this.
      My proble is: if I use the mingw compiler the communication works fine, but if I use the MSVC compiler with the same code, my application is able to write on serial port but is unable to read the data.

      Here a snippet of code where use the serial.

      qint64 pBytesWritten = serial->write(dataToSend);
      serial->flush();
      
      if (pBytesWritten == dataToSend.length()) {
              qDebug().nospace().noquote() << "Write ok";
          QThread::msleep(50);
          if (readMsg() == E_OK) {
              qDebug().nospace().noquote() << "Read ok";
          } else {
              qDebug().nospace().noquote() << "Read not ok";
          }
      

      readMsg function:

      bool readMsg(){
          bool res = serial->waitForReadyRead(ReceptionTimeout);
          QByteArray recBuff;
          if (res)
              recBuff = serial->readAll();
          .
          .
          .
          return res;
      }
      

      The output is always:

      Write ok
      Read not ok
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Turi said in Unable to read from QSerialPort using MSVC compiler:

      if (pBytesWritten == dataToSend.length()) {
      qDebug().nospace().noquote() << "Write ok";
      QThread::msleep(50);
      if (readMsg() == E_OK) {

      This is not how you should use QSerialPort.
      You should read from the port as soon as there is actually something to read.
      To do so connect a slot to https://doc.qt.io/qt-5/qiodevice.html#readyRead signal and do the reading there.

      If, for some reason, you want to do it in sequence, then call https://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead after writing and read afterwards.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      2
      • T Turi

        Hi,
        I'm developing an application to communicate via serial using master/slave architectures with an application running on Linux. My application is the master and runs on Windows 10 and the application on Linux is slave. I'm using the QSerialPort class to do this.
        My proble is: if I use the mingw compiler the communication works fine, but if I use the MSVC compiler with the same code, my application is able to write on serial port but is unable to read the data.

        Here a snippet of code where use the serial.

        qint64 pBytesWritten = serial->write(dataToSend);
        serial->flush();
        
        if (pBytesWritten == dataToSend.length()) {
                qDebug().nospace().noquote() << "Write ok";
            QThread::msleep(50);
            if (readMsg() == E_OK) {
                qDebug().nospace().noquote() << "Read ok";
            } else {
                qDebug().nospace().noquote() << "Read not ok";
            }
        

        readMsg function:

        bool readMsg(){
            bool res = serial->waitForReadyRead(ReceptionTimeout);
            QByteArray recBuff;
            if (res)
                recBuff = serial->readAll();
            .
            .
            .
            return res;
        }
        

        The output is always:

        Write ok
        Read not ok
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Turi for the love of my sanity, please use the provided signals:
        https://doc.qt.io/qt-5/qiodevice.html#signals

        bytesWritten and readyRead

        do not use flush, Qthread::sleep and waitForXXXX!

        also connect to the error signal errorOccured
        https://doc.qt.io/qt-5/qserialport.html#errorOccurred


        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
        2
        • jsulmJ jsulm

          @Turi said in Unable to read from QSerialPort using MSVC compiler:

          if (pBytesWritten == dataToSend.length()) {
          qDebug().nospace().noquote() << "Write ok";
          QThread::msleep(50);
          if (readMsg() == E_OK) {

          This is not how you should use QSerialPort.
          You should read from the port as soon as there is actually something to read.
          To do so connect a slot to https://doc.qt.io/qt-5/qiodevice.html#readyRead signal and do the reading there.

          If, for some reason, you want to do it in sequence, then call https://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead after writing and read afterwards.

          T Offline
          T Offline
          Turi
          wrote on last edited by
          #4

          @jsulm said in Unable to read from QSerialPort using MSVC compiler:

          afterwards

          I need to do it in sequence. I already use waitForReadyRead function to read from serial but this not work if I compile my application with MSVC.

          jsulmJ 1 Reply Last reply
          0
          • T Turi

            @jsulm said in Unable to read from QSerialPort using MSVC compiler:

            afterwards

            I need to do it in sequence. I already use waitForReadyRead function to read from serial but this not work if I compile my application with MSVC.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Turi said in Unable to read from QSerialPort using MSVC compiler:

            I already use waitForReadyRead

            Then QThread::msleep(50); is completely useless.

            Why do you need to do it in sequence?
            Qt is an event driven framework and you should use it as such instead of trying to force it to behave in a way it was not designed to work?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Turi said in Unable to read from QSerialPort using MSVC compiler:

              I already use waitForReadyRead

              Then QThread::msleep(50); is completely useless.

              Why do you need to do it in sequence?
              Qt is an event driven framework and you should use it as such instead of trying to force it to behave in a way it was not designed to work?

              T Offline
              T Offline
              Turi
              wrote on last edited by
              #6

              @jsulm
              I removed QThread::msleep(50) and added a connect to the readyRead signal but I'm still unable to read the serial, the connect is never called. Also with the connect, it is solicited if I use MinGW compiler but not with MSVC.

              jsulmJ 2 Replies Last reply
              0
              • T Turi

                @jsulm
                I removed QThread::msleep(50) and added a connect to the readyRead signal but I'm still unable to read the serial, the connect is never called. Also with the connect, it is solicited if I use MinGW compiler but not with MSVC.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Turi said in Unable to read from QSerialPort using MSVC compiler:

                the connect is never called. Also with the connect, it is solicited if I use MinGW compiler but not with MSVC.

                I don't understand this.
                Do you mean the connect(...) is never called? Then fix that.
                Or do you mean the slot is not called?
                Please show your connect call and also check its return value.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • T Turi

                  @jsulm
                  I removed QThread::msleep(50) and added a connect to the readyRead signal but I'm still unable to read the serial, the connect is never called. Also with the connect, it is solicited if I use MinGW compiler but not with MSVC.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Turi And also please do what @J-Hilk suggested.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Turi
                    wrote on last edited by Turi
                    #9

                    @jsulm
                    I have already followed the suggestions of @J-Hilk .
                    This is my connect:

                    bool ok = connect(serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
                    

                    After that the value of' ok' is true, but the SLOT is never called.

                    jsulmJ 1 Reply Last reply
                    0
                    • T Turi

                      @jsulm
                      I have already followed the suggestions of @J-Hilk .
                      This is my connect:

                      bool ok = connect(serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
                      

                      After that the value of' ok' is true, but the SLOT is never called.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @Turi said in Unable to read from QSerialPort using MSVC compiler:

                      I have already followed the suggestions of @J-Hilk

                      No, I was refering to this:
                      "also connect to the error signal errorOccured
                      https://doc.qt.io/qt-5/qserialport.html#errorOccurred"

                      And you should use the Qt5 connect syntax: https://doc.qt.io/qt-5/signalsandslots.html

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Turi said in Unable to read from QSerialPort using MSVC compiler:

                        I have already followed the suggestions of @J-Hilk

                        No, I was refering to this:
                        "also connect to the error signal errorOccured
                        https://doc.qt.io/qt-5/qserialport.html#errorOccurred"

                        And you should use the Qt5 connect syntax: https://doc.qt.io/qt-5/signalsandslots.html

                        T Offline
                        T Offline
                        Turi
                        wrote on last edited by
                        #11

                        @jsulm
                        I have already tried all this and all seem work fine, without any error. But my application still does not read from the serial only if I use the MSVC compiler.

                        jsulmJ J.HilkJ 2 Replies Last reply
                        0
                        • T Turi

                          @jsulm
                          I have already tried all this and all seem work fine, without any error. But my application still does not read from the serial only if I use the MSVC compiler.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Turi Did you verify that the other side sends something?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          T 1 Reply Last reply
                          0
                          • T Turi

                            @jsulm
                            I have already tried all this and all seem work fine, without any error. But my application still does not read from the serial only if I use the MSVC compiler.

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

                            @Turi what versions are we actually talking here ?

                            Qt(from the kit), msvc, windows etc.

                            and can you show more of your actually class that does the serial communication?


                            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.

                            T 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Turi Did you verify that the other side sends something?

                              T Offline
                              T Offline
                              Turi
                              wrote on last edited by
                              #14

                              @jsulm
                              Yes, the other side reveives and sends messages.

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

                                @Turi what versions are we actually talking here ?

                                Qt(from the kit), msvc, windows etc.

                                and can you show more of your actually class that does the serial communication?

                                T Offline
                                T Offline
                                Turi
                                wrote on last edited by
                                #15

                                @J-Hilk
                                I'm using:

                                • Qt Creator 4.9.0
                                • Based on Qt 5.12.2 (MSVC 2017, 32 bit)
                                • Windows 10 Enterprise

                                Unfortunately I can't show more of my actually class due to the fact that is reserved code.

                                jsulmJ 1 Reply Last reply
                                0
                                • T Turi

                                  @J-Hilk
                                  I'm using:

                                  • Qt Creator 4.9.0
                                  • Based on Qt 5.12.2 (MSVC 2017, 32 bit)
                                  • Windows 10 Enterprise

                                  Unfortunately I can't show more of my actually class due to the fact that is reserved code.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Turi said in Unable to read from QSerialPort using MSVC compiler:

                                  Based on Qt 5.12.2 (MSVC 2017, 32 bit)

                                  This is the Qt version which was used to build QtCreator.
                                  What Qt version do you use?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    Turi
                                    wrote on last edited by
                                    #17

                                    Qt 5.12.3
                                    MinGW 7.3.0 32-bit
                                    MSVC 2017 32-bit

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • T Turi

                                      Qt 5.12.3
                                      MinGW 7.3.0 32-bit
                                      MSVC 2017 32-bit

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Turi Can you try with 5.15.2?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      T 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @Turi Can you try with 5.15.2?

                                        T Offline
                                        T Offline
                                        Turi
                                        wrote on last edited by
                                        #19

                                        @jsulm
                                        Can you pass me a link where I can download this version?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          Turi
                                          wrote on last edited by
                                          #20

                                          I tried with Qt 5.12.2 but not work.

                                          jsulmJ 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