while循环中的QSerialPort? [英] QSerialPort in while Loop?

查看:247
本文介绍了while循环中的QSerialPort?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,通过RS-232发送数据到超级终端。该函数在while循环中正常工作,但是在while循环中,它仅在它不发送任何内容之后的第一个时间发送。

I have a function that sends data over RS-232 to Hyperterminal. The function works properly out of the while loop, however, in the while loop, it sends only at the first time after that it doesn't send anything.

    qDebug() << MESSAGE;
    int choice;
    std::cin >> choice;

    while( choice != 3 )
    {
        switch (choice)
        {
            case 1:
                // Ready to send data 
                port->write("QSerial Port!\r\n");
                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!\r\n", qstrlen("QSerial Port!\r\n")) == -1)
                {
                    qDebug() << port->errorString();
                }
                //port->bytesWritten(strlen("QSerial Port!\r\n"));
                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();
}


推荐答案

与点(3)。
没有什么,说你不能做第二次写,而第一次仍然出色。原则上,应该将数据添加到发送队列中。
这将像在上一个数据写入磁盘之前不能再次写入此文件。
我有一个类似的问题,qtserial我在做什么:

I can't completely agree with point (3). There is nothing which says you can't do a second write, whilst the first is still outstanding. In principle the data should just be added to the transmit queue. It would be like saying "You can't write again to this file until the previous data has been written to disk". I had a similar problem with qtserial where I was doing:

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

原来你有办法做一个qApp-> processEvents接收任何
或在您可以写入新数据之前。
我期待与读/写文件相同的行为。
对我来说qtserial不符合你的期望设备的行为。

Turns out you HAVE to do a qApp->processEvents() before you can receive anything or before you can write new data. I was expecting the same behavior as for reading/writing a file. To me qtserial is NOT behaving as you would expect a device to.

这篇关于while循环中的QSerialPort?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