Qserialport- data access problems
-
Hi,
I'm trying to receive continuous serial data (numbers 0 till 9 in a loop) from microcontroller using Beaglebone Black. When ReadyRead signal comes, it should append the data to the file (serial.txt).
Issues:
1) The data displayed on Application output on my PC (QDebug) shows
@"0"
"1"
"2"
"3"
"4"@
etc. But I want it to be shown in one line, like 0123456... What am I doing wrong?2) Sometimes two or more numbers appear in the same line, like this:
@"0"
"12"
"3"@
Why is it so?3) After receiving the data for 4-5 seconds, QT stops updating the data on the console (QDebug). Is the buffer getting full?
4) QSerialPort accepts data only if I deploy the GUI application after starting transmission from the microcontroller. It does not accept data if the GUI application deployed first. What could be the reason?
main.cpp
@#include "mainwindow.h"
#include <QApplication>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QTextStream>void MainWindow::OpenFile (QString mFilename)
{
mFile.setFileName(mFilename);
if(!mFile.open( QFile::Append | QFile::Text))
{
qDebug() << "Could not open file for writing";
return;
}
}void MainWindow::Write (QString inp)
{
QTextStream out(&mFile);
out << inp << "\n";
}int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}@
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qserialport.h"
#include <QSerialPort>
#include <QDebug>
QSerialPort *serial;MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
bool status = false;
serial = new QSerialPort(this);
serial->setPortName("ttyO0");
status = serial->open(QIODevice::ReadWrite);
if(status){
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
QString mFilename = "/home/root/serial.txt";
OpenFile (mFilename);
connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
}
else{
qDebug() << "Could not open serial port";
}
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::serialReceived()
{
QByteArray bytearray = serial->readAll();
QString input = QString(bytearray);
Write(input);
qDebug() << bytearray;
}@ -
--
-
Hi all,
Please go through the first post of this thread. Looking forward to your replies.
Thank you. -
I modified the serialReceived() function to print the no of bytes received as follows:
@void MainWindow::serialReceived()
{
qDebug() << serial->bytesAvailable();
int ExpectedNoOfBytes = 3;
if (ExpectedNoOfBytes == serial->bytesAvailable()){
QByteArray bytearray = serial->readAll();
QString input = QString(bytearray);
Write(input);
qDebug() << bytearray;
}
}@The output is follows:
@512
1024
1536
2048
2560
3072
3584
3856
3857
3859
3860
3861 ..... @Initially it is in multiples of 512 and then it becomes one byte each. Looks like the if statement is not getting executed at all. Can someone tell what is happening here?