首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QtSerialPort: Windows 7上的重复读取

QtSerialPort: Windows 7上的重复读取
EN

Stack Overflow用户
提问于 2014-04-03 11:37:55
回答 1查看 842关注 0票数 3

我的应用程序是通过串口(usb串行)与嵌入式系统通信。

我正在使用QT4.8和QtSerialPort从gitorious (https://qt.gitorious.org/qt/qtserialport/)开发,我还在使用Windows7和QT5.2.1的VM中进行测试。

它在Linux下运行得很好,但是我得到了相同消息的双读,三读四读。我可以验证这些消息只发送一次,通过puTTY进行通信。

所以问题是,这是一个问题的QtSerialPort,虚拟机(虽然它工作在putty?),串行驱动程序.等。

这是众所周知的问题吗?(不过,我什么也找不到)有什么办法找出真相吗?

我就是这样读到的:

编辑: moar代码:

代码语言:javascript
复制
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();
}
}

编辑:有意思。取消注释“刷新()”会使事情正常工作。通过刷新,我看到了成倍的消息。但在接收端?是否有可能,由于“刷新()”而多次发送消息?

代码语言:javascript
复制
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;

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 13:17:17

这些问题是由调用“刷新”方法引起的。在写入串口后删除刷新调用解决了这个问题。

似乎“刷新”方法存在已知的问题(尽管文档中的提示很好,直到它被修复为止)。下面是我发现的bug报告:

https://bugreports.qt-project.org/browse/QTBUG-36726

引用:

是的,刷新()方法还存在bug。请不要使用冲水器()。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22836468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档