• 【IMX6ULL笔记】-- 从驱动到应用(基于Qt)- 串口


    之前有一篇文章讲过驱动到应用串口部分,之前的算是控制台篇,本章将主要围绕Qt搭建界面交互的串口
    uart 驱动到应用

    前期准备

    1.imx6ull 开发板(笔者使用的是 韦东山开发板)

    2.内核版本 4.9.88

    3.文件系统(buildroot 2019.02工具输出)移植好qt

    4.ubuntu 安装好qt

    5.交叉编译工具链:gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf

    驱动篇

    原理图,因为串口3被 485占用,我们向此处飞线接上USB转串口调试

    请添加图片描述

    设备树配置(之前有提过,这里简述)

    //pinctrl 配置如下
    pinctrl_uart3: uart3grp {
        fsl,pins = <
        MX6UL_PAD_UART3_TX_DATA__UART3_DCE_TX 0X1b0b1
        MX6UL_PAD_UART3_RX_DATA__UART3_DCE_RX 0X1b0b1
        >;
    };
     
    //节点配置如下,基本就是把 imx6ull.dtsi 文件中,uart3节点再重新配置一遍
    //可以把 imx6ull.dtsi 当作默认配置文件
    &uart3 {
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_uart3>;
        status = "okay";
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    修改完成,编译,替换新的dtb文件

    make dtbs
    
    • 1
    应用篇

    使用buildroot qt中添加qt5serialport,然后重新make编译文件系统即可

    请添加图片描述

    QT工程搭建

    *.pro文件中注意添加 serialport

    QT       += core gui serialport
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        mainwindow.cpp
    
    HEADERS += \
        mainwindow.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    源码如下(这种代码网上也一大把,可以拉下来根据项目作一定修改):

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    
    private:
        QSerialPort *pSerialPort;
    
    
        /************左边布局************/
        QGridLayout* pLeftLayout;
        QLabel* pLabel[5];
        QComboBox* pComboBox[5];
        QPushButton *pPushButton[3];
    
        /************右边布局************/
        QVBoxLayout* pRightLayout;
        QTextBrowser *pTextRecive;
        QTextEdit *pTextSend;
    
         /************主体布局************/
        QWidget* mainWidget;
        QHBoxLayout* pMainWidget;
    
    private:
        void layoutConfig(void);
    
        void scanSerialPort(void);
    
        void baudRateInit(void);
        void dataBitsInit(void);
        void parityInit(void);
        void stopBitsInit(void);
    
    private slots:
        void sendSerialPortData();
        void openSerialPortDevice();
        void clearTextData();
        void serialPortReadyRead();
    
    };
    #endif // MAINWINDOW_H
    
    .......
        
    #include "mainwindow.h"
    #include 
    #include 
    #include 
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        layoutConfig();
        scanSerialPort();
        baudRateInit();
        dataBitsInit();
        parityInit();
        stopBitsInit();
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    //
    void MainWindow::layoutConfig(void)
    {
    
        QList <QScreen *> list_screen =  QGuiApplication::screens();
    
        /* 如果是ARM平台,直接设置大小为屏幕的大小 */
    #if __arm__
        /* 重设大小 */
        this->resize(list_screen.at(0)->geometry().width(),
                     list_screen.at(0)->geometry().height());
    #else
        this->resize(800, 480);
    #endif
    
        setWindowTitle("serialport");
    
        int i;
        /****************左边布局***************/
        pLeftLayout = new QGridLayout();
        /* QList链表,字符串类型 */
        QList <QString> list1;
        list1<<"端口:"<<"波特率:"<<"数据位:"<<"停止位:"<<"校验:";
    
        for(i=0;i<5;i++)
        {
            pLabel[i] = new QLabel(list1[i]);
            pComboBox[i] = new QComboBox();
    
            pLeftLayout->addWidget(pLabel[i],i,0);
            pLeftLayout->addWidget(pComboBox[i],i,1);
        }
    
        pPushButton[0] = new QPushButton("打开串口");
        pPushButton[1] = new QPushButton("发送");
        pPushButton[2] = new QPushButton("清空数据");
        pPushButton[1]->setEnabled(false);
    
        pLeftLayout->addWidget(pPushButton[0],5,0,1,2);
        pLeftLayout->addWidget(pPushButton[1],6,0,1,2);
        pLeftLayout->addWidget(pPushButton[2],7,0,1,2);
    
        pLeftLayout->setColumnStretch(0,1);
        pLeftLayout->setColumnStretch(1,3);
    
    
        /****************右边布局***************/
        pRightLayout = new QVBoxLayout();
    
        pTextRecive = new QTextBrowser();
        pTextSend = new QTextEdit();
    
        pRightLayout->addWidget(pTextRecive);
        pRightLayout->addWidget(pTextSend);
    
        /****************主体布局***************/
        QHBoxLayout* pMainWidget;
    
        pMainWidget = new QHBoxLayout();
        pMainWidget->addLayout(pLeftLayout);
        pMainWidget->addLayout(pRightLayout);
        pMainWidget->setStretch(0,1);
        pMainWidget->setStretch(1,4);
    
        mainWidget = new QWidget();
        mainWidget->setLayout(pMainWidget);
        this->setCentralWidget(mainWidget);
    
    
        /* 信号槽连接 */
        connect(pPushButton[0], SIGNAL(clicked()),
                this, SLOT(openSerialPortDevice()));
        connect(pPushButton[1], SIGNAL(clicked()),
                this, SLOT(sendSerialPortData()));
        connect(pPushButton[2], SIGNAL(clicked()),
                this, SLOT(clearTextData()));
    }
    
    void MainWindow::scanSerialPort(void)
    {
        pSerialPort = new QSerialPort(this);
        connect(pSerialPort, SIGNAL(readyRead()),
                this, SLOT(serialPortReadyRead()));
    
        /* 查找可用串口 */
        foreach (const QSerialPortInfo &info,
                QSerialPortInfo::availablePorts())
        {
            pComboBox[0]->addItem(info.portName());
        }
    }
    
    void MainWindow::baudRateInit(void)
    {
        int i;
        QList <QString> list;
        list<<"1200"<<"2400"<<"4800"<<"9600"
        <<"19200"<<"38400"<<"57600"
        <<"115200"<<"230400"<<"460800"
        <<"921600";
    
        for (i = 0; i < 11; i++)
        {
            pComboBox[1]->addItem(list[i]);
        }
        pComboBox[1]->setCurrentIndex(7);
    }
    
    void MainWindow::dataBitsInit(void)
    {
        int i;
        QList <QString> list;
        list<<"5"<<"6"<<"7"<<"8";
    
        for (i = 0; i < 4; i++)
        {
            pComboBox[2]->addItem(list[i]);
        }
        pComboBox[2]->setCurrentIndex(3);
    }
    
    void MainWindow::parityInit(void)
    {
        int i;
        QList <QString> list;
        list<<"None"<<"Even"<<"Odd"<<"Space"<<"Mark";
        for (i = 0; i < 5; i++)
        {
            pComboBox[3]->addItem(list[i]);
        }
        pComboBox[3]->setCurrentIndex(0);
    }
    
    void MainWindow::stopBitsInit(void)
    {
        int i;
        QList <QString> list;
        list<<"1"<<"2";
        for (i = 0; i < 2; i++)
        {
            pComboBox[4]->addItem(list[i]);
        }
        pComboBox[4]->setCurrentIndex(0);
    }
    
    
    void MainWindow::sendSerialPortData()
    {
        /* 获取textEdit数据,转换成utf8格式的字节流 */
        QByteArray data = pTextSend->toPlainText().toUtf8();
        pSerialPort->write(data);
    }
    
    void MainWindow::openSerialPortDevice()
    {
        int i;
        if (pPushButton[0]->text() == "打开串口")
        {
            /* 设置串口名 */
            pSerialPort->setPortName(pComboBox[0]->currentText());
            /* 设置波特率 */
            pSerialPort->setBaudRate(pComboBox[1]->currentText().toInt());
            /* 设置数据位数 */
            switch (pComboBox[2]->currentText().toInt())
            {
                case 5:
                    pSerialPort->setDataBits(QSerialPort::Data5);
                    break;
                case 6:
                    pSerialPort->setDataBits(QSerialPort::Data6);
                    break;
                case 7:
                    pSerialPort->setDataBits(QSerialPort::Data7);
                    break;
                case 8:
                    pSerialPort->setDataBits(QSerialPort::Data8);
                    break;
                default: break;
            }
            /* 设置奇偶校验 */
            switch (pComboBox[3]->currentIndex())
            {
                case 0:
                    pSerialPort->setParity(QSerialPort::NoParity);
                    break;
                case 1:
                    pSerialPort->setParity(QSerialPort::EvenParity);
                    break;
                case 2:
                    pSerialPort->setParity(QSerialPort::OddParity);
                    break;
                case 3:
                    pSerialPort->setParity(QSerialPort::SpaceParity);
                    break;
                case 4:
                    pSerialPort->setParity(QSerialPort::MarkParity);
                    break;
                default: break;
            }
            /* 设置停止位 */
            switch (pComboBox[4]->currentText().toInt())
            {
                case 1:
                    pSerialPort->setStopBits(QSerialPort::OneStop);
                    break;
                case 2:
                    pSerialPort->setStopBits(QSerialPort::TwoStop);
                    break;
                default: break;
            }
            /* 设置流控制 */
            pSerialPort->setFlowControl(QSerialPort::NoFlowControl);
            if (!pSerialPort->open(QIODevice::ReadWrite))
                QMessageBox::about(NULL, "错误",
                                   "串口无法打开!可能串口已经被占用!");
            else
            {
                for (i = 0; i < 5; i++)
                {
                    pComboBox[i]->setEnabled(false);
                }
                pPushButton[0]->setText("关闭串口");
                pPushButton[1]->setEnabled(true);
            }
        }
        else
        {
            pSerialPort->close();
            for (i = 0; i < 5; i++)
            {
                pComboBox[i]->setEnabled(true);
            }
            pPushButton[0]->setText("打开串口");
            pPushButton[1]->setEnabled(false);
        }
    }
    
    void MainWindow::clearTextData(void)
    {
      pTextRecive->clearHistory();
      pTextSend->clear();
    }
    
    void MainWindow::serialPortReadyRead()
    {
        /* 接收缓冲区中读取数据 */
        QByteArray buf = pSerialPort->readAll();
        pTextRecive->insertPlainText(QString(buf));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337

    ubuntu UI显示效果如下(可正常使用):

    请添加图片描述

    windows UI 显示效果如下(可正常使用):

    请添加图片描述

    交叉编译输出适合arm的执行文件,将执行文件拷贝到开发板文件系统,启动开发板执行可执行文件,测试收发情况(注意打开ttymxc2对应开发板串口3):

    请添加图片描述

    请添加图片描述

  • 相关阅读:
    Vue.extend()实现每个页面弹框
    ONNX:Linux系统中使用onnxruntime
    nrf52832 开发板入手笔记:资料搜集
    linux shell程序设计
    图像处理学习笔记-01-绪论
    20212416 2023-2024-2 《移动平台开发与实践》综合实践
    如何理解Go言中的Context?
    conda和pip安装有什么区别
    SSM 框架整合
    字节跳动(抖音)软件测试月薪23K岗、技术总监三面面试题最新出炉
  • 原文地址:https://blog.csdn.net/weixin_38426553/article/details/126794922