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. QSerialPort::readyRead not responding
Qt 6.11 is out! See what's new in the release blog

QSerialPort::readyRead not responding

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

    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"

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Where do you open the port?

      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
      0
      • P Offline
        P Offline
        PradeepCK
        wrote on last edited by
        #3

        I have updated the question with the code snippet that opens the COM port

        jsulmJ 1 Reply Last reply
        0
        • P PradeepCK

          I have updated the question with the code snippet that opens the COM port

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @PradeepCK You connect signals/slots before you create the QSerialPort instance?!
          This can't work...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • P Offline
            P Offline
            PradeepCK
            wrote on last edited by
            #5

            Lo and behold - the code works now.

            Silly me,
            Thank you for pointing this out. You are a genius.

            -- CKP

            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