解决QT串口通信中QIODevice::write (QSerialPort): device not open错误

QT 串口通信中 QIODevice::write (QSerialPort): device not open

问题

请添加图片描述

在使用QT做串口通信,想通过制作的串口助手来发送数据控制STM32开发板,但是串口通信不成功

@Override
connect(ui->SendButton,&QPushButton::clicked,[=](){
            serial->write("1"); //发送字符 1
            //显示连接的串口通信
            qDebug() << "bund"<<ui->BaudBox->currentText();
            qDebug() << "bund"<<ui->BitBox->currentText();
    });

原因分析:

网上查找后说串口在发送时没有打开,但初始化的时候已经打开过了,查找后需要在发送的时候要先打开。


解决方案:

经过查找又看了帮助文档后,通信终于打通了

@Override
connect(ui->SendButton,&QPushButton::clicked,[=](){
        DataSend();//发送发送框数据
        if(serial->QSerialPort::open(QIODevice::ReadWrite)){
            serial->write("1");
            //显示连接的串口通信
            qDebug() << "bund"<<ui->BaudBox->currentText();
            qDebug() << "bund"<<ui->BitBox->currentText();
        }

    });

显示如下:设置好对应的波特率后,点击打开串口,点击发送,STM32开发板成功接收并点亮灯。请添加图片描述点击发送后成功点亮

物联沃分享整理
物联沃-IOTWORD物联网 » 解决QT串口通信中QIODevice::write (QSerialPort): device not open错误

发表回复