PDA

View Full Version : recieve continious data from serial port



ready
29th March 2011, 16:26
I have a problem while reading data from microcontroller...my microcontroller sends char 'a' continiously(just to test)..and I want to receive the character in my ui application..(lineedit)...

QextSerialPort * port = new QextSerialPort("COM4", QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadWrite);
port->setTimeout(500);
port->setRts(true);
port->setDtr(true);
if(port->isReadable())
{
char buff[1024];
qint64 i=port->readLine(buff,1024);

buff[i]='\0';
QString str(buff);
if(i!=-1)
{
ui->lineEdit->setText(str);;
port->close();
}
}

else
qDebug("port not open");
this does not help me ............please help me/...

high_flyer
29th March 2011, 17:02
this does not help me
^- this doesn't help me to help you.

jdiewald
29th March 2011, 19:39
Even if you get the read working, ask yourself what happens to your buffer if readLine returns -1. Specifically, what is buff[i] going to do? It can't be good.

squidge
29th March 2011, 20:09
Have you verified that your micro is even sending the data correctly using something like a terminal emulator?

After that, you have a possible buffer underrun, and you don't need the null character anyway, as the method you are calling adds it for you.

Last, you are running the serial port in event driven mode and then attempting to poll it. Doomed to failure.

ready
31st March 2011, 10:58
Yes i have checked data from microcontroller .............but could not recieve in my ui application ....:(
please help me..

high_flyer
31st March 2011, 11:21
How do you know that "COM4" is the correct port?
Use the port enumeration static functions in QextSerialPort to extract the available ports, and be sure you are reading the port you micro is connected to.

squidge
31st March 2011, 14:20
Yes i have checked data from microcontroller .............but could not recieve in my ui application ....:(
please help me..

Use signals and slots, then the event loop will run.

ready
31st March 2011, 19:37
Use signals and slots, then the event loop will run.
can you please elaborate it...

squidge
31st March 2011, 20:44
Sure, http://doc.qt.nokia.com/4.7/signalsandslots.html

BTW, it seems like you are attempting to copy and paste chunks of code. This will not work, you have to understand the code first.

wysota
1st April 2011, 01:53
In addition to what was already said you are trying to read a line of data and a line is characterised by the fact that it ends with a newline character. Since your peer sends you only 'a' characters, you don't have a newline character and hence you don't have a line. readLine() probably will work and simply return all pending data but it's good to avoid bad habits.

ready
2nd April 2011, 09:58
finally my lineedit showed some character after I pressed the button .....but it read different character......for example If I send "b" from microcontroller......it reads as 'd',again after pressing the button it reads 'h' and again after pressing button it reads't'.....why this is happening so??

wysota
2nd April 2011, 10:06
Do you send and receive at the same speed and all the other port settings?