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. serialport read not working(no errors)
Qt 6.11 is out! See what's new in the release blog

serialport read not working(no errors)

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 1.2k 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.
  • E Emre MUTLU

    @agmar

    serial.setPortName(portName); // replace with the name of the serial port
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    if (serial.open(QIODevice::ReadWrite)) {
    qDebug()<<"port Open";
    } else {
    qDebug()<<"Port OpenError";
    }
    if(serial.waitForBytesWritten(2000)){//it waits to bytes written
        while(serial.waitForReadyRead(1000)){//it waits to read
                   const QByteArray data = serial.readAll();
                 }
    }
    
    SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @agmar Please use proper coding tags around all your code snippets.

    That said, you do not show how you initialize nor configure m_serial.

    With the code you posted, you are likely not using m_serial at all, or are trying to access the same device twice which should fail.

    Qt is an asynchronous framework, you should first learn how it works especially with regards to serial ports.

    Please take the time to properly read and understand the links @jsulm provided.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    A 1 Reply Last reply
    0
    • E Emre MUTLU

      @agmar

      serial.setPortName(portName); // replace with the name of the serial port
      serial.setBaudRate(QSerialPort::Baud19200);
      serial.setDataBits(QSerialPort::Data8);
      serial.setParity(QSerialPort::NoParity);
      serial.setStopBits(QSerialPort::OneStop);
      serial.setFlowControl(QSerialPort::NoFlowControl);
      if (serial.open(QIODevice::ReadWrite)) {
      qDebug()<<"port Open";
      } else {
      qDebug()<<"Port OpenError";
      }
      if(serial.waitForBytesWritten(2000)){//it waits to bytes written
          while(serial.waitForReadyRead(1000)){//it waits to read
                     const QByteArray data = serial.readAll();
                   }
      }
      
      A Offline
      A Offline
      agmar
      wrote on last edited by
      #5

      @Emre-MUTLU from my understanding, this is blocking code isnt it? this is not optimal and usable in a real time application if that's the case.
      I did read in the documentation that readAll() would allow asynchronous operation, is there anyway to rewrite that to provide async operation?

      JonBJ 1 Reply Last reply
      0
      • A agmar

        @Emre-MUTLU from my understanding, this is blocking code isnt it? this is not optimal and usable in a real time application if that's the case.
        I did read in the documentation that readAll() would allow asynchronous operation, is there anyway to rewrite that to provide async operation?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #6

        @agmar Yes, follow the links @jsulm gave you!

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @agmar said in serialport read not working(no errors):

          if (serial.open(QIODevice::ReadWrite)) {
          const QByteArray data = serial.readAll();

          Please read documentation.
          This simply can't work. Calling readAll does not mean that data was already received.
          Proper way to implement is to use signals slots: connect a slot to https://doc.qt.io/qt-6/qiodevice.html#readyRead signal of your QSerialPort instance and call readAll there.
          There are also examples showing how to do it: https://doc.qt.io/qt-6/qtserialport-examples.html

          A Offline
          A Offline
          agmar
          wrote on last edited by
          #7

          @jsulm

          @jsulm I have an MCU connected to to the serial port that sends back what i received(tested,works) , and i did read the documentation, but its only useful if you already know how QT works and i know about the terminal and enumerator ,blocking sender and receiver examples, they're not clear to me...

          this is the whole function:

          void MainWindow::sendMessage()sd
          {
          QSerialPort serial;
          serial.setPortName(portNsame); // replace with the name of the serial port
          serial.setBaudRate(QSerialPort::Baud19200);
          serial.setDataBits(QSerialPort::Data8);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop);
          serial.setFlowControl(QSerialPort::NoFlowControl);
          if (serial.open(QIODevice::ReadWrite)) {
          QString message = ui->lineEdit->text();
          std::cout << message.toStdString() << std::endl;
          QByteArray messageData = message.toUtf8();
          std::cout << messageData.toStdString() << std::endl;
          serial.write(message.toUtf8());
          serial.waitForBytesWritten(-1);
          serial.close();

          }

          serial.setPortName(portName); // replace with the name of the serial port
          serial.setBaudRate(QSerialPort::Baud19200);
          serial.setDataBits(QSerialPort::Data8);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop);
          serial.setFlowControl(QSerialPort::NoFlowControl);
          if (serial.open(QIODevice::ReadWrite)) {

          const QByteArray data = serial.readAll();
          lol = data;
          ui->labTest2->setText(lol);
          QString resultat = "data received";

          std::cout << resultat.toStdString() << std::endl;
          std::cout << lol.toStdString() << std::endl;

          }else {

          QString erri = "we have a problem here";
          std::cout << erri.toStdString() << std::endl;

          }
          }

          is the
          connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
          not valid?

          JonBJ 1 Reply Last reply
          0
          • A agmar

            @jsulm

            @jsulm I have an MCU connected to to the serial port that sends back what i received(tested,works) , and i did read the documentation, but its only useful if you already know how QT works and i know about the terminal and enumerator ,blocking sender and receiver examples, they're not clear to me...

            this is the whole function:

            void MainWindow::sendMessage()sd
            {
            QSerialPort serial;
            serial.setPortName(portNsame); // replace with the name of the serial port
            serial.setBaudRate(QSerialPort::Baud19200);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop);
            serial.setFlowControl(QSerialPort::NoFlowControl);
            if (serial.open(QIODevice::ReadWrite)) {
            QString message = ui->lineEdit->text();
            std::cout << message.toStdString() << std::endl;
            QByteArray messageData = message.toUtf8();
            std::cout << messageData.toStdString() << std::endl;
            serial.write(message.toUtf8());
            serial.waitForBytesWritten(-1);
            serial.close();

            }

            serial.setPortName(portName); // replace with the name of the serial port
            serial.setBaudRate(QSerialPort::Baud19200);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop);
            serial.setFlowControl(QSerialPort::NoFlowControl);
            if (serial.open(QIODevice::ReadWrite)) {

            const QByteArray data = serial.readAll();
            lol = data;
            ui->labTest2->setText(lol);
            QString resultat = "data received";

            std::cout << resultat.toStdString() << std::endl;
            std::cout << lol.toStdString() << std::endl;

            }else {

            QString erri = "we have a problem here";
            std::cout << erri.toStdString() << std::endl;

            }
            }

            is the
            connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
            not valid?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #8

            @agmar
            The connect() looks right [edit, No, see end of my post?]. But not the

            if (serial.open(QIODevice::ReadWrite)) {
            
            const QByteArray data = serial.readAll();
            

            All readAll()s belong in the MainWindow::readData() slot. And don't forget that can/will be called multiple times, and each time there is a only a guarantee that at least 1 byte is ready to read, not more than that. So you may have to buffer the received data.

            As a separate issue. You seem to have one QSerialPort serial; local variable in sendMessage(), and some other serial used outside that. You open and close serial port for each read or write?? You are supposed to have one serial port open across a number of/all read/writes.

            Furthermore, that actually seems to make

            connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
            

            wrong, what is m_serial here?? Elsewhere you use serial. I don't know what you are up to. Your application should (presumably) have just one instance of QSerialPort, with its settings performed and opened only once.

            1 Reply Last reply
            1
            • SGaistS SGaist

              @agmar Please use proper coding tags around all your code snippets.

              That said, you do not show how you initialize nor configure m_serial.

              With the code you posted, you are likely not using m_serial at all, or are trying to access the same device twice which should fail.

              Qt is an asynchronous framework, you should first learn how it works especially with regards to serial ports.

              Please take the time to properly read and understand the links @jsulm provided.

              A Offline
              A Offline
              agmar
              wrote on last edited by
              #9

              @SGaist How do i use proper coding tags all around my code snippets? i have no idea how to do it...

              JonBJ 1 Reply Last reply
              0
              • A agmar

                @SGaist How do i use proper coding tags all around my code snippets? i have no idea how to do it...

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #10

                @agmar
                When you type a post here you have a "toolbar" of icons above where you type, right? The one which looks like </> has a tooltip of Code and puts tags around whatever you have selected, or put lines of just 3 backtick characters ``` before & after your snippet.

                A 1 Reply Last reply
                1
                • JonBJ JonB

                  @agmar
                  When you type a post here you have a "toolbar" of icons above where you type, right? The one which looks like </> has a tooltip of Code and puts tags around whatever you have selected, or put lines of just 3 backtick characters ``` before & after your snippet.

                  A Offline
                  A Offline
                  agmar
                  wrote on last edited by
                  #11

                  @JonB
                  I tried making it into serial, but this is what the compiler kept throwing at me, and thanks for the info earlier!
                  45eb14e9-0453-4a80-9d00-d71242cb094b-image.png

                  JonBJ 1 Reply Last reply
                  0
                  • A agmar

                    @JonB
                    I tried making it into serial, but this is what the compiler kept throwing at me, and thanks for the info earlier!
                    45eb14e9-0453-4a80-9d00-d71242cb094b-image.png

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #12

                    @agmar
                    I don't know what to say. This is straightforward C++ stuff. You are not supposed to just change things without understanding.

                    Your code seems to have at least 3 QSerialPort declarations as far as I can see, one for m_serial and 2 for serial. I don't know why you would want any more than just one. Whatever you need to understand and sort out your code.

                    A 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @agmar
                      I don't know what to say. This is straightforward C++ stuff. You are not supposed to just change things without understanding.

                      Your code seems to have at least 3 QSerialPort declarations as far as I can see, one for m_serial and 2 for serial. I don't know why you would want any more than just one. Whatever you need to understand and sort out your code.

                      A Offline
                      A Offline
                      agmar
                      wrote on last edited by
                      #13

                      @JonB
                      I know, but to understand and learn you need to change, and if i need to understand everything to change anything, how will i ever be able to understand in the first place(catch22)?

                      As for m_serial, that was used in the terminal example and just 'serial' in the other, this code is made from pieces of example code i found, too bad its not commented...

                      JonBJ 1 Reply Last reply
                      0
                      • A agmar

                        @JonB
                        I know, but to understand and learn you need to change, and if i need to understand everything to change anything, how will i ever be able to understand in the first place(catch22)?

                        As for m_serial, that was used in the terminal example and just 'serial' in the other, this code is made from pieces of example code i found, too bad its not commented...

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #14

                        @agmar
                        My guess is you would just use the m_serial everywhere, like I said I don't think you will want to have any more that one QSerialPort instance.

                        Sorry, but if you take bits of code from different places you have to understand how it works to get it right.

                        1 Reply Last reply
                        2

                        • Login

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