Feature #777 » maintenance_qt.cpp
| 1 |
#include <QCoreApplication>
|
|---|---|
| 2 |
#include <QSerialPort>
|
| 3 |
#include <QTimer>
|
| 4 |
#include <QDebug>
|
| 5 |
|
| 6 |
int main(int argc, char *argv[]) |
| 7 |
{
|
| 8 |
QCoreApplication app(argc, argv); |
| 9 |
|
| 10 |
QSerialPort serial; |
| 11 |
serial.setPortName("/dev/ttyUSB0"); |
| 12 |
serial.setBaudRate(QSerialPort::Baud115200); |
| 13 |
serial.setDataBits(QSerialPort::Data8); |
| 14 |
serial.setParity(QSerialPort::NoParity); |
| 15 |
serial.setStopBits(QSerialPort::OneStop); |
| 16 |
serial.setFlowControl(QSerialPort::NoFlowControl); |
| 17 |
|
| 18 |
// Nur zum Schreiben öffnen
|
| 19 |
if (!serial.open(QIODevice::WriteOnly)) { |
| 20 |
qCritical() << "Konnte Port nicht öffnen:" << serial.errorString(); |
| 21 |
return 1; |
| 22 |
}
|
| 23 |
|
| 24 |
qInfo() << "Port geöffnet. Sende Daten..."; |
| 25 |
|
| 26 |
// Timer sendet alle 2 Sekunden einen Befehl
|
| 27 |
QTimer timer; |
| 28 |
QObject::connect(&timer, &QTimer::timeout, [&]() { |
| 29 |
QByteArray cmd = "STATUS?\r\n"; |
| 30 |
qint64 written = serial.write(cmd); |
| 31 |
|
| 32 |
if (written == -1) { |
| 33 |
qCritical() << "Fehler beim Schreiben:" << serial.errorString(); |
| 34 |
} else { |
| 35 |
qInfo() << "Gesendet:" << cmd.trimmed(); |
| 36 |
}
|
| 37 |
});
|
| 38 |
|
| 39 |
timer.start(2000); |
| 40 |
|
| 41 |
return app.exec(); |
| 42 |
}
|