Qt-串口

文章介绍了Qt中QSerialPort类用于串口通信的使用,包括非阻塞和阻塞两种模式。非阻塞模式依靠readyRead()和bytesWritten()信号处理数据读写,而阻塞模式使用waitForReadyRead()和waitForBytesWritten()函数等待数据。此外,还提供了一个串口调试助手的代码示例,该助手具有自动发现串口和动态更新端口列表的功能。
摘要由CSDN通过智能技术生成

串口

串口总是以独占访问的方式打开,即没有其他进程或线程可以访问已经打开的串口
读取:一次性读不完,则留在输入缓冲区中,待下次读取,新来的数据则添加到缓冲区中

相关函数

read() // 可以设置读取的最大字节数
write() // 写入数据,返回实际被写入的数量

readBufferSize() // 输入缓冲区的大小,限制了read readAll读取的上限
setReadBufferSize() // 设置读取缓冲区的大小

readAll() // 读取缓冲区中所有可读的数据

readLine() // ???QIODevice::
readLineData() // ???QIODevice::

readData() // ???QIODevice::
writeData() // ???QIODevice::

readyRead() // 信号函数,只要有可读的数据就发送该信号 // 有新数据到达的时候发送readyRead;缓冲区中还有未读取的数据触发readyRead
bytesWritten() // 信号函数,该信号触发(只要有一个数据被写入底层串口就会触发)则阻塞函数waitForBytesWritten()会返回true

bytesAvailable() // 返回可以用于读取的字节数量,常在读取前用于动态分配内存,此时字节数据并没有被真正的读取
bytesToWrite() // 返回正在等待被写入输出缓冲区的字节数量,此时字节数据并没有被真正的写入 ,字节写入的实际是事件循环返回控制,或调用flush()

clear() // 清空输入(读取)或输出(发送)缓冲区
close() // 关闭串口
flush() // 非阻塞方式将输出缓冲区中的数据写入底层串口,只要有一个数据被写入则返回true,通常情况下不需要显示的调用,

// 配对
/*
	read - write 
	readData - wirteData ???
	readyRead - bytesWritten // 信号函数,非阻塞调用
	bytesAvailable - bytesToWrite // 返回数量
	waitForReadyRead - waitForBytesWritten // 阻塞调用
*/

非阻塞串口

信号函数

readyRead() // 信号函数,只要有可读的数据就发送该信号 // 有新数据到达的时候发送readyRead;缓冲区中还有未读取的数据触发readyRead
bytesWritten() // 信号函数,该信号触发(只要有一个数据被写入底层串口就会触发)则阻塞函数waitForBytesWritten()会返回true

阻塞串口

阻塞串口,用于阻塞挂起调用的线程,直到发出特定信号
阻塞串口不需要事件循环,但是应该用在非GUI线程中

waitForReadyRead() // 阻塞调用,直到新的数据可以被读取(输入缓冲区中有可被读取的数据),然后readyRead信号被触发后该函数返回,只有当关闭连接,或者是发生错误才会返回false
waitForBytesWritten() // 阻塞调用,直到有效(至少一个)数据被写入到串口中(从缓冲区写入到底层串口),然后bytesWritten信号会被触发,返回该函数返回true

例子-串口调试助手

https://github.com/vseasky/vsailorproject

功能待完善

  • 串口发现机制
  • 周期发送
  • 智能识别发送的格式

.pro

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
//#include <QSerialPort/QSerialPort>
#include <QSerialPort>
//#include <QSerialPort/QSerialPortInfo>
#include <QSerialPortInfo>
#include <QTimer>
#include <QString>
#include <QStringList>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;

    QTimer *timer;

    QStringList portStringList;

    QSerialPort *serialPort;

    bool isOpButtonOn;

    QByteArray array;
    QString receiveText;
    long receiveText_length;
    long receiveAllText_length;
    long receiveHex_size;
    long receiveAllHex_size;

    QString sendText;
    long sendText_length;
    long sendHex_size;

