これは必ずしもそれ自体で別のライブラリを必要とするわけではありませんが、そのようなユース ケースではQtSerialPortとQwtを組み合わせるのが一般的です。
基本原則は、非同期読み取りを使用することです。固定期間で内部的にタイマーを実行し、指定した間隔ごとに「ストリップ チャート」の次の部分を描画できます。
利用可能なデータがなくなるなど、特定の条件が満たされるまでこれを行うことができます。QtSerialPort を使用しているかどうかについては言及していませんが、Qt プロジェクトで使用することはおそらく理にかなっていますが、これはほとんど接線です。
非同期リーダーの例では、QtSerialPort で以下のようなコードを書くことができます。アイデアは、グラフィカル ウィジェットに定期的に追加することです。
SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
: QObject(parent)
, m_serialPort(serialPort)
, m_standardOutput(stdout)
{
connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));
m_timer.start(5000);
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
// *** This will display the next part of the strip chart ***
// *** Optionally make the use of a plotting library here as 'Qwt' ***
myWidget.append('=');
if (!m_timer.isActive())
m_timer.start(5000);
}
void SerialPortReader::handleTimeout()
{
if (m_readData.isEmpty()) {
m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
} else {
m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
m_standardOutput << m_readData << endl;
}
QCoreApplication::quit();
}
void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError == QSerialPort::ReadError) {
m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
QCoreApplication::exit(1);
}
}