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. readyRead serial will NOT work properly

readyRead serial will NOT work properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k 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.
  • K Offline
    K Offline
    khhhhhhh
    wrote on last edited by khhhhhhh
    #1
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QCoreApplication>
    #include<QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        serialPort= new QSerialPort(this);
        serialPort->setPortName("COM3");
        serialPort->setBaudRate(QSerialPort::Baud9600);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setParity(QSerialPort::NoParity);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
    
        if (serialPort->open(QIODevice::ReadOnly)) {
    
            qInfo() << "GOOD";
    
        } else {
    
            qInfo() << "BAD";
    
        }
    
        connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead);
    
    }
    
    void MainWindow::handleReadyRead() {
    
        qInfo() << "GOT SOMETHING";
    
    }
    
    MainWindow::~MainWindow()
    {
        serialPort->disconnect();
        delete ui;
    }
    

    I've been at this for hours, it just refuses to receive anything. Both putty and a different serial reading application both can see the data I'm spamming on the COM3 serial port from my ATMega, but QT just refuses to see it. It does sometimes work, like every 20th try, and then it goes back to not responding. I'll then open putty and it's reading all of the info fine.

    if I unplug my ATMega board, or use any COM port not in use, it outputs "bad", if I plug it back in and use COM3, it says "good" (every time) so it's obviously detecting it. Am I doing something wrong to get these unreliable results? Using the latest version 5 release.

    If this info helps: every time it works properly (very rare) and I exit the application, it will just hang and stop responding.

    J.HilkJ 1 Reply Last reply
    0
    • K khhhhhhh
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QCoreApplication>
      #include<QDebug>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          serialPort= new QSerialPort(this);
          serialPort->setPortName("COM3");
          serialPort->setBaudRate(QSerialPort::Baud9600);
          serialPort->setDataBits(QSerialPort::Data8);
          serialPort->setParity(QSerialPort::NoParity);
          serialPort->setStopBits(QSerialPort::OneStop);
          serialPort->setFlowControl(QSerialPort::NoFlowControl);
      
          if (serialPort->open(QIODevice::ReadOnly)) {
      
              qInfo() << "GOOD";
      
          } else {
      
              qInfo() << "BAD";
      
          }
      
          connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead);
      
      }
      
      void MainWindow::handleReadyRead() {
      
          qInfo() << "GOT SOMETHING";
      
      }
      
      MainWindow::~MainWindow()
      {
          serialPort->disconnect();
          delete ui;
      }
      

      I've been at this for hours, it just refuses to receive anything. Both putty and a different serial reading application both can see the data I'm spamming on the COM3 serial port from my ATMega, but QT just refuses to see it. It does sometimes work, like every 20th try, and then it goes back to not responding. I'll then open putty and it's reading all of the info fine.

      if I unplug my ATMega board, or use any COM port not in use, it outputs "bad", if I plug it back in and use COM3, it says "good" (every time) so it's obviously detecting it. Am I doing something wrong to get these unreliable results? Using the latest version 5 release.

      If this info helps: every time it works properly (very rare) and I exit the application, it will just hang and stop responding.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @khhhhhhh and welcome.

      first step I would suggest is connection to the error/errorOccured signal of QSerialPort and see if that helps identifying the issue, you should also store the serial port pointer, so you can get the actual errorString instead of only the enum, that the signal provides.

      Make sure to do the connect call before you start setting your Connection Parameters


      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.

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

        hi @khhhhhhh and welcome.

        first step I would suggest is connection to the error/errorOccured signal of QSerialPort and see if that helps identifying the issue, you should also store the serial port pointer, so you can get the actual errorString instead of only the enum, that the signal provides.

        Make sure to do the connect call before you start setting your Connection Parameters

        K Offline
        K Offline
        khhhhhhh
        wrote on last edited by khhhhhhh
        #3

        @J-Hilk

        I'm getting closer to having it work, however when it does work, the application fails to quit properly when I click the X button, and just hangs, forcing me to terminate studio as well.

        J.HilkJ 1 Reply Last reply
        0
        • K khhhhhhh

          @J-Hilk

          I'm getting closer to having it work, however when it does work, the application fails to quit properly when I click the X button, and just hangs, forcing me to terminate studio as well.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @khhhhhhh

          the data I'm spamming on the COM3 serial port from my ATMega

          what interval between sends are we talking here ?


          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.

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

            @khhhhhhh

            the data I'm spamming on the COM3 serial port from my ATMega

            what interval between sends are we talking here ?

            K Offline
            K Offline
            khhhhhhh
            wrote on last edited by
            #5

            @J-Hilk

            The atmega chip is just writing to serial as fast as possible. I'm just trying to get this working properly. Putty and the other applications don't crash.

            As for QT not reading the messages, it looks like (I could be wrong) that it was just garbage characters that QT wasn't seeing or was just disregarding altogether as garbage. Since then I've fixed that so they're proper english characters and it looks like it's receiving properly now (again could be wrong) but now the main issue is it's just crashing as soon as I quit.

            I would reply faster but I'm limited every 600 seconds due to me being a new user.

            J.HilkJ 1 Reply Last reply
            1
            • K khhhhhhh

              @J-Hilk

              The atmega chip is just writing to serial as fast as possible. I'm just trying to get this working properly. Putty and the other applications don't crash.

              As for QT not reading the messages, it looks like (I could be wrong) that it was just garbage characters that QT wasn't seeing or was just disregarding altogether as garbage. Since then I've fixed that so they're proper english characters and it looks like it's receiving properly now (again could be wrong) but now the main issue is it's just crashing as soon as I quit.

              I would reply faster but I'm limited every 600 seconds due to me being a new user.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @khhhhhhh said in readyRead serial will NOT work properly:

              I would reply faster but I'm limited every 600 seconds due to me being a new user.

              the upvote should have fixed that.

              Try to delay the sending.

              QSerialport is rather high-level API it has to go through all kinds of OS/dll and Qt Layers until your code is executed. Can be that the buffer of the comport is flooded and hundert of thousands signals are emitted (possibly internal of QSerialPort) and therefore compromises the functionality.


              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.

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

                @khhhhhhh said in readyRead serial will NOT work properly:

                I would reply faster but I'm limited every 600 seconds due to me being a new user.

                the upvote should have fixed that.

                Try to delay the sending.

                QSerialport is rather high-level API it has to go through all kinds of OS/dll and Qt Layers until your code is executed. Can be that the buffer of the comport is flooded and hundert of thousands signals are emitted (possibly internal of QSerialPort) and therefore compromises the functionality.

                K Offline
                K Offline
                khhhhhhh
                wrote on last edited by khhhhhhh
                #7

                @J-Hilk

                Yep added a 1000ms delay on the chip and that seemed to fix the crashing. Microchip studio and QT Creator may also be conflicting with each other at times since they're both trying to work with the same com port.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  What happens if your read the data that actually arrived on the port in your slot ?

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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    khhhhhhh
                    wrote on last edited by khhhhhhh
                    #9

                    So it looks like the error never went away. If I unplug my microchip, and plug it back in, QT cannot read the serial port no matter how many times I restart the application. HOWEVER, if I start Putty and start listening in on that port (reads flawlessly in putty) and then I exit putty and start my QT application again, then QT is able to read from the COM port. Very weird bug.

                    Edit: Looks like I was missing two lines:

                            serialPort->setRequestToSend(true);
                            serialPort->setDataTerminalReady(true);
                    
                    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