private slots:
    void myTimerEvent(void);
    void on_pushButton_clicked();

    void serialPort_readyRead();
    void on_checkBox_clicked();
    void on_checkBox_2_clicked();
    void on_checkBox_3_clicked();
    void on_checkBox_4_clicked();
    void on_checkBox_5_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_5_clicked();
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 窗口设置
    this->resize(800,600);
    this->setWindowTitle("Serial Port Assistant");

    // 设置点击开始表示
    isOpButtonOn=false;

    // 设置串口默认值
    // 设置串口号,combobox 下来菜单选项 input类型
    ui->comboBox_2->setCurrentIndex(1); // 波特率
    ui->comboBox_3->setCurrentIndex(3); // 数据位
    ui->comboBox_4->setCurrentIndex(2); // 校验位
    ui->comboBox_5->setCurrentIndex(0); // 停止位

    // 设置接收端进制数默认值
    ui->checkBox->setCheckState(Qt::Checked); // 默认是ASCII  等价于ui->checkBox->setChecked(true);
    // 设置发送端进制数默认值
    ui->checkBox_3->setCheckState(Qt::Checked); // 默认是ASCII

    // 创建一个对象
    serialPort=new QSerialPort(this); // this 相当于是一个Widget实例对象

    // 接收数据
    // readyRead 有数据发送过来,收到数据产生一定的动作,执行serialPort_readyRead
    connect(serialPort,&QSerialPort::readyRead,this,&Widget::serialPort_readyRead);
    receiveText_length=0;
    receiveAllText_length=0;
    receiveHex_size=0;
    receiveAllHex_size=0;

    // 发送数据
    sendText_length=0;
    sendHex_size=0;

    // 定时器扫描有多少个串口
    // version1
    timer=new QTimer(this);
    timer->start(500); // 启动定时器,单位毫秒
    connect(timer,&QTimer::timeout,this,&Widget::myTimerEvent);
    // version2
    //    timerId1=startTimer(500);
    //    void Widget::timerEvent(QTimerEvent *event)
    //    {
    //        if (event->timerId()==timerId1){}
    //    }


    ui->checkBox->setEnabled(false);
    ui->checkBox_2->setEnabled(false);
    ui->checkBox_3->setEnabled(false);
    ui->checkBox_4->setEnabled(false);
    ui->checkBox_5->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_5->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::myTimerEvent(void)
{
    //    创建一个容器
    QStringList newPortStringList;
    newPortStringList.clear(); // 不加也可以

    // foreach关键字 foreach(variable,container) container是一个容器,容器中的每个元素都是一个variable
    // QList<QSerialPortInfo>QSerialPortInfo::availabelPorts()
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) // QSerialPortInfo &info 是一个引用
    {
        QString portName=info.portName();
        //        qDebug()<<portName;
        newPortStringList+=portName; // 等价于 newPortStringList+=info.portName()
    }

    // #TODO -- 字符串比较
    // 缺少一个判断portname是否相同
    //    QStringList portStringList2Update;
    //    foreach (const QString &new_portname, newPortStringList) {
    //        foreach (const QString &portname, portStringList) {
    //            if (){}
    //        }
    //    }

    if (newPortStringList.size()!=portStringList.size())
    {
        portStringList=newPortStringList;
        ui->comboBox->clear(); // 清空下拉菜单
        ui->comboBox->addItems(portStringList);
    }

}


