QByteArray WorkThread::getAtCmdResponse() { if (nullptr == serialPort) {return "";} QByteArray resp; while (serialPort->waitForReadyRead(20)) { qApp->processEvents(); resp += readSerialPort(); } qDebug()<<resp; return resp; }
时间: 2024-04-28 13:27:06 浏览: 11
这是一个名为`getAtCmdResponse`的函数,它返回一个`QByteArray`类型的对象。该函数的作用是从串口接收AT指令的响应并返回响应内容。如果`serialPort`指针为空,则返回一个空的QByteArray对象。
在函数中,首先判断`serialPort`指针是否为空,如果是,则直接返回空的QByteArray对象。接着,通过循环等待串口的数据读取完成,每20毫秒检查一次是否有数据可读,并通过`readSerialPort()`函数从串口读取数据,并将读取的数据添加到`resp`对象中。在等待读取数据的过程中,通过`processEvents()`函数处理事件队列,以确保程序的响应能力。最后打印读取到的响应内容,并将其返回。
相关问题
QByteArray WorkThread::readSerialPort() { if (nullptr == serialPort) {return "";} qint64 byteLen = serialPort->bytesAvailable(); if(byteLen < 0) { return ""; } return serialPort->readAll(); }
这段代码是用来读取串口数据的,先判断QSerialPort对象是否为nullptr,如果是就返回一个空的QByteArray。然后获取可用的字节数,如果小于0也返回一个空的QByteArray。最后调用QSerialPort的readAll()函数读取所有可用的数据,并将其封装到一个QByteArray对象中返回。这里需要注意,readAll()函数可能会阻塞程序,直到读取到数据为止。
void WorkThread::sendSerialData(QByteArray data) { if (serialPort != nullptr) { serialPort->write(data); } }
这段代码是用来发送串口数据的,首先判断QSerialPort对象是否为nullptr,如果不是就调用QSerialPort的write()函数发送数据。write()函数会将指定的数据写入到串口缓冲区中,如果缓冲区已满,则write()函数可能会阻塞程序,直到缓冲区有足够的空间为止。需要注意的是,write()函数只是将数据写入缓冲区,并不保证数据已经成功发送到串口设备。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)