我的应用程序正在通过串行端口(USB 串行)与嵌入式系统通信。
我正在使用来自 gitorious ( https://qt.gitorious.org/qt/qtserialport/ ) 的 Qt 4.8 和 QtSerialPort 进行开发,我还在带有 Windows 7 和 Qt 5.2.1 的虚拟机中进行测试
它在 Linux 下运行良好,但我对同一消息进行了两次、三次四次读取。 我可以验证这些消息只发送一次,通过 PuTTY 进行通信。
所以问题是,这是 QtSerialPort、VM(尽管它与 putty 一起工作?)、串行驱动程序...等的问题
这是一个已知问题吗? (虽然我找不到任何东西) 有什么想法可以弄清楚这个问题吗?
我是这样读的:
编辑:摩尔代码:
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;
最佳答案
问题是由调用“flush”方法引起的。 在写入串行端口后删除刷新调用可以解决问题。
“刷新”方法似乎存在已知问题(尽管文档中的提示会很好,直到它被修复)。这是我发现的错误报告:
https://bugreports.qt-project.org/browse/QTBUG-36726
引用:
Yes, a flush() method has bugs yet. Please do not use a flush().
关于c++ - QtSerialPort:在 Windows 7 上重复读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836468/