QSerialPort : I couldn't get all array (/w arduino)
-
Hi guys!
I've just started to learn qt. I try to communication qt and arduino. My code:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSerialPort> #include <QDebug> QSerialPort *serial; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); serial = new QSerialPort(this); serial->setPortName("COM3"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadOnly); // serial->write("ok*"); connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())); } MainWindow::~MainWindow() { delete ui; serial->close(); } void MainWindow::serialReceived(){ QByteArray ba; // serial->waitForReadyRead(1000); ba=serial->readAll(); // if(ba.count()>=4) ui->label->setText(ba); qDebug()<<ba; }
I couldn't get more than 4 letter. My sentence is "Hello from arduino". but i get:
"Ardu" "ino" "Hel" "lo " "From" " Ard" "uino" "He" "llo " "From" " Ard" "uino" "H" "ello" " Fro" "m Ar" "duin" "o"
When i write 4 letter still i can't get all array. What can i do ?
-
Hi, you'll get all the array if you wait long enough. Because the speed of the connection is only about 1000 characters per second, and "Hello from arduino" is 18 characters long, so try wait for say 20 milliseconds before doing the serial->readAll().
Or you can look for a stopping character, if you transmit "Hello from arduinoX" you can wait for the "X" to appear.
-
@hskoglund said in QSerialPort : I couldn't get all array (/w arduino):
Hi, you'll get all the array if you wait long enough. Because the speed of the c
Thank you. 1000ms is very long for this program what i understand. If i write 20ms program executed correctly. But i can't write in label now.
-
Instead of waiting you could try the peek function, it allows you to look at the received characters without taking them, for example:
void MainWindow::serialReceived(){ QByteArray ba; // serial->waitForReadyRead(1000); if (serial->peek(18).count() < 18) return; ba=serial->readAll(); // if(ba.count()>=4) ui->label->setText(ba); qDebug()<<ba; }
-
Now i want two things. But i can't do it. My Code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSerialPort> #include <QDebug> #include <QString> QSerialPort *serial; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); serial = new QSerialPort(this); serial->setPortName("COM3"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadWrite); // serial->write("Hello from the hell"); // serial->waitForBytesWritten(10); connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())); } MainWindow::~MainWindow() { delete ui; serial->close(); } void MainWindow::on_pushButton_clicked() { // QString text = ui->lineEdit->text(); serial->write("Hello from arduino"); // ui->label->setText(text); serial->waitForBytesWritten(50); } void MainWindow::serialReceived(){ QByteArray ba; serial->waitForReadyRead(50); // if(serial->peek(18).count()<18) ba=serial->readAll(); ui->label->setText(ba); qDebug()<<ba; } Output: . "Hello from arduino" "" "" "" ""
My arduino code sending back what i sending it.
Problem 1 : When i pressed the button i take some spaces like my output . So my text box show space.
Problem 2: I want to what i write in text box when i pressed the button sending this text. i write this code but i take some errors. i cant using this format.
QString text = ui->lineEdit->text(); serial->write(text); serial->waitForBytesWritten(50);
-
Hi,
- I don't understand your description
write
takes a QByteArray so you can usetext.toLatin1()
for example.
-
Actually, my problem : arduino is sending blank lines. So i can't get my sentence.
-
@lordumuzca
this feels like I answered this before oO:Asynchronous approach, because why would one want to block ones thread!?
readyReadSlot{ QByteArray ba,baRead; while(serial->canReadLine()){ baRead = serial->readLine(); if(baRead.size() > 0/*1?*/) ba.append(baRead); } qDebug() << "All read" << ba; }