[SOLVED] Qserialport sending data when opened without action
-
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_OBJECTpublic:
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
-
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 ?
-
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 -
So the exact same code using QextSerialPort and QSerialPort does not behave in the same manner ?
-
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? -
Which version of QSerialPort and Qt are you using ?
-
I use Qt 5.1.1 and QSerialPort built-in
-
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 ?
-
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 -
2 fred_douille,
first, you can check it on the "standard" Terminal example from the QtSerialPort, because maybe you do something wrong.
-
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
-
Hi,
I just tried with Qt 5.2 RC and the problem is still there.
Now I'm trying again with QextSerialPort -
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 -
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.
-
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
-
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] -
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.
-
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.