Reading from serial Port
-
wrote on 13 May 2014, 09:41 last edited by
Hi friends i am trying to develope an application which reads data from serial port and display. Actually my data is in a format like STXss:mm:hh:od:os\r\r\r\r\r\r\rr\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\rr\ ie every data frame contains start of Text them hou ,min and second information and lastly carrieage returns padded in such a way that it becomes 285 bytes in total. Now this frame is emitted by a device every one second. I want to read the data and display HOUR ,MIN and SECOND in three different TextBox or LineEdit in Qt.
The problem is that till now i have not been able to read the data completely ie some time i get half frame some time full . Some time HOUR text box displays the minute information and so on. I have read the data using the timer of i sec and also using the event based reading none of them display that data correctly. I am using qt5 like code given below
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include <QTimer>char databuff[1024];
QextSerialPort *port;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);port = new QextSerialPort("ttyS0"); port->open(QIODevice::ReadWrite); // port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
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(1000);
}void MainWindow::onDataAvailable()
{port->read(databuff,15); QString str(databuff[10]);
ui->lineEdit->setText( ui->lineEdit->text().append(databuff) );
}MainWindow::~MainWindow()
{
delete ui;
port->close();
}
@please suggest any solution. Is there any way such that i can start reading when STX arrives and end reading when \r\r\r arrives. Thank u all in advance
Edit: please mark your code as code in future, Gerolf
-
You need to align the data yourself. Scan the results you are getting, then take STX as the beginning and read further bytes.
-
wrote on 13 May 2014, 14:54 last edited by
[quote author="sierdzio" date="1399980456"]You need to align the data yourself. Scan the results you are getting, then take STX as the beginning and read further bytes.[/quote]
Hi sierdzio thank u very much for ur reply . can u please explain using some code.?
-
Take a chunk of data (read(), readAll(), etc.), and do this:
@
QString chunk = serialPort.readAll();int beginning = chunk.indexOf("STX");
chunk = chunk.mid(beginning + 3, 13); // This will cut only your hard data form the chunkint seconds = chunk.left(2).toInt(); // this will give you seconds
// etc.
@ -
wrote on 16 May 2014, 10:02 last edited by
[quote author="sierdzio" date="1399993854"]Take a chunk of data (read(), readAll(), etc.), and do this:
@
QString chunk = serialPort.readAll();int beginning = chunk.indexOf("STX");
chunk = chunk.mid(beginning + 3, 13); // This will cut only your hard data form the chunkint seconds = chunk.left(2).toInt(); // this will give you seconds
// etc.
@[/quote]Hi sierdzio thank u for ur reply. As i tried above code id did not work because the first character in the incoming string is not Exactly "STX" but it a symbol(face like) whose decimal value is ==1.
No can u please give me the modified code so that i can read the data correctly. Thank u in advance -
Please do it yourself. I do not know the exact binary structure of your packets, and I don't have any testing rig to make sure my solution will work. What I gave you is an idea on how to - possibly - make it work; the rest of the job is yours.
-
wrote on 16 May 2014, 10:39 last edited by
Just a little correction: STX has the decimal value 2 (not 1) it's one of the "ASCII ctrl characters":http://www.asciitable.com/
1/7