Qt串口通信
简单了解一下串口通信
概念非常简单,串口按位(Bit)发送和接收字节;可以在使用一根线发送数据的同时使用另一根线接收数据。串口用于ASCII码字符的传输。通信使用3根线完成,分别是地线、发送、接收。由于串口通信是异步的,端口能够在一根线上发送数据同时在另一根线上接收数据。其他线用于握手,但不是必须的。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通信的端口,这些参数必须匹配。
(1)波特率:衡量符号传输速率的参数(单位时间内载波参数变化的次数)
如每秒钟传送240个字符,而每个字符格式包含10位(1个起始位,1个停止位,8个数据位),这时的波特率为240Bd,比特率为10位*240个/秒=2400bps。一般调制速率大于波特率,比如曼彻斯特编码)。通常电话线的波特率为14400,28800和36600。波特率可以远远大于这些值。
(2)数据位:衡量通信中实际数据位的参数
当计算机发送一个信息包,实际的数据往往不会是8位的,标准的值是6、7和8位。如何设置取决于你想传送的信息。
(3)停止位:表示单个包的最后一位(典型值1、1.5、2位)
停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。适用于停止位的位数越多,不同时钟同步的容忍程度越大,但是数据传输率同时也越慢。
(4)奇偶校验位:串口通信中一种简单的检错方式(检错方式:偶、奇、高、低)
当然没有校验位也是可以的。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位
实例应用
效果图如下:
上代码(含注释)
在.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
FORMS += \
mainwindow. ui
# Default rules for deployment.
qnx: target. path = / tmp/ $${ TARGET} / bin
else : unix: ! android: target. path = / opt/ $${ TARGET} / bin
! isEmpty ( target. path) : INSTALLS += target
mainwindow.h
# ifndef MAINWINDOW_H
# define MAINWINDOW_H
# include <QMainWindow>
# include <QSerialPort>
# include <QSerialPortInfo>
# include <QList>
# include <QMessageBox>
# include <QDateTime>
namespace Ui {
class MainWindow ;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public :
explicit MainWindow ( QWidget * parent = 0 ) ;
~ MainWindow ( ) ;
protected :
void findFreePorts ( ) ;
bool initSerialPort ( ) ;
void sendMsg ( const QString & msg) ;
public slots:
void recvMsg ( ) ;
private :
Ui:: MainWindow * ui;
QSerialPort * serialPort;
} ;
# endif
mainwindow.cpp
# include "mainwindow.h"
# include "ui_mainwindow.h"
MainWindow :: MainWindow ( QWidget * parent) :
QMainWindow ( parent) ,
ui ( new Ui:: MainWindow)
{
ui-> setupUi ( this ) ;
ui-> baudRate-> setCurrentText ( "115200" ) ;
this -> serialPort = new QSerialPort;
findFreePorts ( ) ;
connect ( ui-> openCom, & QCheckBox:: toggled, [ = ] ( bool checked) {
if ( checked) {
initSerialPort ( ) ;
ui-> btnSend-> setEnabled ( true ) ;
} else {
this -> serialPort-> close ( ) ;
ui-> btnSend-> setEnabled ( false ) ;
ui-> openCom-> setChecked ( false ) ;
}
} ) ;
connect ( this -> serialPort, SIGNAL ( readyRead ( ) ) , this , SLOT ( recvMsg ( ) ) ) ;
connect ( ui-> btnSend, & QPushButton:: clicked, [ = ] ( ) {
sendMsg ( ui-> message-> toPlainText ( ) ) ;
} ) ;
}
MainWindow :: ~ MainWindow ( )
{
delete ui;
}
void MainWindow :: findFreePorts ( ) {
QList< QSerialPortInfo> ports = QSerialPortInfo :: availablePorts ( ) ;
for ( int i = 0 ; i < ports. size ( ) ; ++ i) {
if ( ports. at ( i) . isBusy ( ) ) {
ports. removeAt ( i) ;
continue ;
}
ui-> portName-> addItem ( ports. at ( i) . portName ( ) ) ;
}
if ( ! ports. size ( ) ) {
QMessageBox :: warning ( NULL , "Tip" , QStringLiteral ( "找不到空闲串口" ) ) ;
return ;
}
}
bool MainWindow :: initSerialPort ( ) {
this -> serialPort-> setPortName ( ui-> portName-> currentText ( ) ) ;
if ( ! this -> serialPort-> open ( QIODevice:: ReadWrite) ) {
QMessageBox :: warning ( NULL , "Tip" , QStringLiteral ( "串口打开失败" ) ) ;
return false ;
}
this -> serialPort-> setBaudRate ( ui-> baudRate-> currentText ( ) . toInt ( ) ) ;
if ( ui-> dataBits-> currentText ( ) . toInt ( ) == 8 ) {
this -> serialPort-> setDataBits ( QSerialPort:: Data8) ;
} else if ( ui-> dataBits-> currentText ( ) . toInt ( ) == 7 ) {
this -> serialPort-> setDataBits ( QSerialPort:: Data7) ;
} else if ( ui-> dataBits-> currentText ( ) . toInt ( ) == 6 ) {
this -> serialPort-> setDataBits ( QSerialPort:: Data6) ;
} else if ( ui-> dataBits-> currentText ( ) . toInt ( ) == 5 ) {
this -> serialPort-> setDataBits ( QSerialPort:: Data5) ;
}
if ( ui-> stopBits-> currentText ( ) . toInt ( ) == 1 ) {
this -> serialPort-> setStopBits ( QSerialPort:: OneStop) ;
} else if ( ui-> stopBits-> currentText ( ) . toInt ( ) == 2 ) {
this -> serialPort-> setStopBits ( QSerialPort:: TwoStop) ;
}
if ( ui-> parity-> currentText ( ) == "NoParity" ) {
this -> serialPort-> setParity ( QSerialPort:: NoParity) ;
} else if ( ui-> parity-> currentText ( ) == "EvenParity" ) {
this -> serialPort-> setParity ( QSerialPort:: EvenParity) ;
} else if ( ui-> parity-> currentText ( ) == "OddParity" ) {
this -> serialPort-> setParity ( QSerialPort:: OddParity) ;
}
return true ;
}
void MainWindow :: sendMsg ( const QString & msg) {
this -> serialPort-> write ( QByteArray :: fromHex ( msg. toLatin1 ( ) ) ) ;
ui-> comLog-> insertPlainText ( QDateTime :: currentDateTime ( ) . toString ( "yyyy-MM-dd hh:mm:ss" ) + " [send] " + msg + "\n" ) ;
}
void MainWindow :: recvMsg ( ) {
QByteArray msg = this -> serialPort-> readAll ( ) ;
ui-> comLog-> insertPlainText ( QDateTime :: currentDateTime ( ) . toString ( "yyyy-MM-dd hh:mm:ss" ) + " [recieve] " + msg. toHex ( ) . data ( ) + "\n" ) ;
}
mainwindow.ui
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ui version= "4.0" >
< class > MainWindow< / class >
< widget class = "QMainWindow" name= "MainWindow" >
< property name= "geometry" >
< rect>
< x> 0 < / x>
< y> 0 < / y>
< width> 660 < / width>
< height> 494 < / height>
< / rect>
< / property>
< property name= "windowTitle" >
< string> Qt串口通信< / string>
< / property>
< widget class = "QWidget" name= "centralWidget" >
< widget class = "QGroupBox" name= "groupBox" >
< property name= "geometry" >
< rect>
< x> 10 < / x>
< y> 10 < / y>
< width> 361 < / width>
< height> 351 < / height>
< / rect>
< / property>
< property name= "title" >
< string> 通信日志< / string>
< / property>
< widget class = "QTextBrowser" name= "comLog" >
< property name= "geometry" >
< rect>
< x> 10 < / x>
< y> 20 < / y>
< width> 341 < / width>
< height> 321 < / height>
< / rect>
< / property>
< / widget>
< / widget>
< widget class = "QGroupBox" name= "groupBox_2" >
< property name= "geometry" >
< rect>
< x> 380 < / x>
< y> 10 < / y>
< width> 271 < / width>
< height> 351 < / height>
< / rect>
< / property>
< property name= "title" >
< string> 串口参数< / string>
< / property>
< widget class = "QCheckBox" name= "openCom" >
< property name= "geometry" >
< rect>
< x> 30 < / x>
< y> 300 < / y>
< width> 131 < / width>
< height> 31 < / height>
< / rect>
< / property>
< property name= "text" >
< string> 打开串口< / string>
< / property>
< / widget>
< widget class = "QWidget" name= "layoutWidget" >
< property name= "geometry" >
< rect>
< x> 20 < / x>
< y> 30 < / y>
< width> 231 < / width>
< height> 261 < / height>
< / rect>
< / property>
< layout class = "QVBoxLayout" name= "verticalLayout" >
< item>
< layout class = "QHBoxLayout" name= "horizontalLayout" >
< item>
< widget class = "QLabel" name= "label" >
< property name= "text" >
< string> 串口:< / string>
< / property>
< / widget>
< / item>
< item>
< widget class = "QComboBox" name= "portName" / >
< / item>
< / layout>
< / item>
< item>
< layout class = "QHBoxLayout" name= "horizontalLayout_5" >
< item>
< widget class = "QLabel" name= "label_5" >
< property name= "text" >
< string> 数据位:< / string>
< / property>
< / widget>
< / item>
< item>
< widget class = "QComboBox" name= "dataBits" >
< item>
< property name= "text" >
< string> 8 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 7 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 6 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 5 < / string>
< / property>
< / item>
< / widget>
< / item>
< / layout>
< / item>
< item>
< layout class = "QHBoxLayout" name= "horizontalLayout_2" >
< item>
< widget class = "QLabel" name= "label_2" >
< property name= "text" >
< string> 波特率:< / string>
< / property>
< / widget>
< / item>
< item>
< widget class = "QComboBox" name= "baudRate" >
< item>
< property name= "text" >
< string> 9600 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 4800 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 19200 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 38400 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 115200 < / string>
< / property>
< / item>
< / widget>
< / item>
< / layout>
< / item>
< item>
< layout class = "QHBoxLayout" name= "horizontalLayout_4" >
< item>
< widget class = "QLabel" name= "label_4" >
< property name= "text" >
< string> 停止位:< / string>
< / property>
< / widget>
< / item>
< item>
< widget class = "QComboBox" name= "stopBits" >
< item>
< property name= "text" >
< string> 1 < / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> 2 < / string>
< / property>
< / item>
< / widget>
< / item>
< / layout>
< / item>
< item>
< layout class = "QHBoxLayout" name= "horizontalLayout_3" >
< item>
< widget class = "QLabel" name= "label_3" >
< property name= "text" >
< string> 校验位:< / string>
< / property>
< / widget>
< / item>
< item>
< widget class = "QComboBox" name= "parity" >
< item>
< property name= "text" >
< string> NoParity< / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> EvenParity< / string>
< / property>
< / item>
< item>
< property name= "text" >
< string> OddParity< / string>
< / property>
< / item>
< / widget>
< / item>
< / layout>
< / item>
< / layout>
< / widget>
< / widget>
< widget class = "QGroupBox" name= "groupBox_3" >
< property name= "geometry" >
< rect>
< x> 10 < / x>
< y> 370 < / y>
< width> 641 < / width>
< height> 111 < / height>
< / rect>
< / property>
< property name= "title" >
< string> 发送< / string>
< / property>
< widget class = "QWidget" name= "layoutWidget" >
< property name= "geometry" >
< rect>
< x> 10 < / x>
< y> 20 < / y>
< width> 621 < / width>
< height> 81 < / height>
< / rect>
< / property>
< layout class = "QHBoxLayout" name= "horizontalLayout_6" >
< item>
< widget class = "QTextEdit" name= "message" / >
< / item>
< item>
< widget class = "QPushButton" name= "btnSend" >
< property name= "enabled" >
< bool > false < / bool >
< / property>
< property name= "text" >
< string> 发送< / string>
< / property>
< / widget>
< / item>
< / layout>
< / widget>
< / widget>
< / widget>
< / widget>
< layoutdefault spacing= "6" margin= "11" / >
< resources/ >
< connections/ >
< / ui>
在做知识整理的同时,希望对大家有所帮助!!!