本文介绍了等待数据从Qt中的串口到达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在我的qt应用程序中使用串行端口连接。 我的问题 - 我不能在发送命令后返回控制(或值来自通讯端口)。 我有一个名为serial的类。 cpp负责串口的连接。 此类包含2个队列。一个用于从通信端口保存字节,第二个用于解码的消息。该类具有以下函数: void Serial :: sendCommand(QString s) { QString sometext = s; QByteArray ba = QByteArray :: fromHex(sometext.toLatin1()); serial-> write(ba); } void Serial :: serialReceived() { QByteArray ba; serialArray = serial-> readAll(); for(int i = 0; i< sizeof(serialArray); i ++) { queueBytes.enqueue(serialArray [i]); //用于保存字节的队列} QVector< int> vect = queueBytes.toVector(); packetSize = 6; if(vect.size()> = packetSize) {//整个包到达 for(int i = 0; i { item = queueBytes.dequeue(); ba.append(item); } } if(ba.toHex()==12ee02010176) queueMsgs.enqueue(ACK); // ...等等} 这里是调用类: void Engine :: onNewMessageFromAppReceived(int msgId,QString args) { serial-> sendCommand(ee1203190209005569); while(serial-> queueMsgs.size()== 0)//无限循环 - 队列总是空的 { usleep(1); } QVector< QString> vect2 = serial-> queueMsgs.toVector(); qDebug()<< vect2 } 请帮助解决方案 QSerialPort 类继承自 QIODevice 有 waitFor ... 方法可能是你正在寻找。请查看这些文档。 如果要异步处理串行端口,请使用 readyRead 信号,并在连接到该信号的函数中执行读取。如果你不介意操作是阻塞的, waitForReadyRead 函数是你要找的。 好方法 这是使用 Qt的信号和槽机制。此不会阻止您的GUI,并且即使在您等待串口时,您的应用程序也会响应用户操作。 将函数连接到 bytesWritten 信号 在通过串行端口发送数据之后要执行的代码应放在此函数中 将函数连接到 readyRead 信号从串行中读取某些数据之后要执行的代码 错误的方式 h2> 在某些情况下,您可以这样做,但它阻止,这意味着您的应用程序等待串口时您的GUI会冻结。 打开端口 发送数据 li> 致电 waitForBytesWritten 致电 waitForReadyRead 工作示例代码 Qt有一个大量的工作示例代码。甚至有关于如何使用 QSerialPort ,他们是值得退房。您可能最感兴趣的是异步写入器示例和异步阅读器示例。 I use serial port connection in my qt application.My problem- I cant get back the control (or the values comes from the comm port) after sending the command.I have a class named serial.cpp which responsible to serial port connection.This class contains 2 queues. one for save bytes from the comm port and second for the decoded messages. the class has the functions below:void Serial::sendCommand(QString s){ QString sometext = s; QByteArray ba = QByteArray::fromHex(sometext.toLatin1()); serial->write(ba);}void Serial::serialReceived(){ QByteArray ba ; serialArray = serial->readAll(); for (int i=0; i<sizeof(serialArray);i++) { queueBytes.enqueue(serialArray[i]); // queue for saving the bytes } QVector<int> vect = queueBytes.toVector(); packetSize = 6; if (vect.size() >= packetSize) { // the whole packet arrived for (int i =0 ;i<packetSize;i++) { item = queueBytes.dequeue(); ba.append(item); } } if (ba.toHex() == "12ee02010176") queueMsgs.enqueue("ACK"); // ... and so on}here is the call class:void Engine::onNewMessageFromAppReceived(int msgId,QString args){ serial->sendCommand("ee1203190209005569"); while (serial->queueMsgs.size() == 0) // infinite loop-the queue is always empty { usleep(1); } QVector<QString> vect2 = serial->queueMsgs.toVector(); qDebug() << vect2 << "get ack---" ;}please your help 解决方案 The QSerialPort class inherits from QIODevice which has waitFor... methods that may be what you're looking for. Take a look at these docs.If you want to handle the serial port asynchronously, use the readyRead signal and perform reading in a function that you connected to that signal. If you don't mind that the operation is blocking, the waitForReadyRead function is what you're looking for.The good wayHere's the proper way to do it using Qt's signals and slots mechanism. This will not block your GUI and lets your application respond to user actions even while you are waiting for the serial port.Connect a function to the bytesWritten signalThe code you want to execute after you sent data through the serial port should be placed in this function.Connect a function to the readyRead signalThe code you want to execute after you read some data from the serial port should be placed in this function.Open the portThe bad wayIn some cases you can do it like this, but it's blocking, meaning that your GUI will freeze while your app is waiting for the serial port. I don't recommend doing it like this.Open the portSend dataCall waitForBytesWrittenCall waitForReadyReadWorking example codeQt has a vast amount of working example code. There are even examples about how to use QSerialPort, and they are well worth checking out. You might be most interested in the async writer example and the async reader example. 这篇关于等待数据从Qt中的串口到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 11:57