[solved] incomplete data read by qserialdevice
-
i think the event driven application im using is working the same as if it was on another thread and i think im aware of where my program is living while executing the problem is that sometimes the signal of the readyread event is raised before it truly ready i guess
-
You might be right. I saw an implementation with different threads for reading and writing on the device. Since I have used it as a guideline, I have implemented in a separate thread as well. It could well be over-engineering, but it does not have the problems on lower baud rates. At least I do not know of such problems ;-)
-
Hi arianoo even i am using qserialdevice for serial data communication.
I am connecting two devices on two diff ports one is running at 19200 baud and another at 4800 baud.
but i am facing a serious problem ie when one port is open and at the same time if i am opening the other port it is unable to open it.
I have taken two different instances of abstractserial class but i am unable to open both the ports at the same time..
if i close the second port and then i am opening the other port then i am able to read the data successfully..
i just caunt get where i am stuck. if you could guide me then i would be able to sort this problem out.. -
i am doing the same way also im openning two ports by two instances of qabstractserial and i have never had such problem maybe in some settings of your system there's a limitation for the number of open serial ports or maybe ur using the same name for both instances u create(???) or maybe something else is the problem because i never faced this issue u can also put a piece of ur program so that people on the forum see it maybe the answer will be found
-
@
#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include "qserialdevice_global.h"
#include "abstractserial.h"
QByteArray ba;
QByteArray ba1;
AbstractSerial *port;
AbstractSerial *port1;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{timer = new QTimer(this);
// timer1 = new QTimer(this);
udpsocket = new QUdpSocket(this);
messageNo = 1;
ui->setupUi(this);
connect(ui->sstartButton, SIGNAL(clicked()), this, SLOT(startserialrecv()));
connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
connect(ui->sstartButton_2, SIGNAL(clicked()),this, SLOT(ttlopen()));
connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->closebutton,SIGNAL(clicked()),this,SLOT(closeport()));
connect(ui->closebutton_2,SIGNAL(clicked()), this, SLOT(ttlclose()));
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
//connect(timer1, SIGNAL(timeout()), this, SLOT(startserialrecv()));}
void Dialog::startBroadcasting()
{
ui->startButton->setEnabled(false);
timer->start(100);
}void Dialog::startserialrecv()
{
port = new AbstractSerial(this);
connect( port, SIGNAL(readyRead()), this, SLOT(slotread()));
int rrto = 10;
int len = 100;
char *dn="/dev/ttyS0";
port->setDeviceName("/dev/ttyS0");
bool ret = port->open(AbstractSerial::ReadOnly);
if(ret)
{
qDebug("Port Opened Successfully");
if (!port->setBaudRate(AbstractSerial::BaudRate19200))
{
qDebug() << "Set baud rate " << AbstractSerial::BaudRate19200 << " error.";}; if (!port->setDataBits(AbstractSerial::DataBits8)) { qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error."; } if (!port->setParity(AbstractSerial::ParityEven)) { qDebug() << "Set parity " << AbstractSerial::ParityEven << " error."; } if (!port->setStopBits(AbstractSerial::StopBits1)) { qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error."; } if (!port->setFlowControl(AbstractSerial::FlowControlOff)) { qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error."; } if (port->waitForReadyRead(rrto)) { /* ba = port->readAll(); qDebug(ba); ui->textEdit1->setText(ba); ui->textEdit1->setText("Data Transmitted is: \"%1\""); qDebug(port->readAll()); */} else { qDebug() << "Timeout read data in time : " << QTime::currentTime(); } } else { qDebug("Serial Port Opening Failed"); }
}
void Dialog::slotread()
{
ba = port->readAll();
qDebug()<<"Data Rcvd :"<<ba;
ui->textEdit1->setText(ba);
qDebug() << "Readed is : " << ba.size() << " bytes";}
void Dialog::closeport()
{
port->close();
qDebug() << "Serial device " << port->deviceName() << " is closed";
delete port;
port = 0;
}
void Dialog::ttlopen()
{
port1 = new AbstractSerial(this);
connect( port1, SIGNAL(readyRead()), this, SLOT(slotttlread()));int rrto = 10; int len = 100; char *dn="/dev/ttyS1"; port1->setDeviceName("/dev/ttyS1");
// connect( port, SIGNAL(readyRead()), this, SLOT(readDataSlot()) );
//port->openMode(QIODevice::OpenMode); bool ret = port1->open(AbstractSerial::ReadOnly); if(ret) { qDebug("Port Opened Successfully"); //Here, the default current parameters (for example) if (!port1->setBaudRate(AbstractSerial::BaudRate115200)) { qDebug() << "Set baud rate " << AbstractSerial::BaudRate115200 << " error."; }; if (!port1->setDataBits(AbstractSerial::DataBits8)) { qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error."; } if (!port1->setParity(AbstractSerial::ParityNone)) { qDebug() << "Set parity " << AbstractSerial::ParityNone << " error."; } if (!port1->setStopBits(AbstractSerial::StopBits1)) { qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error."; } if (!port1->setFlowControl(AbstractSerial::FlowControlOff)) { qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error."; } /* if (port1->waitForReadyRead(rrto)) { ba = port->readAll(); qDebug(ba); ui->textEdit1->setText(ba); ui->textEdit1->setText("Data Transmitted is: \"%1\""); qDebug(port->readAll()); } else { qDebug() << "Timeout read data in time : " << QTime::currentTime(); }*/ } else { qDebug("Serial Port Opening Failed"); }
}
void Dialog::slotttlread()
{
ba1 = port1->readAll();
qDebug()<<"Data Rcvd :"<<ba1;
ui->textEdit1_2->setText(ba1);
qDebug() << "Readed is : " << ba1.size() << " bytes";
}void Dialog::ttlclose() { port1->close(); qDebug() << "Serial device " << port1->deviceName() << " is closed"; delete port1; port1 = 0; }
void Dialog::broadcastDatagram()
{QByteArray datagram = ba+ QByteArray::number(messageNo); // QByteArray datagram = ba+ "123456"; udpsocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 5000); ++messageNo; ui->textEdit->setText(tr("Sending data: \"%1\"").arg(datagram.data())); qDebug("Data is being transmitted");
}
@[EDIT: code formatting, please wrap in @-tags, Volker]
-
[quote author="abhijsj" date="1323510423"]Hey Arianoo i had already mailed a copy of my code to you you can check that out.
[/quote]You need to use '@' as tag for starting and ending a section with source code. Otherwise it is not readable (see "here":http://developer.qt.nokia.com/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 ) .
In addition it would have been better to start a new thread for your issue. This would attract more likely others to help you. Unfortunately, I cannot split threads.
-
@#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include "qserialdevice_global.h"
#include "abstractserial.h"
QByteArray ba;
QByteArray ba1;
AbstractSerial *port;
AbstractSerial *port1;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{timer = new QTimer(this);
// timer1 = new QTimer(this);
udpsocket = new QUdpSocket(this);
messageNo = 1;
ui->setupUi(this);
connect(ui->sstartButton, SIGNAL(clicked()), this, SLOT(startserialrecv()));
connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
connect(ui->sstartButton_2, SIGNAL(clicked()),this, SLOT(ttlopen()));
connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->closebutton,SIGNAL(clicked()),this,SLOT(closeport()));
connect(ui->closebutton_2,SIGNAL(clicked()), this, SLOT(ttlclose()));
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
//connect(timer1, SIGNAL(timeout()), this, SLOT(startserialrecv()));}
void Dialog::startBroadcasting()
{
ui->startButton->setEnabled(false);
timer->start(100);
}void Dialog::startserialrecv()
{
port = new AbstractSerial(this);
connect( port, SIGNAL(readyRead()), this, SLOT(slotread()));
int rrto = 10;
int len = 100;
char *dn="/dev/ttyS0";
port->setDeviceName("/dev/ttyS0");
bool ret = port->open(AbstractSerial::ReadOnly);
if(ret)
{
qDebug("Port Opened Successfully");
if (!port->setBaudRate(AbstractSerial::BaudRate19200))
{
qDebug() << "Set baud rate " << AbstractSerial::BaudRate19200 << " error.";}; if (!port->setDataBits(AbstractSerial::DataBits8)) { qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error."; } if (!port->setParity(AbstractSerial::ParityEven)) { qDebug() << "Set parity " << AbstractSerial::ParityEven << " error."; } if (!port->setStopBits(AbstractSerial::StopBits1)) { qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error."; } if (!port->setFlowControl(AbstractSerial::FlowControlOff)) { qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error."; } if (port->waitForReadyRead(rrto)) { /* ba = port->readAll(); qDebug(ba); ui->textEdit1->setText(ba); ui->textEdit1->setText("Data Transmitted is: \"%1\""); qDebug(port->readAll()); */} else { qDebug() << "Timeout read data in time : " << QTime::currentTime(); } } else { qDebug("Serial Port Opening Failed"); }
}
void Dialog::slotread()
{
ba = port->readAll();
qDebug()<<"Data Rcvd :"<<ba;
ui->textEdit1->setText(ba);
qDebug() << "Readed is : " << ba.size() << " bytes";}
void Dialog::closeport()
{
port->close();
qDebug() << "Serial device " << port->deviceName() << " is closed";
delete port;
port = 0;
}
void Dialog::ttlopen()
{
port1 = new AbstractSerial(this);
connect( port1, SIGNAL(readyRead()), this, SLOT(slotttlread()));int rrto = 10; int len = 100; char *dn="/dev/ttyS1"; port1->setDeviceName("/dev/ttyS1");
// connect( port, SIGNAL(readyRead()), this, SLOT(readDataSlot()) );
//port->openMode(QIODevice::OpenMode); bool ret1 = port1->open(AbstractSerial::ReadOnly); if(ret1) { qDebug("Port Opened Successfully"); //Here, the default current parameters (for example) if (!port1->setBaudRate(AbstractSerial::BaudRate115200)) { qDebug() << "Set baud rate " << AbstractSerial::BaudRate115200 << " error."; }; if (!port1->setDataBits(AbstractSerial::DataBits8)) { qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error."; } if (!port1->setParity(AbstractSerial::ParityNone)) { qDebug() << "Set parity " << AbstractSerial::ParityNone << " error."; } if (!port1->setStopBits(AbstractSerial::StopBits1)) { qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error."; } if (!port1->setFlowControl(AbstractSerial::FlowControlOff)) { qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error."; } /* if (port1->waitForReadyRead(rrto)) { ba = port->readAll(); qDebug(ba); ui->textEdit1->setText(ba); ui->textEdit1->setText("Data Transmitted is: \"%1\""); qDebug(port->readAll()); } else { qDebug() << "Timeout read data in time : " << QTime::currentTime(); }*/ } else { qDebug("Serial Port Opening Failed"); }
}
void Dialog::slotttlread()
{
ba1 = port1->readAll();
qDebug()<<"Data Rcvd :"<<ba1;
ui->textEdit1_2->setText(ba1);
qDebug() << "Readed is : " << ba1.size() << " bytes";
}void Dialog::ttlclose() { port1->close(); qDebug() << "Serial device " << port1->deviceName() << " is closed"; delete port1; port1 = 0; }
void Dialog::broadcastDatagram()
{QByteArray datagram = ba+ QByteArray::number(messageNo); // QByteArray datagram = ba+ "123456"; udpsocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 5000); ++messageNo; ui->textEdit->setText(tr("Sending data: \"%1\"").arg(datagram.data())); qDebug("Data is being transmitted");
}@
-
There has been a response in another "thread concerning QSerialDevice":http://developer.qt.nokia.com/forums/viewthread/11634/#67246
-
hi abhijsj
ive looked at your code and im not sure what wxactly makes your problem but there are somethings i can mention:@ if (port->waitForReadyRead(rrto))
{
/* ba = port->readAll();
qDebug(ba);
ui->textEdit1->setText(ba);
ui->textEdit1->setText("Data Transmitted is: "%1"");
qDebug(port->readAll());
*/} @first of all i think this part on line 78 should be ommited.
also i think theres no need to lines 11-15 in your main.cpp code.
another point is, its better to put line 114 after opening your port in line 119 of the code.
try this and tell the result and ill explore with more attention in your code maybe i can find the problem. -
now there's a question i have:
how did u use the close() in a method different from the one that created port?
I mean i can only access port1->close() if im in the slot that i created port = new abstractserial(); i dont know how u got access to that from another method im so keen to know it plz answer me because i get segmentation fault when calling close() from another method in the same class ??? -
Thnx for replyin ...
I declared AbstractSerial *port globally in line 8. because of which i am able to close it in different slot.
I used another slot to close the port because i need the port to be always in open state because data is pumped on port every 100msecs so it couldnt be closed.
I want to ask that can two ports be in open state at same time and whether they would be able to receive data at the same time at diff baud rates.
Solution to my problem lies in this question.
I am a newbie and had very little idea about serial ports. -
dear abhijsj
thank u so much u really helped me. my problem was that I created the ports in the constructor of my classes and so i couldn't get access to the close slot anywhere else in the same class but now that i create the port and open it outside of the constructor, everything works fine with closing the port:)
about your question, it's absolutely possible to communicate with two different ports at the same time so easily. the program I'm working on now is also doing this and i have never faced any problem with having two ports opened and i think your program can also do it correctly if you pay attention to some points.
as my experience says,if u press sstartButton or sstartButton2 two times your port wont get opened again i handled this problem by opening it the third time if it isnt opened yet as this:
@port = new AbstractSerial(); port->setDeviceName("/dev/ttyS1"); port->open(AbstractSerial::ReadWrite); if (!port->openMode()) port->open(AbstractSerial::ReadWrite ) }
@
this way you will get sure each time you call the slot to open the port your port will be opened.
next i think its a bit important that you first creat the port, then assign a name to it, then assign its properties and at last connect its signal to a slot. i think u should be careful about the order(im not sure).
I cant find any other problem that prevents u from getting data from both ports at the same time because i'm doing the same thing and my two ports work simultanously and i havent seen any problem in it. u can also debug your program and say where it doesnt respond correctly. -
Use QExtSerialPort, download it and just build the library and link it to your application
Heres how I read the serial port instead of events (which can get hairy when parsing a lot of data)
Start a while loop
Set and start a timer for X milliseconds so when it fires, it changes the state of the while loop variable so you can exit the while loop
Check for data on the port by using bytesavailable()
Sit in while loop for a little while and keep reading the port until no more data is available or until a delimiter is reached
use QCoreApplication::processEvents() while in the while loop so the gui doesn't freeze upBoom, timer fires, you delayed X mS to get all the data from where ever
Carry on with what you want to do.
-
What u say about events making problem is quite correct ive even seen some cases in my own application but the method you are talking about makes lots of time loss which is very important in my case and i cant ignore it. i should write the program in a way that it works as fast as possible and i guess that i can achieve it via signals and events but maybe i'm wrong and using a while loop when needed to read data till its complete is better? Im getting curious about which way will make the program work faster???????
-
I guess it depends, do you know the size of the data that your expecting? Is there a delimiter on your string that you are expecting through the serial? You could do some simple timing tests to optimize your program.
Either that, or when you get an event that data is on your port, then sit in a while and read, and repeat my previous suggestion.
-
We strongly recommend using QSerialDevice 2.0.
People, you have ever read that thread "link":http://developer.qt.nokia.com/forums/viewthread/11634/#67246, which showed by Mr. koahnig?
I recommend to read.
-
yea I'm using QSerialDevice and i hope to get better in working with this class because 90% of my application is about working with serial ports and its so important for me. dvez yes i can find the size of coming data by its third byte coming (if i get the first third bytes exactly as theyre sent) and i can stay in the while loop till i get as much data as i need and i can also call it when i send something and im waiting to get answer.do u think it'll work faster and ill be able to read data without getting it damaged?