QSerialPort in while Loop?
QSerialPort in while Loop?
我有一个通过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的行为不像你期望的那样。
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 什么是'do while loop'的真实应用?
- 如果用户输入数字 <0,我如何停止" while loop "?
- C++ cin.ignore and getline in while loop
- for(;;) vs do..while() for main program loop
- 如何在 while-loop 中重新初始化向量
- C++ cin.fail() while loop
- 非静态成员函数的使用无效(while loop/no class/wget popen)
- do-while loop within for is not wokirng
- Empty While loop- Arduino
- C++Winapi while loop解决方法
- Char*和While Loop不能相互作用
- While loop在碰到break后再进行一个循环
- Poco ODBC and while SQL loop
- C++ While Loop: Hex & Oct, & Dec
- 编程原理和实践使用C++while-loop演练。找不到退出循环的方法
- While loop and char
- While loop and cin char, int
- 将值赋给while-loop内部的指针(使用< direct .h>)
- QSerialPort in while Loop?