過去にこのライブラリを Qt4 で使用したことがありますが、現在、より多くの受信データを含むプロジェクトで使用しようとしています。プログラムで正しい COM ポートに接続しようとすると、パケットが受信されていません。別のターミナル プログラムを使用すると、一定のデータ フローが表示されます。COM ポートへの接続を何度か試みた後、私のプログラムは最終的に接続して正しく動作します。コマンドが実行されたときに、プログラムが一貫して COM ポートに接続できるようにする必要があります。私のコードの何が問題なのかについて何か考えがある人がいれば、本当に助けていただければ幸いです。
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
void MainWindow::comportSelected()
{
// If some other serial port is open then close it.
if(serial.isOpen())
serial.close();
if(ui->comportList->currentIndex() != 0)
{
serial.setDataBits(QSerialPort::Data8);
serial.setFlowControl(QSerialPort::NoFlowControl);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setParity(QSerialPort::NoParity);
serial.setPort(comPortList.at(ui->comportList->currentIndex()-1));
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::critical(this, tr("Error"), serial.errorString());
ui->console->setEnabled(false);
}
else
{
connect((const QObject*)&serial, SIGNAL(readyRead()), this, SLOT(processPendingSerialData()));
}
}
else serial.close();
}
私はそれから次のように読みます:
void MainWindow::processPendingSerialData()
{
// While there are bytes in the buffer.
while(serial.bytesAvailable() > 0)
{
// Read a byte.
serial.read((char *)&ch, 1);
等...