Qextserialport not reading data fully
-
wrote on 6 Dec 2013, 07:18 last edited by
Hi frnds i am new to qt and trying to read data from serial port in qt on rhel linux . The incomming data is 15 byte data and sent at every 1s from the sender device. When i am reading this data i get 7 byets some times 4bytes and so. But i should get the 15byte data.
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include <QTimer>
#include <QtNetwork>char databuff[1024];
QextSerialPort *port;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
port = new QextSerialPort("ttyS0", QextSerialPort::EventDriven);
port->open(QIODevice::ReadWrite);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setQueryMode(QextSerialPort::EventDriven);
connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));// QTimer *timer = new QTimer(this);
//connect(timer, SIGNAL(timeout()), this, SLOT(onDataAvailable()));
//timer->start(200);
}void MainWindow::onDataAvailable()
{
port->read(databuff,15);
ui->lineEdit->setText(databuff);
}@i have also used timer but same problem persists. Pls suggest how to solve this problem. Thanking u all in advance
-
the readyRead() signal should be emitted multiple times whenever new data is available.
So in your onDataAvailable() slot jsut append the read data. It's not guaranteed that every time readyRead() is emitted all data is available.btw. in Qt5 there is already a serial port module available.
-
wrote on 6 Dec 2013, 09:02 last edited by
hi worx can u please explain a little bit more the line " So in your onDataAvailable() slot jsut append the read data". What exactly i have to do please explain.
-
wrote on 6 Dec 2013, 09:02 last edited by
[quote author="raven-worx" date="1386316484"]the readyRead() signal should be emitted multiple times whenever new data is available.
So in your onDataAvailable() slot jsut append the read data. It's not guaranteed that every time readyRead() is emitted all data is available.btw. in Qt5 there is already a serial port module available.[/quote]
hi worx can u please explain a little bit more the line “ So in your onDataAvailable() slot jsut append the read data”. What exactly i have to do please explain.
-
something like this:
@
ui->lineEdit->setText( ui->lineEdit->text().append(databuff) );
@
Should result in the full data you expect at the end once all the 15byte data has been sent.Or this:
@
void MainWindow::onDataAvailable()
{
if( port->bytesAvailable() >= 15 )
{
port->read(databuff,15);
ui->lineEdit->setText(databuff);
}
}
@
To ensure you read the data in 15-byte-steps only -
wrote on 6 Dec 2013, 10:49 last edited by
[quote author="raven-worx" date="1386320632"]something like this:
@
ui->lineEdit->setText( ui->lineEdit->text().append(databuff) );
@
Should result in the full data you expect at the end once all the 15byte data has been sent.Or this:
@Hi worx
void MainWindow::onDataAvailable()
{
if( port->bytesAvailable() >= 15 )
{
port->read(databuff,15);
ui->lineEdit->setText(databuff);
}
}
@
To ensure you read the data in 15-byte-steps only[/quote]Hi worx than u very much for ur reply . Now i am getting the correct data
which is better than earlier.
1/6