QSerialPort in while Loop?

QSerialPort in while Loop?

本文关键字:Loop while in QSerialPort      更新时间:2023-10-16

我有一个通过RS-232发送数据到超级终端的函数。该函数在while循环外正常工作,然而,在while循环中,它只在第一次发送,之后它不发送任何东西。

    qDebug() << MESSAGE;
    int choice;
    std::cin >> choice;
    while( choice != 3 )
    {
        switch (choice)
        {
            case 1:
                // Ready to send data 
                port->write("QSerial Port!rn");
                break;
            case 2:
                qDebug() << "Todo...";
                break;
            case 3:
                break;
            default:
                qDebug() << "Invalid Choice ...";
        }
        qDebug() << MESSAGE;
        std::cin >> choice;
    }
编辑:

#include <QCoreApplication>
#include <iostream>
#include <QDebug>
#include <QSerialPort>
const char MESSAGE[] = "n----- New Menu ----"
                       "n1- Send Data     n"
                       "2- Receive Data    n"
                       "3- Quit:           n";

int main(int argc, char *argv[])
{
    QCoreApplication  app(argc, argv);
    QSerialPort *port = new QSerialPort;
    port->setPortName("COM4");

    // Check the validity of the port
    if ( !port->open(QIODevice::ReadWrite) )
    {
        qDebug() << "nError: " << port->portName() << " port can't be opened ...";
        return -1;
    }else{
        qDebug() << 'n' << port->portName() << " port has been opened successfully ...";
        port->setBaudRate(QSerialPort::Baud9600);
        port->setStopBits(QSerialPort::OneStop);
        port->setDataBits(QSerialPort::Data8);
        port->setParity(QSerialPort::NoParity);
        port->setFlowControl(QSerialPort::NoFlowControl);
        qDebug() << port->portName() << " port has been configured correctly ...";
    }
    qDebug() << MESSAGE;
    int choice;
    std::cin >> choice;
    while( choice != 3 )
    {
        switch (choice)
        {
            case 1:
            {
                // Ready to send data 
                if ( port->write("QSerial Port!rn", qstrlen("QSerial Port!rn")) == -1)
                {
                    qDebug() << port->errorString();
                }
                //port->bytesWritten(strlen("QSerial Port!rn"));
                port->waitForBytesWritten(-1);
                //qDebug() << port->errorString();
            }
                break;
            case 2:
                qDebug() << "Todo...";
                break;
            case 3:
                break;
            default:
                qDebug() << "Invalid Choice ...";
        }
        qDebug() << MESSAGE;
        std::cin >> choice;
    }
    qDebug() << "n Goodbye ....";
    port->close();
    delete port;
    return app.exec();
}

你的代码有以下缺陷:

1)你没有处理错误。

2)不检查写操作的返回值

3)在再次写入之前,您似乎没有按照程序等待。这是不正确的。要么使用sync waitForBytesWritten信号,要么使用async bytesWritten信号,为下一次写入提供绿灯。

最关键的可能是最后一点。这将导致数据发送的"随机"行为。在这种特殊情况下,它可能会被顺序发送,因为等待输入可能会花费更长的时间,但它仍然不是稳定和健壮的代码编写。

我不完全同意(3)点。没有什么说你不能写第二篇,而第一篇仍然很出色。原则上,数据应该直接添加到传输队列中。这就像说"在之前的数据被写入磁盘之前,您不能再次写入该文件"。我有一个类似的问题与qtserial,我正在做:

while (..)
{
    <send data>
    <receive data>
}

原来你必须做一个qApp->processEvents()之前,你可以接收任何东西或者在写入新数据之前。我期待相同的行为为读/写一个文件。对我来说,qtserial的行为不像你期望的那样。