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. [SOLVED] Qserialport sending data when opened without action
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Qserialport sending data when opened without action

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 6.8k 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.
  • F Offline
    F Offline
    fred_douille
    wrote on last edited by
    #1

    Hello,

    I'm confused, I use Qserialport to controle an arduino card.
    With a simple code to open the port, I have some data sent automatically and I don't understand why??

    Here my code

    .h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtSerialPort/QSerialPort>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_open_clicked();
    void read();

    private:
    Ui::MainWindow *ui;
    QSerialPort *serial;
    };

    #endif // MAINWINDOW_H
    @

    and .cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial = new QSerialPort(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_open_clicked()
    {
    serial->setPortName("COM7");

    if( serial->open(QIODevice::ReadWrite))
    {
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
    
        connect(serial, SIGNAL(readyRead()), this, SLOT(read()));
    }   
    

    }

    void MainWindow::read()
    {
    QByteArray data = serial->readAll();

    ui->plainTextEdit->insertPlainText(data);

    }
    @

    When I lauch it, I receive 10 times "240" in my plaintextedit.
    I don't understand why they are sent.
    Have you an idea?

    thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The first idea would be that your arduino board is sending something. Does it also happen if you use a terminal application ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fred_douille
        wrote on last edited by
        #3

        Thank you for your answer.
        In the first place i used qextserialport and i did not have this problem.
        Also i changed the code from the arduino to read port and to send what was read to the computer, it is for that if there are datas in the plaintextedit. Arduino should only send what it received

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So the exact same code using QextSerialPort and QSerialPort does not behave in the same manner ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fred_douille
            wrote on last edited by
            #5

            Yes, I used the same code on arduino for QextSerialPort and QSerialPort.
            It's for that I don't understand what happened.
            And I think that both of them use Qiodevice?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Which version of QSerialPort and Qt are you using ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fred_douille
                wrote on last edited by
                #7

                I use Qt 5.1.1 and QSerialPort built-in

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  IIRC there's been some work on the QSerialPort module recently, can you test the 5.2 version to see whether it's still happening ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fred_douille
                    wrote on last edited by
                    #9

                    thanks, I am downloading the RC version.
                    I can't try this week end but I will on monday.
                    thank you and I'll tell you if it works

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kuzulis
                      Qt Champions 2020
                      wrote on last edited by
                      #10

                      2 fred_douille,

                      first, you can check it on the "standard" Terminal example from the QtSerialPort, because maybe you do something wrong.

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        fred_douille
                        wrote on last edited by
                        #11

                        In fact I used the terminal example from QtSerialPort. It is a very good help to explain how to use the library. But with it I had the problem, so I simplify the code to find where is the problem but in vain. I will try again with Qextserialport to be sure and maybe I should use an other arduino board. And I hope Qt 5.2 solved the problem

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fred_douille
                          wrote on last edited by
                          #12

                          Hi,

                          I just tried with Qt 5.2 RC and the problem is still there.
                          Now I'm trying again with QextSerialPort

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fred_douille
                            wrote on last edited by
                            #13

                            So, with QextSerialPort, no problem.
                            With QSerialPort I solved half the problem, if I open the port before setup the ui there is not data sent. But when I close my app, datas are sent.
                            It's look like there is a problem in the management of the port.
                            Maybe I have to put a specific flag when I open the port but I don't know wihch one.

                            Edit: In fact if no data are sent when I open the port with QSerialPort it's because I used QextSerialPort before.
                            It's look like the buffer is not empty, QextSerialPort empty it when opening and QserialPort isn't.

                            I think the difference is that QextSerialPort is not asynchronous while QSerialPort is.
                            And it's for that I can't use readyread signal with QextSerialPort.
                            I don't know what to do

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kuzulis
                              Qt Champions 2020
                              wrote on last edited by
                              #14

                              In this case you can use QSerialPort::clear() method after opening.

                              BTW: Also your provided code do a multiple signal/slot connections after multiple clicks to the "open" button. It can lead to some problems for you.

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fred_douille
                                wrote on last edited by
                                #15

                                I just tried QSerialPort::clear() but with no effect, very strange..

                                @serial->clear(QSerialPort::AllDirections);@

                                And now I changed my code, I open the port at the program launch without button, and close and reopen the program each time

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  kuzulis
                                  Qt Champions 2020
                                  wrote on last edited by
                                  #16

                                  You do clear() after opening? Please check a clear() return value.

                                  Need to do like:

                                  [code]
                                  ...
                                  bool ret = port->open();
                                  if (ret)
                                  ret = port->clear();
                                  if (ret) {
                                  // do something
                                  }
                                  [/code]

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    fred_douille
                                    wrote on last edited by
                                    #17

                                    I tried this:
                                    @
                                    if( serial->open(QIODevice::ReadWrite) )
                                    {
                                    if (serial->clear(QSerialPort::AllDirections))
                                    {
                                    QMessageBox::information(this,"test","cleared");
                                    }
                                    }
                                    @

                                    I have my QMessageBox saying "cleared" but still datas sent.

                                    1 Reply Last reply
                                    0
                                    • F Offline
                                      F Offline
                                      fred_douille
                                      wrote on last edited by
                                      #18

                                      I tried to open the port in ReadOnly mode and datas are still sent.
                                      It's so strange...

                                      With QextSerialPort I can't use ReadyRead signal, so I'm stuck.

                                      SOLVED!
                                      It was ridiculous. It's look like there is a problem in QSerialPort for baudrate at 115200.
                                      I adapted my code and my arduino to run at 9600 and it's fine.

                                      1 Reply Last reply
                                      0

                                      • Login

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