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

QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 970 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.
  • M Offline
    M Offline
    mvsri
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

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

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      2
      • M mvsri

        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.

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

        @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?

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

        M 1 Reply Last reply
        2
        • jsulmJ jsulm

          @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?

          M Offline
          M Offline
          mvsri
          wrote on last edited by
          #4

          @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.

          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • M mvsri

            @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.

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

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

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

            M 1 Reply Last reply
            1
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              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
              2
              • M mvsri

                @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.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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 😄


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • jsulmJ jsulm

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

                  M Offline
                  M Offline
                  mvsri
                  wrote on last edited by
                  #8

                  @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.

                  aha_1980A 1 Reply Last reply
                  0
                  • M mvsri

                    @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.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      mvsri
                      wrote on last edited by
                      #10

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

                      Thank you for the help!

                      aha_1980A M 2 Replies Last reply
                      2
                      • M mvsri

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

                        Thank you for the help!

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @mvsri

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

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1
                        • M mvsri

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

                          Thank you for the help!

                          M Offline
                          M Offline
                          Marco Flad
                          wrote on last edited by
                          #12

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

                          aha_1980A 1 Reply Last reply
                          0
                          • M Marco Flad

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

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Marco-Flad

                            Please see here: https://doc.qt.io/qt-5/signalsandslots.html

                            Qt has to stay free or it will die.

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved