QT5.5实现串口通信

QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
这两个库,后续只需要调用函数,对串口名,读写方式,波特率,验证方式,数据位数,结束位进行相应设置,便可以连接相应串口进行通信。

部分代码:
 if(ui->pushButton->text()==tr("打开串口")){
        my_serialport= new QSerialPort();
        my_serialport->setPortName(ui->comboBox->currentText());
        my_serialport->open(QIODevice::ReadWrite);
        my_serialport->setBaudRate(ui->comboBox_2->currentText().toInt());
        switch(ui->comboBox_3->currentIndex()){
        case 0: my_serialport->setParity(QSerialPort::NoParity);break;
        case 1: my_serialport->setParity(QSerialPort::OddParity);break;
        case 2: my_serialport->setParity(QSerialPort::EvenParity);break;
        default: break;
        }
        switch(ui->comboBox_4->currentIndex()){
        case 0: my_serialport->setDataBits(QSerialPort::Data8);break;
        case 1: my_serialport->setDataBits(QSerialPort::Data7);break;
        case 2: my_serialport->setDataBits(QSerialPort::Data6);break;
        default: break;
        }
        switch(ui->comboBox_5->currentIndex()){
        case 0: my_serialport->setStopBits(QSerialPort::OneStop);break;
        case 1: my_serialport->setStopBits(QSerialPort::TwoStop);break;
        default: break;
        }
        my_serialport->setFlowControl(QSerialPort::NoFlowControl);
        connect(my_serialport,SIGNAL(readyRead()),this,SLOT(my_readuart()));
        ui->comboBox->setEnabled(false);
        ui->comboBox_2->setEnabled(false);
        ui->comboBox_3->setEnabled(false);
        ui->comboBox_4->setEnabled(false);
        ui->comboBox_5->setEnabled(false);
        ui->label_6->setStyleSheet("background-color:red");
        ui->pushButton->setText(tr("关闭串口"));
        ui->pushButton_2->setEnabled(true);
    }
    else {
        my_serialport->clear();
        my_serialport->deleteLater();
        //  my_serialport->deleteLater();
        ui->comboBox->setEnabled(true);
        ui->comboBox_2->setEnabled(true);
        ui->comboBox_3->setEnabled(true);
        ui->comboBox_4->setEnabled(true);
        ui->comboBox_5->setEnabled(true);
     //   ui->label_6->setStyleSheet("background-color:none");
        ui->label_6->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pushButton->setText(tr("打开串口"));
        ui->pushButton_2->setEnabled(false);
    }
其中参考于csdn的yh_1988
void serial::on_pushButton_2_clicked()
{
    my_serialport->write(ui->lineEdit->text().toLatin1());
}

值得注意的是,在用于串口通信时,上述函数中只讲lineEdit中的text传递给串口,但是这样是缺少换行符/n的。如果是用在有uboot系统上通过串口进行交互时,应该修改为:

void serial::on_pushButton_2_clicked()
{
    my_serialport->write(ui->lineEdit->text().toLatin1());
  my_serialport->write("/n");
}
这样就可以正常的通信啦。

系统启动过程打印

输入?弹出帮助菜单

 

最近用Qt5.9重新编辑了一个Demo,需要下载的同学可以在下面的网址下载

https://github.com/dong930623/Qt/tree/master/SerialPortToolDemo

转载于:https://www.cnblogs.com/-Donny/p/5039761.html

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值