QSerialPort
-
Hey there,
I have been working with the QSerialPort, Here is the codemainwindow.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. -
@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 Outputvoid 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.
-
@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.
-
Then please comment all the code within
MainWindow::on_pushButton_write_clicked()
and just add aqDebug() << "on_pushButton_write_clicked";
in it.If you click the button once, you should see that message also one time. Does that work?
Regards
-
Please see here: https://doc.qt.io/qt-5/signalsandslots.html