您当前的位置: 首页 >  嵌入式

正点原子

暂无认证

  • 4浏览

    0关注

    382博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【正点原子Linux连载】第十六章 Serial Port 摘自【正点原子】I.MX6U嵌入式Qt开发指南V1.0.2

正点原子 发布时间:2022-07-15 11:21:38 ,浏览量:4

第十六章 Serial Port

Qt提供了串口类,可以直接对串口访问。我们可以直接使用Qt的串口类编程即可,十分方便。Qt串口类不仅在Windows能用,还能在Linux下用,虽然串口编程不是什么新鲜事儿,既然Qt提供了这方面的接口,我们就充分利用起来,这将会使我们的开发十分方便!其实Qt也提供了相关的Qt串口的例子,我们也可以直接参考来编程,编者根据实际情况,化繁为易,直接写了个简单的例子给大家参考。

17.1 资源简介

在正点原子的I.MX6U开发板的出厂系统里,默认已经配置了两路串口可用。一路是调试串口UART1(对应系统里的节点/dev/ttymxc0),另一路是UART3(对应系统里的节点/dev/ttymxc2)。由于UART1已经作为调试串口被使用。所以我们只能对UART3编程,(如需要使用多路串口,请自行设计底板与系统)。 17.2 应用实例 项目简介:Qt串口的使用示例,应用到正点原子I.MX6U开发板上。 例03_serialport,Qt串口编程(难度:一般)。项目路径为Qt/3/03_serialport。 在03_serialport.pro里,我们需要使用串口,需要在pro项目文件中添加串口模块的支持,如下。

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

第2行,添加的serialport就是串口模块的支持。 在头文件“mainwindow.h”的代码如下。

   /******************************************************************
    Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
    * @projectName   03_serialport
    * @brief         mainwindow.h
    * @author        Deng Zhimao
    * @email         1252699831@qq.com
    * @net            www.openedv.com
    * @date           2021-03-12
    *******************************************************************/
1   #ifndef MAINWINDOW_H
2   #define MAINWINDOW_H
3 
4   #include 
5   #include 
6   #include 
7   #include 
8   #include 
9   #include 
10  #include 
11  #include 
12  #include 
13  #include 
14  #include 
15  #include 
16
17  class MainWindow : public QMainWindow
18  {
19      Q_OBJECT
20
21  public:
22      MainWindow(QWidget *parent = nullptr);
23      ~MainWindow();
24
25  private:
26      /* 串口对象 */
27      QSerialPort *serialPort;
28
29      /* 用作接收数据 */
30      QTextBrowser *textBrowser;
31
32      /* 用作发送数据 */
33      QTextEdit *textEdit;
34
35      /* 按钮 */
36      QPushButton *pushButton[2];
37
38      /* 下拉选择盒子 */
39      QComboBox *comboBox[5];
40
41      /* 标签 */
42      QLabel *label[5];
43
44      /* 垂直布局 */
45      QVBoxLayout *vboxLayout;
46
47      /* 网络布局 */
48      QGridLayout *gridLayout;
49
50      /* 主布局 */
51      QWidget *mainWidget;
52
53      /* 设置功能区域 */
54      QWidget *funcWidget;
55
56      /* 布局初始化 */
57      void layoutInit();
58
59      /* 扫描系统可用串口 */
60      void scanSerialPort();
61
62      /* 波特率项初始化 */
63      void baudRateItemInit();
64
65      /* 数据位项初始化 */
66      void dataBitsItemInit();
67
68      /* 检验位项初始化 */
69      void parityItemInit();
70
71      /* 停止位项初始化 */
72      void stopBitsItemInit();
73
74  private slots:
75      void sendPushButtonClicked();
76      void openSerialPortPushButtonClicked();
77      void serialPortReadyRead();
78  };
79  #endif // MAINWINDOW_H

上面代码是在mianwindow.h里声明需要用到的变量,方法及槽函数。 mainwindow.cpp的代码如下。

    /******************************************************************
    Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
    * @projectName   03_serialport
    * @brief         mainwindow.cpp
    * @author        Deng Zhimao
    * @email         1252699831@qq.com
    * @net            www.openedv.com
    * @date           2021-03-12
    *******************************************************************/
1   #include "mainwindow.h"
2   #include 
3   #include 
4   #include 
5   #include 
6  
7   MainWindow::MainWindow(QWidget *parent)
8       : QMainWindow(parent)
9   {
10      /* 布局初始化 */
11      layoutInit();
12 
13      /* 扫描系统的串口 */
14      scanSerialPort();
15 
16      /* 波特率项初始化 */
17      baudRateItemInit();
18 
19      /* 数据位项初始化 */
20      dataBitsItemInit();
21 
22      /* 检验位项初始化 */
23      parityItemInit();
24 
25      /* 停止位项初始化 */
26      stopBitsItemInit();
27  }
28 
29  void MainWindow::layoutInit()
30  {
31      /* 获取屏幕的分辨率,Qt官方建议使用这
32       * 种方法获取屏幕分辨率,防上多屏设备导致对应不上
33       * 注意,这是获取整个桌面系统的分辨率
34       */
35      QList  list_screen =  QGuiApplication::screens();
36 
37      /* 如果是ARM平台,直接设置大小为屏幕的大小 */
38  #if __arm__
39      /* 重设大小 */
40      this->resize(list_screen.at(0)->geometry().width(),
41                   list_screen.at(0)->geometry().height());
42  #else
43      /* 否则则设置主窗体大小为800x480 */
44      this->resize(800, 480);
45  #endif
46      /* 初始化 */
47      serialPort = new QSerialPort(this);
48      textBrowser = new QTextBrowser();
49      textEdit = new QTextEdit();
50      vboxLayout = new QVBoxLayout();
51      funcWidget = new QWidget();
52      mainWidget = new QWidget();
53      gridLayout = new QGridLayout();
54 
55      /* QList链表,字符串类型 */
56      QList  list1;
57      list1setPlaceholderText("接收到的消息");
111     textEdit->setText("www.openedv.com");
112
113     /* 信号槽连接 */
114     connect(pushButton[0], SIGNAL(clicked()),
115             this, SLOT(sendPushButtonClicked()));
116     connect(pushButton[1], SIGNAL(clicked()),
117             this, SLOT(openSerialPortPushButtonClicked()));
118
119     connect(serialPort, SIGNAL(readyRead()),
120             this, SLOT(serialPortReadyRead()));
121 }
122
123 void MainWindow::scanSerialPort()
124 {
125     /* 查找可用串口 */
126     foreach (const QSerialPortInfo &info,
127             QSerialPortInfo::availablePorts()) {
128         comboBox[0]->addItem(info.portName());
129     }
130 }
131
132 void MainWindow::baudRateItemInit()
133 {
134     /* QList链表,字符串类型 */
135     QList  list;
136     list            
关注
打赏
1665308814
查看更多评论
0.3629s