void Widget::on_pushButton_clicked()
{
    if (ui->pushButton->text()==QString("打开串口"))
    {
        // 设置当前串口
        serialPort->setPortName(ui->comboBox->currentText()); // 设置串口名
        serialPort->setBaudRate(ui->comboBox_2->currentText().toInt()); // 设置波特率

        // 设置数据位
        // setDataBits是枚举类型
        // 注意枚举类型不能利用整型进行赋值,只能用枚举类型进行复制,除非进行强制类型转换
        switch (ui->comboBox_3->currentText().toInt()) {
        case 5:
            serialPort->setDataBits(QSerialPort::Data5);
            break;
        case 6:
            serialPort->setDataBits(QSerialPort::Data6);
            break;
        case 7:
            serialPort->setDataBits(QSerialPort::Data7);
            break;
        case 8:
            serialPort->setDataBits(QSerialPort::Data8);
            break;
        default:
            serialPort->setDataBits(QSerialPort::UnknownDataBits);
            break;
        }

        // 设置校验位
        // currentIndex 利用combobox索引编号从0开始
        switch (ui->comboBox_4->currentIndex()) {
        case 0:
            serialPort->setParity(QSerialPort::OddParity); // 奇校验
            break;
        case 1:
            serialPort->setParity(QSerialPort::EvenParity); // 偶校验
            break;
        case 2:
            serialPort->setParity(QSerialPort::NoParity); // 无校验
        default:
            serialPort->setParity(QSerialPort::UnknownParity);
            break;
        }

        // 设置停止位
        switch (ui->comboBox_5->currentIndex()) {
        case 0:
            serialPort->setStopBits(QSerialPort::OneStop);
            break;
        case 1:
            serialPort->setStopBits(QSerialPort::OneAndHalfStop);
            break;
        case 2:
            serialPort->setStopBits(QSerialPort::TwoStop);
        default:
            serialPort->setStopBits(QSerialPort::UnknownStopBits);
            break;
        }

        // 流控
        serialPort->setFlowControl(QSerialPort::NoFlowControl);

        // 打开串口
        // 这是虚函数
        // QIODevice::ReadWrite 读写权限
        if (!serialPort->open(QIODevice::ReadWrite)) // 判断打开串口是否失败
        {
            QMessageBox::information(this,"ERROR","failed to open this port",QMessageBox::Ok); // QMessageBox::ok 提示按键
            return ; // 直接退出
        }

        // 设置状态,不允许修改
        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->pushButton->setText("关闭串口");

        ui->checkBox->setEnabled(true);
        ui->checkBox_2->setEnabled(true);
        ui->checkBox_3->setEnabled(true);
        ui->checkBox_4->setEnabled(true);
        ui->checkBox_5->setEnabled(true);

        ui->pushButton_2->setEnabled(true);
        ui->pushButton_5->setEnabled(true);
    }
    else
    {
        // 关闭串口
        serialPort->close();
        // 设置状态,允许修改
        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->pushButton->setText("打开串口");

        ui->checkBox->setEnabled(false);
        ui->checkBox_2->setEnabled(false);
        ui->checkBox_3->setEnabled(false);
        ui->checkBox_4->setEnabled(false);
        ui->checkBox_5->setEnabled(false);
        ui->pushButton_2->setEnabled(false);
        ui->pushButton_5->setEnabled(false);
    }
}


void Widget::serialPort_readyRead()
{
    //    qDebug()<<"receive data";

    // 累计接收的数据
    QString receiveAllText;

    // 判断是否暂停
    if (ui->checkBox_5->checkState()!=Qt::Checked)
    {
        /*== version 1 QString ==*/
        // 之前接收的全部数据
        receiveAllText=ui->textEdit->toPlainText(); // 读取显示的数据

        // 应该QByteArray 才对,但是QString也可以
        // (QByteArray -> QString ) 可以直接赋值
        receiveText=serialPort->readAll(); // 把串口内的数据全部读取
        receiveText_length=receiveText.length(); // 没有\0 ;接收到当前的字符数
        // ui->textEdit->setText(receiveText); // 仅显示当前次的
        // ui->label_8->setText(QString::number(receiveText_length));

        // 处理历史数据
        receiveAllText_length+=receiveText_length; // 历史接收到的字符数
        ui->label_8->setText(QString::number(receiveAllText_length));

        // 处理空格问题
        int receiveHexText_length; // toHex() 得到的字符串

        // ASCII 显示
        if (ui->checkBox->checkState()==Qt::Checked)
        {
            // can use no code...
            // receiveText=receiveText.toLatin1(); // Qstring -> QByteArray (基于ASCII编码方式进行转换)
            // 内部又从QByteArray -> QString

        }
        else if(ui->checkBox_2->checkState()==Qt::Checked) // HEX显示
        {
            // string -> QByteArray (基于ASCII编码方式进行转换)
            // QByteArray receiveHex=receiveTex.toLatin1(); // 虽然toLatin1转变成了QByteArray 但是又进行了二次转换
            // ui->textEdit->setText(receiveHex.toHex());

            receiveText=receiveText.toLatin1().toHex(); // toHex 返回的是字节数的字符串
            receiveHexText_length=receiveText.length();
            for (int i=0;i<receiveHexText_length/2;i++)
            {
                receiveText.insert(2+3*i,' '); // 插入空格的位置找规律
            }
        }

        // 最终读取和显示都落到字符串的格式比较方便
        receiveAllText=receiveAllText.append(receiveText);
        ui->textEdit->setText(receiveAllText); // 累计显示

        /*== version 2 QByteArray ==*/
        // 之前接收的全部数据
        //        QByteArray receiveHex=serialPort->readAll(); // 把串口内的数据全部读取
        //        receiveHex_size=receiveHex.size();

        //        if (ui->checkBox->checkState()==Qt::Checked) // ASCII编码
        //        {
        //            receiveAllText=ui->textEdit->toPlainText(); // 读取显示的数据
        //            receiveAllText=receiveAllText.append(QString(receiveHex)); // QByteArray -> QString
        //        }
        //        else if (ui->checkBox_2->checkState()==Qt::Checked)
        //        {
        //            receiveAllText=ui->textEdit->toPlainText(); // 读取显示的数据
        //            QByteArray receiveAllHex=QByteArray::fromHex(receiveAllText.toLatin1());

        //            // 删除空格
        //            receiveAllHex_size=receiveAllHex.size();
        //            for (int i=receiveAllHex_size-1;i>=0;i--)
        //            {
        //                if (receiveAllHex[i]==0x20)
        //                {
        //                    receiveAllHex.remove(i,1);
        //                }
        //            }

        //            // 纯字节数据
        //            receiveAllHex.push_back(receiveHex);

        //            receiveAllText=QString(receiveAllHex.toHex());

        //            int receiveAllHexText_length=receiveAllText.size();

        //            // 处理空格问题
        //            for (int i=0;i<receiveAllHexText_length/2;i++)
        //            {
        //                receiveAllText.insert(2+3*i,' '); // 插入空格的位置找规律
        //            }
        //            // qDebug()<<receiveAllHex<<" "<<receiveAllHex.toHex();

        //        }
        //        ui->textEdit->setText(receiveAllText); // 累计显示

    }
}

