Problem with QSerialPort::readyRead signal
-
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
-
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
@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
-
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 slotI 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
-
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 ?
-
Hi SGaist, you're a Genius :)
The problem is the bad command sent to device.Many thanks for help.