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. Problem with QSerialPort::readyRead signal

Problem with QSerialPort::readyRead signal

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 312 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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi at all,
    I've a problem with QSerialPort and readyRead signal.
    Seems that this signal is called one time, only when connect signal/slot function is executed.
    I placed a breakpoint in this slot but when I receive data from serial port this slot is not called.
    I've seen that this problem was in Qt 5.12.5~5.13.0 but I use 5.15.2 and I haven't seen any reports about this version.
    This is my code:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtSerialPort/QSerialPort>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
    
    private:
        QSerialPort *Reader = nullptr;
        QByteArray  *BarCodeBuffer = nullptr;
    
    private slots:
        void    sltReadData (void);
    
    };
    #endif // MAINWINDOW_H
    
    
    #include <QDebug>
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        BarCodeBuffer = new QByteArray;
    
        Reader = new QSerialPort;
        Reader->setPortName("COM9");
        Reader->setBaudRate(QSerialPort::Baud115200);
        Reader->setDataBits(QSerialPort::Data8);
        Reader->setParity(QSerialPort::NoParity);
        Reader->setStopBits(QSerialPort::OneStop);
        Reader->setFlowControl(QSerialPort::NoFlowControl);
        qDebug() << Reader->open(QIODevice::ReadWrite);
    
        if(!Reader->isOpen())
        {
            qDebug() << "Error com9";
        }
        else
        {
            connect(this->Reader, SIGNAL(readyRead()), this, SLOT(sltReadData()));
            Reader->write(QByteArray("R")); //Send reset to BarCode Reader
        }
    }
    
    
    void MainWindow::sltReadData(void)
    {
        //Waiting serial data
        if(Reader->bytesAvailable() == 0)
        {
            qDebug() << Reader->bytesAvailable();
            return;
        }
    
        BarCodeBuffer->append(Reader->readAll());
    }
    
    
    

    Any suggestion?
    Thanks.
    Best regards.

    Stefano

    Christian EhrlicherC 1 Reply Last reply
    0
    • S Stefanoxjx

      Hi at all,
      I've a problem with QSerialPort and readyRead signal.
      Seems that this signal is called one time, only when connect signal/slot function is executed.
      I placed a breakpoint in this slot but when I receive data from serial port this slot is not called.
      I've seen that this problem was in Qt 5.12.5~5.13.0 but I use 5.15.2 and I haven't seen any reports about this version.
      This is my code:

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QtSerialPort/QSerialPort>
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
      
      private:
          QSerialPort *Reader = nullptr;
          QByteArray  *BarCodeBuffer = nullptr;
      
      private slots:
          void    sltReadData (void);
      
      };
      #endif // MAINWINDOW_H
      
      
      #include <QDebug>
      #include "mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
      {
          BarCodeBuffer = new QByteArray;
      
          Reader = new QSerialPort;
          Reader->setPortName("COM9");
          Reader->setBaudRate(QSerialPort::Baud115200);
          Reader->setDataBits(QSerialPort::Data8);
          Reader->setParity(QSerialPort::NoParity);
          Reader->setStopBits(QSerialPort::OneStop);
          Reader->setFlowControl(QSerialPort::NoFlowControl);
          qDebug() << Reader->open(QIODevice::ReadWrite);
      
          if(!Reader->isOpen())
          {
              qDebug() << "Error com9";
          }
          else
          {
              connect(this->Reader, SIGNAL(readyRead()), this, SLOT(sltReadData()));
              Reader->write(QByteArray("R")); //Send reset to BarCode Reader
          }
      }
      
      
      void MainWindow::sltReadData(void)
      {
          //Waiting serial data
          if(Reader->bytesAvailable() == 0)
          {
              qDebug() << Reader->bytesAvailable();
              return;
          }
      
          BarCodeBuffer->append(Reader->readAll());
      }
      
      
      

      Any suggestion?
      Thanks.
      Best regards.

      Stefano

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Stefanoxjx said in Problem with QSerialPort::readyRead signal:

      Seems that this signal is called one time, only when connect signal/slot function is executed.

      QObject::connect() does not call the slot. It's only called when the signal is emitted. So put a breakpoint in your slot and take a look at the backtrace from where/if it is emitted

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

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by
        #3

        Hi Christian,
        I explained bad :(
        I know that QObject::connect doesn't emit any signal, I wrote this because I saw that the slot is called only one time, immediately after to have connect the signal to the slot.
        I already tried to place a breakpoint in my slot

        I placed a breakpoint in this slot but when I receive data from serial port this slot is not called.
        

        but after first time, it isn't called anymore.

        Stefano

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

          Hi,

          Might be a silly question but are you sure your device sends more data than what you receive after you sent the reset command ?

          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
          4
          • S Offline
            S Offline
            Stefanoxjx
            wrote on last edited by
            #5

            Hi SGaist, you're a Genius :)
            The problem is the bad command sent to device.

            Many thanks for help.

            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