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.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 :-(
}
*/
//QByteArray ba = serial.readLine(100);
cout<<"[" << *ba << "]"<<endl;
cout<< ba.size() << endl;
}
int main (int argc, char *argv[])
{
InitialiseSerialPort();
for(int i=0; i<2000; i++)
{
Write();
ReadData();
}
return 0;
}
#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;
}
To copy to clipboard, switch view to plain text mode
There comes no data into the QByteArray, but why?
Bookmarks