I finally succeeded,The code I wrote is as follows:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_serialPort=new QSerialPort;
m_serialPort->setPortName("COM3");
m_serialPort->open(QIODevice::ReadWrite);
m_serialPort->setBaudRate(QSerialPort::Baud9600);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
while(!m_serialPort->isOpen()) m_serialPort->open(QIODevice::ReadWrite);
connect(m_serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead);
connect(&m_timer, &QTimer::timeout, this, &MainWindow::handleTimeout);
m_timer.start(5000);
}
void MainWindow::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
if (!m_timer.isActive())
m_timer.start(5000);
}
void MainWindow::handleTimeout()
{
if (m_readData.isEmpty()) {
qDebug()<< "m_readData is empty" <<m_readData;
} else {
qDebug()<< "m_readData is full" <<m_readData;
QJsonDocument jsonDoc = QJsonDocument::fromJson(m_readData);
QJsonObject jsonObj = jsonDoc.object();
QJsonValue jsonVal;
jsonVal = jsonObj.value(QString("Bat_Temp"));
QTextStream textStream(stdout);
textStream << QString::number(jsonVal.toInt()) << endl;
}