PDA

View Full Version : Example how to receive Stings from SerialPort?



Brixus
19th October 2014, 14:49
Hello,

I would like to know, how I can receive strings from QtSerialPort.

I tried to find an example in the internet, but I didn't find one.

I know how to configure a Port and how to send, but with receiving I have no idea.

How can I receive strings?
Can I check every couple of seconds whether there is something new?

All in all I just need to receive something like "start" and put then my variable bool start=0; to start = 1;.

How can I do this?


Thank you very much :-)

ChrisW67
19th October 2014, 21:01
QSerialPort is a QIODevice so the readyRead() signal and the readAll() function would be a good place start.

http://qt-project.org/doc/qt-5/qtserialport-terminal-example.html

Brixus
19th October 2014, 22:52
Thank you very much :-)

As far I understand the com port has a small buffer and if there is data available I can check this with the readyRead(); function, right?
But this is from the type void, so how can I do this?

I have connected Rxd and Txd on my serial adapter so that all sent data is sent back.

This is my try:

#include <iostream>
#include <QCoreApplication>
#include <QApplication>
#include <QSerialPort>

using namespace std;

QSerialPort serial;


void InitialiseSerialPort()
{
serial.setPortName("/dev/ttyUSB0");
serial.open(QIODevice::WriteOnly);
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
}


void Write()
{ static int i=0;
char buffer [33];
sprintf(buffer, "%d", i);
serial.write(buffer);
serial.flush();
i++;
}

void ReadData()
{
/* if(serial.readyRead()==true)
{
//How can I check whether bytes are available?
//Sadly readyRead is void and not bool :-(
}
*/
static QByteArray ba = serial.readAll();
//QByteArray ba = serial.readLine(100);
cout<<"[" << *ba << "]"<<endl;
cout<< ba.size() << endl;
}


int main (int argc, char *argv[])
{
QApplication app(argc, argv);
InitialiseSerialPort();

for(int i=0; i<2000; i++)
{
Write();
ReadData();
}

return 0;
}


There comes no data into the QByteArray, but why?

Lesiok
20th October 2014, 07:15
QSerialPort has a method waitForReadyRead.

Brixus
20th October 2014, 08:27
Thanks,

in my last post there is a small mistake.
Of course it must be serial.open(QIODevice::ReadWrite); instead of ReadOnly.

Now I use:


void ReadData()
{
if(serial.waitForReadyRead(20))
{
static QByteArray ba = serial.readAll();
//QByteArray ba = serial.readLine(100);
cout<<"[" << *ba << "]"<<endl;
cout<< ba.size() << endl;
}
}

But there is still not data comming into the Array :-(

Why is it?

cszawisza
20th October 2014, 08:40
wwhy do you print pointer to ba instead ba? change it to


static QByteArray ba;
ba.append( serial.readAll()); // or clear and append or something else....
cout<<"[" << ba << "]"<<endl;
cout<< ba.size() << endl;


And tell how it works ;)

Brixus
20th October 2014, 09:54
Thank you very much :-)

Your solution to fill the array works well.

For myself I discovered a better solution:


void ReadData()
{
if(serial.waitForReadyRead(10))
{
char buffer[1024];
qint64 lineLength = serial.readLine(buffer,sizeof(buffer));
cout << buffer << endl;

}
}


Thanks :-)