如何在linux下用QT写一个简单的串口调试助手

如何在linux下用QT写一个简单的串口调试助手

QT5串口类

在QT5以前,编写串口一般使用的是qextserialport类,但在QT5之后有了QT自带的串口类SerialPort(串口基础类)和SerialPortInfo(串口信息类)

使用方法

pro中添加
QT += serialport

工程中包含相应的头文件
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

linux下查询串口名的方法

ctrl+alt+a打开终端,输入dmesg | grep ttyS*查询串口名,一般为ttyUSB0
如图 可以看到串口名为ttyUSB0,为ch340的驱动
如图 可以看到串口名为ttyUSB0,为ch340的驱动

linux的串口权限问题

我在刚开始写这个程序的时候总是无法打开串口,尝试了qextserialport类和SerialPort类都无法成功,后来想起来再用cutecom来调试串口的时候想到需要用管理员权限打开cutecom才可以打开串口,可是我又不知道怎么给QT管理员权限,因此我让普通用户也能使用串口,增加udev规则来实现

sudo vim /etc/udev/rules.d/70-ttyusb.rules
增加如下内容:
KERNEL=="ttyUSB[0-9]*",MODE="0666"
保存并退出,重新插入USB转串口,就可以打开串口了

此方法参考的如下链接:https://blog.csdn.net/touch_dream/article/details/52873651

以下给出部分代码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QTextCodec>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
   Q_OBJECT
public:
   explicit MainWindow(QWidget *parent = 0);
   ~MainWindow();
private slots:
   void on_clearButton_clicked();//清除数据
   void on_sendButton_clicked();//发送数据
   void on_openButton_clicked();//打开串口
   void Read_Data();//读取串口数据
private:
   Ui::MainWindow *ui;
   QSerialPort *serial;//串口对象
};
#endif // MAINWINDOW_H

下面是mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //查找可用的串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            ui->PortBox->addItem(serial.portName());
            serial.close();
        }
    }
    //关闭发送按钮的使能
    ui->sendButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
    delete ui;
}
//清空接受窗口
void MainWindow::on_clearButton_clicked()
{
    ui->textEdit->clear();
}
//发送数据
void MainWindow::on_sendButton_clicked()
{
    serial->write(ui->textEdit_2->toPlainText().toLatin1());
}
void MainWindow::readMyCom() //读取串口数据并显示出来
{
      QByteArray temp = myCom->readAll();     //读取串口缓冲区的所有数据给临时变量temp
      ui->textEdit->insertPlainText(temp);     //将串口的数据显示在窗口的文本浏览器中
}

//读取接收到的数据
void MainWindow::Read_Data()
{
    qDebug() << tr("youshuju");
    QByteArray buf;
    buf = serial->readAll();
    if(!buf.isEmpty())
    {
        QString str = ui->textEdit->toPlainText();

        QTextCodec *codec = QTextCodec::codecForName("GBK");//指定QString的编码方式
        QString str_buf=codec->toUnicode(buf);//buf转换成QString类型
        str=str+'\n'+str_buf;

        //buf=buf.toHex();     或者使用这句话将buf转换成十六进制显示
        //str=str+'\n'+tr(buf);

        ui->textEdit->clear();
        ui->textEdit->append(str);
    }
    buf.clear();
}
void MainWindow::on_openButton_clicked()
{
    if(ui->openButton->text()==tr("打开串口"))
    {
        serial = new QSerialPort;
        //设置串口名
        serial->setPortName("/dev/ttyUSB0");//linux下默认的串口名,可以进入终端查询
        //打开串口
        if(serial->open(QIODevice::ReadWrite))  //如果成功打开串口
        //设置波特率
        {
            qDebug() << tr("succeed");
//            serial->setBaudRate(QSerialPort::Baud9600);
    
//            serial->setDataBits(QSerialPort::Data8);
    
//            serial->setParity(QSerialPort::NoParity);
    
//            serial->setStopBits(QSerialPort::OneStop);
            //设置数据位数
            switch(ui->BitNumBox->currentIndex())
            {
            case 8: serial->setDataBits(QSerialPort::Data8); break;
            default: break;
            }
            //设置奇偶校验
            switch(ui->ParityBox->currentIndex())
            {
            case 0: serial->setParity(QSerialPort::NoParity); break;
            default: break;
            }
            //设置停止位
            switch(ui->StopBox->currentIndex())
            {
            case 1: serial->setStopBits(QSerialPort::OneStop); break;
            case 2: serial->setStopBits(QSerialPort::TwoStop); break;
            default: break;
            }
            //设置流控制
            serial->setFlowControl(QSerialPort::NoFlowControl);
            //关闭设置菜单使能
            ui->PortBox->setEnabled(false);
            ui->BaudBox->setEnabled(false);
            ui->BitNumBox->setEnabled(false);
            ui->ParityBox->setEnabled(false);
            ui->StopBox->setEnabled(false);
            ui->openButton->setText(tr("关闭串口"));
            ui->sendButton->setEnabled(true);
            //连接信号槽
            QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::Read_Data);
        }
        else
        {
            qDebug() << tr("default");
        }
    }
    else
    {
        //关闭串口
        serial->clear();
        serial->close();
        serial->deleteLater();
        //恢复设置使能
        ui->PortBox->setEnabled(true);
        ui->BaudBox->setEnabled(true);
        ui->BitNumBox->setEnabled(true);
        ui->ParityBox->setEnabled(true);
        ui->StopBox->setEnabled(true);
        ui->openButton->setText(tr("打开串口"));
        ui->sendButton->setEnabled(false);
    }
}

最终效果

在这里插入图片描述
打开串口后即可收到串口发送过来的数据

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值