Solved QSerialPort::readyRead not responding
-
Hello,
I am trying to implement Serial COM port program.
The program is not responding to readyread event (I hope my understanding is not wrong).Here is the code:
/* in .h */ QSerialPort *serialData = nullptr; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), serialData(new QSerialPort(this)) { connect(ui->button_connect, &QPushButton::clicked, this, &MainWindow::openSerialPort); connect(serialData, &QSerialPort::errorOccurred, this, &MainWindow::handleCOMError); connect(serialData, &QSerialPort::readyRead, this, &MainWindow::readSerialData); }
Open Serial port:
void MainWindow::openSerialPort() { serialData = new QSerialPort(); serialData->setPortName(ui->combo_commPort->currentText()); serialData->setBaudRate(static_cast<QSerialPort::BaudRate>(baudRates[ui->combo_BaudRate->currentIndex()].baudRate)); serialData->setDataBits(static_cast<QSerialPort::DataBits>(dataBits[ui->combo_DataBits->currentIndex()].dataBits)); serialData->setParity(static_cast<QSerialPort::Parity>(parity[ui->combo_Parity->currentIndex()].Parity)); serialData->setStopBits(static_cast<QSerialPort::StopBits>(stopBits[ui->combo_StopBits->currentIndex()].StopBits)); serialData->setFlowControl(static_cast<QSerialPort::FlowControl>(flowControl[ui->combo_FlowControl->currentIndex()].FlowControl)); if (serialData->open(QIODevice::ReadWrite)) { ui->button_connect->setDisabled(true); ui->tabWidget->setEnabled(true); } else { QMessageBox::critical(this, tr("Error"), serialData->errorString()); } }
This code is not triggered:
void MainWindow::readSerialData() { const QByteArray data = serialData->readAll(); ui->textEdit_recv->append(data); }
To write data on COM :
void MainWindow::on_button_send_clicked() { QByteArray str = ui->textEdit_send->toPlainText().toUtf8(); serialData->write(str, str.length()); }
Please let me know a way to resolve this problem. Any pointers would be appreciated.
-- CKP
Note: I wrote most of this program by copying it from an example program - "Terminal Example"
-
Where do you open the port?
-
I have updated the question with the code snippet that opens the COM port
-
@PradeepCK You connect signals/slots before you create the QSerialPort instance?!
This can't work... -
Lo and behold - the code works now.
Silly me,
Thank you for pointing this out. You are a genius.-- CKP