Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort



  • Hey there,
    I have been working with the QSerialPort, Here is the code

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_connect_clicked()
    {
        serial = new QSerialPort(this);
        connect(serial, SIGNAL(readyRead()), this, SLOT(serialRead()));
        serial->setPortName("COM7");
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
    }
    
    void MainWindow::on_pushButton_write_clicked()
    {
        QString Result = ui->lineEdit->text();
        QString request = "\x02"+QString("003")+ QString("%1").arg(Result.toInt(),7,10,QChar('0')) +"\r";
        const QByteArray requestData = request.toUtf8();
        serial->write(requestData);
        qInfo() << "Data Write";
    }
    
    void MainWindow::serialRead()
    {
        qInfo() << "Serial Read Data: " << serial->readAll();
    }
    
    

    and mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    
    #include <QSerialport>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pushButton_connect_clicked();
    
        void on_pushButton_write_clicked();
    
        void serialRead();
    
    private:
        Ui::MainWindow *ui;
        QSerialPort *serial;
    
    };
    
    #endif // MAINWINDOW_H
    
    

    The code us working fine, the only problem is when i send something using serial->write(), it loop's itself in the Pushbutton_write() function.
    Is there any way i can use serial->write() only once when i click the pushbutton.



  • I'd suggest throwing out to qDebug content of request and requestData.


  • Lifetime Qt Champion

    @mvsri said in QSerialPort:

    it loop's itself in the Pushbutton_write() function

    That's not clear. Can you show what exactly you're doing there?



  • @jsulm
    i meant once i clicked on the pushbutton_write, i get continuous qInfo() in Application Output

    void MainWindow::on_pushButton_write_clicked()
    {
        QString Result = ui->lineEdit->text();
        QString request = "\x02"+QString("003")+ QString("%1").arg(Result.toInt(),7,10,QChar('0')) +"\r";
        const QByteArray requestData = request.toUtf8();
        serial->write(requestData);
        qInfo() << "Data Write:  " << requestData;
    }
    

    According to the logic of the above code when i press PushButton the code should write the data and print the data only once.

    Although what happens is as soon as i press PushButton the whole on_pushButton_write_clicked() function goes into loop(like repeating the pushbutton fucntion itself)by sending write data using serial->write() and printing the qInfo() in Application Output.


  • Lifetime Qt Champion

    @mvsri Maybe you connected on_pushButton_write_clicked() to some other signal?


  • Lifetime Qt Champion

    @mvsri said in QSerialPort:

    "\x02"+QString("003")

    Since \x02 is no valid QString char I would not use QString here in the first place. You will get problems sooner or later with this.


  • Moderators

    @mvsri I would try to define the slot by hand and explicitly call the QObject::connect to connect signal and slot.

    The connectSlotByName meta-method is known to behave not exactly consistent 😄



  • @jsulm NO i haven't connected it to any other signal, infact if you could check the mainwindow.cpp you can see that i haven't connected it.


  • Lifetime Qt Champion

    @mvsri

    Then please comment all the code within MainWindow::on_pushButton_write_clicked() and just add a qDebug() << "on_pushButton_write_clicked"; in it.

    If you click the button once, you should see that message also one time. Does that work?

    Regards



  • i used QObject::connect to connect signal and slot and it solved the problem.

    Thank you for the help!


  • Lifetime Qt Champion

    @mvsri

    Glad you figured it out. So please mark this topic as SOLVED too. Thanks!



  • @mvsri please can you paste the code or explain more how QObject::connect .


  • Lifetime Qt Champion


Log in to reply