QSerialPort open
-
@
#include <QtSerialPort/QSerialPort>#include <QApplication>
#include <QtCore/QTime>
#include <QtCore/QTextStream>int main(int argc, char *argv[])
{
// QApplication app(argc, argv);QTextStream cout(stdout); QTextStream cin(stdin); QSerialPort serial; QString portName = "ttyUSB0"; serial.setPortName(portName); if (!serial.open(QIODevice::ReadWrite)) { cout << QObject::tr("Failed to open port %1, error: %2") .arg(portName) .arg(serial.errorString()) << endl; return 1; } if (!serial.setBaudRate(QSerialPort::Baud115200)) { cout << QObject::tr("Can't set baud rate 9600 baud to port %1, error code %2") .arg(portName).arg(serial.error()); return 1; } if (!serial.setDataBits(QSerialPort::Data8)) { cout << QObject::tr("Can't set 8 data bits to port %1, error code %2") .arg(portName).arg(serial.error()); return 1; } if (!serial.setParity(QSerialPort::NoParity)) { cout << QObject::tr("Can't set no parity to port %1, error code %2") .arg(portName).arg(serial.error()); return 1; } if (!serial.setStopBits(QSerialPort::OneStop)) { cout << QObject::tr("Can't set 1 stop bit to port %1, error code %2") .arg(portName).arg(serial.error()); return 1; } if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { cout << QObject::tr("Can't set no flow control to port %1, error code %2") .arg(portName).arg(serial.error()); return 1; } QByteArray requestData("ls"); requestData.append(QLatin1String("\x0A")); // write request serial.write(requestData); if (serial.waitForBytesWritten(5000)) { //! [8] //! [10] // read response if (serial.waitForReadyRead(5000)) { QByteArray responseData = serial.readAll(); while (serial.waitForReadyRead(10)) responseData += serial.readAll(); QString response(responseData); //! [12] cout << response << endl; //! [10] //! [11] //! [12] } else { cout << QObject::tr("Wait read response timeout %1") .arg(QTime::currentTime().toString()); } //! [9] //! [11] } else { cout << QObject::tr("Wait write request timeout %1") .arg(QTime::currentTime().toString()); } return 0;
}
@When i run the program, it appears like this:
QApplication::qAppName: Please instantiate the QApplication object firstHow can i do ?
-
first uncomment line 1 in your main() ;)
on the return do app.exec() instead of returning 0Also you need to move the implementation of your serial port to an object which lives in the Qt event loop. If you use a gui simply add it into a widget. If not just use an QObject which does the work instead doing all in the main function.
-
i tried, but failed. Could U give a simple example ? Thank you
-
i tried, but failed. Could U give a simple example ? Thank you
[quote author="raven-worx" date="1386321238"]first uncomment line 1 in your main() ;)
on the return do app.exec() instead of returning 0Also you need to move the implementation of your serial port to an object which lives in the Qt event loop. If you use a gui simply add it into a widget. If not just use an QObject which does the work instead doing all in the main function.[/quote]
-
Hi,
What did fail ?
Since you are not using any GUI element you can use a QCoreApplication
-
BTW: This is not the QSerialPort issue.. :)
-
Does it work without Qt event loop environment?
[quote author="SGaist" date="1386331898"]Hi,What did fail ?
Since you are not using any GUI element you can use a QCoreApplication[/quote]
-
it works without an event loop when you only use the blocking methods (all waitXXX() methods).
Maybe in your case it's enough to just instantiate the Q(Core)Application? -
yes, you are right.
[quote author="SGaist" date="1386331898"]Hi,What did fail ?
Since you are not using any GUI element you can use a QCoreApplication[/quote]
-
Atfer 2 places modified, it's ok now. But i've a new problem.
The following is the response, and the first line "ls" is the sent data.
Now i don't want the first line to be displayed, then how?ls
bin lib mnt root tmp
dev linuxrc opt sbin usr
etc lost+found proc sys[quote author="raven-worx" date="1386339040"]it works without an event loop when you only use the blocking methods (all waitXXX() methods).
Maybe in your case it's enough to just instantiate the Q(Core)Application?[/quote] -
Isn't that your device that echoes what it has received ?
-
Ah, maybe, i'll check
[quote author="SGaist" date="1386578714"]Isn't that your device that echoes what it has received ?[/quote]