QT (USBor其它)串口通信的实现方法

本文详细介绍了在QT中实现串口通信的方法,包括设置串口名、波特率、数据位、校验位、停止位以及流控制。同时,强调了数据发送和接收的注意事项,如字节发送问题和字符与字符串的区别,对于与USB下位机的通信具有参考价值。
摘要由CSDN通过智能技术生成

如果用qt写程序作为上位机,然后通过和usb和下位机通信的时候,就需要用到qt中的串口通信了。本文介绍了QT串口通信的实现方法,感兴趣的小伙伴们可以参考一下

前言:如果用qt写程序作为上位机,然后通过和usb和下位机通信的时候,就需要用到qt中的串口通信了。

使用qt中的串口通信的时候需要用到的两个头文件分别为:

1

2

#include <QtSerialPort/QSerialPort>

#include <QtSerialPort/QSerialPortInfo>

除了加上面两个头文件之外,还需要在工程文件中加下面一行代码:

1

QT  += serialport

我们一般都需要先定义一个全局的串口对象,记得在自己的头文件中添加上:

1

QSerialPort *serial;

到这里我们就可以调用qt串口通信中的函数了,一般来讲qt串口通信需要经过7步:

1、设置串口名(如COM1)

1

2

serial = new QSerialPort;

serial->setPortName(ui->PortBox->currentText());

这里我使用自动寻找可用串口的方法,直接自动设置了

1

2

3

4

5

6

7

8

9

10

foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())

    {

        QSerialPort serial;

        serial.setPort(info);

        if(serial.open(QIODevice::ReadWrite))

        {

            ui->PortBox->addItem(serial.portName());

            serial.close();

        }

    }

2、打开串口

1

serial->open(QIODevice::ReadWrite);

3、设置波特率(如115200)

1

serial->setBaudRate(QSerialPort::Baud115200);//设置波特率为115200

4、设置数据位(如8)

1

serial->setDataBits(QSerialPort::Data8);//设置数据位8

5、设置校验位(如0)

1

serial->setParity(QSerialPort::NoParity); //校验位设置为0

6、设置停止位(如1)

1

serial->setStopBits(QSerialPort::OneStop);//停止位设置为1

7、设置流控制

1

serial->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制

到这里串口通信的设置就完成了,下面我们需要实现对数据的发送和接收

1、连接数据接收槽函数,下位机中一有数据发送过来的时候就会响应这个槽函数

1

QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadData);

2、从上位机发送数据到下位机

1

serial->write(ui->textEdit_2->toPlainText().toLatin1());

主要使用的函数就这些了,我们来看看代码:

1、工程文件SerialPortTool.pro

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

#-------------------------------------------------

#

# Project created by QtCreator 2017-11-17T15:43:04

#

#-------------------------------------------------

QT       += core gui

QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SerialPortTool

TEMPLATE = app

# The following define makes your compiler emit warnings if you use

# any feature of Qt which as been marked as 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 you use 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

FORMS += \

        mainwindow.ui

2、头文件mainwindow.h

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

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QMainWindow>

#include <QDebug>

#include <QtSerialPort/QSerialPort>

#include <QtSerialPort/QSerialPortInfo>

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

private slots:

    void on_OpenSerialButton_clicked();

    void ReadData();

    void on_SendButton_clicked();

private:

    Ui::MainWindow *ui;

    QSerialPort *serial;

};

#endif // MAINWINDOW_H

3、源文件mainwindow.cpp

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

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWid

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值