void Widget::on_checkBox_clicked()
{
    // 选项互斥
    ui->checkBox->setCheckState(Qt::Checked);
    ui->checkBox_2->setCheckState(Qt::Unchecked);
    ui->checkBox_5->setCheckState(Qt::Unchecked);
}


void Widget::on_checkBox_2_clicked()
{
    // 选项互斥
    ui->checkBox_2->setCheckState(Qt::Checked);
    ui->checkBox->setCheckState(Qt::Unchecked);
    ui->checkBox_5->setCheckState(Qt::Unchecked);
}

void Widget::on_checkBox_5_clicked()
{
    // 选项互斥
    ui->checkBox_5->setCheckState(Qt::Checked);
    ui->checkBox->setCheckState(Qt::Unchecked);
    ui->checkBox_2->setCheckState(Qt::Unchecked);
}

void Widget::on_checkBox_3_clicked()
{
    ui->checkBox_3->setCheckState(Qt::Checked);
    ui->checkBox_4->setCheckState(Qt::Unchecked);
    QMessageBox::information(this,
                            "notice",
                            "输入正确的ASCII数据",
                            QMessageBox::Ok);
}

void Widget::on_checkBox_4_clicked()
{
    ui->checkBox_4->setCheckState(Qt::Checked);
    ui->checkBox_3->setCheckState(Qt::Unchecked);
    QMessageBox::information(this,
                            "notice",
                            "输入正确的HEX数据",
                            QMessageBox::Ok);
}



void Widget::on_pushButton_2_clicked()
{
    QByteArray sendArray;

    // 待发送的数据
    if (ui->checkBox_3->isChecked())
    {
        sendText=ui->textEdit_2->toPlainText();
        sendArray=sendText.toLatin1();
    }
    else if (ui->checkBox_4->isChecked())
    {

        sendText=ui->textEdit_2->toPlainText();
        // 根据正则表达式删除所有空格
        sendText.remove(QRegExp("\\s"));
        sendArray=QByteArray::fromHex(sendText.toLatin1());
    }

    int isSend=serialPort->write(sendArray);
    if (isSend==-1  || isSend!=sendArray.size())
    {
        QMessageBox::warning(this,"WARING","未发送成功!",QMessageBox::Ok);
    }

    sendHex_size+=sendArray.size();
    ui->label_9->setText(QString::number(sendHex_size));
}

void Widget::on_pushButton_3_clicked()
{
    ui->textEdit->clear();
}

void Widget::on_pushButton_4_clicked()
{
    ui->textEdit_2->clear();
}

void Widget::on_pushButton_5_clicked()
{

}

main.cpp

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

.ui文件

见资源

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值