私のアプリケーションは、シリアル ポート (USB シリアル) 経由で組み込みシステムと通信しています。
私は Qt 4.8 と gitorious ( https://qt.gitorious.org/qt/qtserialport/ ) の QtSerialPort で開発しており、Windows 7 と Qt 5.2.1 の VM でテストしています。
Linux では問題なく動作しますが、同じメッセージを 2 回、3 回、4 回読み取ります。これらのメッセージが一度だけ送信され、puTTY 経由で通信していることを確認できます。
問題は、QtSerialPort、VM (パテで動作しますが)、シリアルドライバーなどの問題ですか?
これは既知の問題ですか? (しかし、何も見つかりませんでした)これを突き止める方法はありますか?
これが私が読む方法です:
編集: moar コード:
ModuleCommunicator::ModuleCommunicator(const QString &device, SBStateModel &state_model)
: upstate(UpdateDone), model(state_model)
{
port = new QSerialPort(device);
if (!port->open(QIODevice::ReadWrite /*| QIODevice::Truncate*/)) {
qDebug() << "Failed to open - Portname: " << port->portName() << endl;
qDebug() << "Error: " << port->errorString() << endl;
return;
}
port->setBaudRate(QSerialPort::Baud115200, QSerialPort::AllDirections);
port->setParity(QSerialPort::NoParity);
port->setStopBits(QSerialPort::OneStop);
port->setDataBits(QSerialPort::Data8);
port->setFlowControl(QSerialPort::NoFlowControl);
msgBuffer = new QStringList();
log_init = false;
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onError(QSerialPort::SerialPortError)));
connect(port, SIGNAL(aboutToClose()), this, SLOT(devClosing()));
/* Timer for log level */
conTimer = new QTimer(this);
connect(conTimer, SIGNAL(timeout()), this, SLOT(timerFired()));
}
ModuleCommunicator::~ModuleCommunicator()
{
if (port->isOpen())
port->close();
delete msgBuffer;
delete port;
}
...
void ModuleCommunicator::onReadyRead()
{
if (port->bytesAvailable()) {
QString msg = QString(port->readAll());
msgBuffer->append(msg);
if (msg.endsWith("\n")) {
msg = msgBuffer->join("").trimmed();
msgBuffer->clear();
msgBuffer->append(msg.split("\r\n"));
} else {
return;
}
for (int i = 0; i < msgBuffer->size(); i++) {
msg = msgBuffer->at(i);
qDebug() << "MSG: " << msg << endl;
if (isResponse(msg)) {
handleMsg(msg);
}
}
msgBuffer->clear();
}
}
編集:興味深い。「flush()」のコメントを外すと、うまくいきます。フラッシュを使用すると、複数のメッセージが表示されます。しかし、受信側では?「flush()」が原因でメッセージが複数回送信される可能性はありますか?
void ModuleCommunicator::handleMsg(const QString &msg)
{
// Parse msg
QSharedPointer<USBResponse> resp = QSharedPointer<USBResponse>(parse_message(msg));
if (!resp->is_valid()) {
// LOG
return; // Invalid response
}
// omitted
/* Find completion handler for response
Match to first command in Queue with same command & address,
or same command and wildcard adress */
// Omitted
// Sending next msg in queue if there is one
if (!sendQueue.isEmpty()) {
QueuedCommand qc = sendQueue.takeFirst();
QString nxtcmd = qc.cmdstr;
completionHandlerQueue.append(qc.ch);
qDebug() << "handleMsg: Sending next msg: " << qc.cmdstr;
if (port->write(nxtcmd.toLatin1()) == -1) {
qDebug() << "handleMsg: write failed!";
}
// !!! Removing the following line makes things work?!
port->flush();
}
return;
}