PDA

View Full Version : Using QtSerialPort in a console application



Brixus
22nd March 2014, 17:19
Hello,
first I am sorry for my english. I am not a native speaker.

I am a beginner in Qt and I try to use QtSerialPort in a console application.

I tried to use the following code:


#include <QCoreApplication>
#include <QSerialPort>
#include <QDebug>
QSerialPort serial;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

serial.setPortName("/dev/ttyUSB0");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);

serial.write("-helloworld-");
serial.close();

while (1)
{
if (serial.bytesAvailable()>0||serial.waitForReadyRead(10))
{
QByteArray ba;
ba=serial.readAll();
serial.write(ba);
qDebug()<<ba;
}
}
serial.close();
return a.exec();
}


In the open console window is written "Press <Return> to close this window..." and nothing more.
The application output window in Qt tells me that the programm crashed but I dont know why and I also get no errors or warnings!

If I try to use the Gui example of QtSerialPort named "terminal" everything woks fine.
Therefore I can be sure that the access rights are ok and the hardware is working also with Qt.

I also have testet another Gui application using the QtSerialPort and everything worked fine.

But why is it not working in my console application?


Can anybody help me?

Thank you very much :-)

anda_skoa
22nd March 2014, 18:18
One thing that looks weird is that you close() after writing but then try to read again from the same object.

Cheers,
_

Brixus
22nd March 2014, 18:35
Thank you very much.

How embarrassing...

I deleted the upper "serial.close" and now everything works fine :)