|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
1.编辑工程文件(后缀为 .pro 的文件)在 QT += core gui 后添加 serialport。; P8 o6 b; C" F
1 @9 C+ h$ `- |+ k
2.自动获取串口, E$ c6 ~9 H0 D( y5 a
使用 QSerialPortInfo:::availablePorts()获取当前串口,该函数返回容器类 Qlist,用 Qt 定义的关键字 foreach 遍历容器 Qlist 里的串口信息,并将串口信息放到 QStringList 的类对象 serialNamePort,显示到 ui 的串口组件。
# Y0 R) K# K% E0 V# Z1 @6 [{
. _3 L- d# A( G" v- W& l( E{
- @, h8 Y3 G* ^ui->setupUi(this);
# n8 F6 H! W: dQStringList serialNamePort;' _6 Q5 T" [# o# \+ R$ k! p
//遍历:availablePorts()返回的串口信息' b3 e2 n- G8 r% y* K( |
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){
1 z/ Q/ G6 L! K; \+ p( fserialNamePort << info.portName();
1 j/ }0 x6 m1 M# V/ t}1 s% k4 U& S7 _, y! X, A
ui->serialCb->addItems(serialNamePort);
( _- ]) G6 [4 ]. G6 y: ~" T. ~! a6 S}
, G( P7 v" j, B( m% O+ |+ K编译后点击串口选择框,会出现已经连接的串口。5 n" W! A5 V8 K9 ~! z3 K
/ w+ M6 G: p4 q8 q$ |' I
3.打开串口功能和属性设置 ]* m; |# Z5 L( {4 u
步骤一:实例化串口类 QSerialPort 对象 serialPort,对串口的操作就是对 serialPort 对象的操作,调用QSerialPort 封装的成员变量(属性)和成员函数(功能)就能控制串口。* e) j. O! h! }: W+ F$ e
class Example : public QMainWindow
# {- c/ j) y5 ]1 o{
) g) e0 x1 r5 `public:
. i( t0 [0 F6 [2 m- s* x.......... QSerialPort * serialPort;
& d- ?; l% O( k.......... };
. R& L) o% F4 X' o8 Hui(new Ui::Example)
" j" C$ H% N ]$ F! Q6 `# E{
; q6 x! n) s' z8 G( C$ Uui->setupUi(this);
. X3 M3 a9 I) Z% {, E8 z- w......
8 H% N5 @1 z, m, \, S& rserialPort = new QSerialPort;4 C& X: L' \6 o2 s
...... }- E/ ~- W/ K$ d& Q4 _: c8 u( X7 S
步骤二:填充波特率,数据位,停止位,校验位等属性。获取 ui 组件传递过来的串口信息,将串口属性填充到 serialPort 对象。
& Q% ~* ~# W7 |/ R; W 1 x( e5 N) ]* K+ T
步骤三:打开串口,判断是否打开成功。! [# p. y; @( G. j8 X' k
/*打开按钮*/( r0 z- [' o# l
void Example: n_openCb_clicked()
2 u# e- r, u# g{0 W; \8 O0 |7 b% B" U
QSerialPort::BaudRate bauRate; //波特率- V X: k% t: h5 p9 z; o- G
QSerialPort: ataBits dataBits; //数据位
: s/ C+ W3 O1 W- c2 UQSerialPort::StopBits stopBits; //停止位+ e2 @8 ~9 R+ d2 O! H. Q
QSerialPort: arity checkBits; //校验位
0 a: W& b/ m! G! |+ w) k- Y//设置波特率
$ Z y: R9 Y! Z4 ~5 S1 T. ]if (ui->baudCb->currentText() == "4800" ) { bauRate = QSerialPort::Baud4800; }3 I. R$ H7 z6 ?8 _
else if(ui->baudCb->currentText() == "9600" ) { bauRate = QSerialPort::Baud9600; }
8 q) h+ u e' ?0 t; T% felse if(ui->baudCb->currentText() == "115200") { bauRate = QSerialPort::Baud115200;}
' b9 h: g! p1 u- i9 j5 z//设置数据位 v) x# e& Y& v
if (ui->dataCb->currentText() == "5") { dataBits = QSerialPort: ata5;}
' c; i, m& d c8 p( @else if(ui->dataCb->currentText() == "6") { dataBits = QSerialPort: ata6;}; @; `, K* @9 p0 S
else if(ui->dataCb->currentText() == "7") { dataBits = QSerialPort::Data7;}0 ^! t! c$ w3 }0 ~# ] |; p" v
else if(ui->dataCb->currentText() == "8") { dataBits = QSerialPort::Data8;}- \9 p+ I% X1 c3 l
//设置停止位
9 g" C- ]5 s3 U" ` Rif (ui->stopCb->currentText() == "1" ) { stopBits = QSerialPort::OneStop; }* X$ @# C. a7 A& s' e& O
else if(ui->stopCb->currentText() == "1.5" ) { stopBits = QSerialPort::OneAndHalfStop; }, `! S$ `. l' Q& z$ O
else if(ui->stopCb->currentText() == "2" ) { stopBits = QSerialPort::TwoStop; }
/ o2 o) t2 ?+ {6 o; z7 j \//设置校验位
9 }: X% }: o. A% l9 h. `if(ui->checkCb->currentText() == "none" ) { checkBits = QSerialPort::NoParity; }
5 w V5 x; {$ i" O. z! \+ m* m//填充串口对象的属性值) x4 Z% D8 X" V) q
serialPort->setPortName(ui->serialCb->currentText());
z/ z* b5 T, pserialPort->setBaudRate(bauRate);
d$ N# Y* b4 w9 o* w6 `. v4 c7 ?, T0 NserialPort->setDataBits(dataBits);
1 g0 @" n! Y, i! tserialPort->setStopBits(stopBits);9 O# x# W/ _# u- a
serialPort->setParity(checkBits);
$ n8 u4 @& [3 m" {! I' a//设置好属性后打开串口
X8 V+ I/ M# h% ^if(serialPort->open(QIODevice::ReadWrite) == true){5 Q, c1 F# m. _! p# @
QMessageBox::information(this,"提示","成功");- O3 w7 O& { w$ j. S" I6 a' n$ @0 M
}else{
_- I/ ?& s" {# k& f- V8 G- x% yQMessageBox::critical(this,"提示","失败");
$ R) A$ G$ h& t- J7 t o}
I w$ I) S9 _( j q- ?+ L}3 E3 _) Q/ h/ J+ f* K/ @( o# h
4.收发串口数据功能
( l' y5 k* c! L: M0 N) t读数据:每当数据流从串口到达系统一次,就会传到 Qt 应用程序一次,readyRead 信号就会触 发 一次,所以可以用前面章节讲的信号和槽机制将 readyRead 信号和槽函数绑定,然后就可以在槽函数中读取串口数据。槽函数中使用 readAll()读取数据,使用带换行功能的 appendPlainText()显示到 ui 的接收窗口。8 N, s) `2 i* A* a
//类中声明槽函数3 u- d8 R3 O. V: `5 [
private slots:
, R+ E$ c+ L+ `void serialPortReadyRead_Solt(void);
, H: @' V; y# ]/ Q//readyRead 信号和槽函数绑定% C! m/ M8 Z: i( N+ I0 l. @! T$ f: j0 Q
connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortReadyRead_Solt()));
. I9 \4 e; K( l- M+ ]//读串口3 N$ X# I I) B# T6 L; z1 \" I0 J% G
void Example::serialPortReadyRead_Solt(void)
1 G# r' j) R: o; w{
4 Z% \+ n9 Y) y; t0 Q0 Z+ C$ GQString buf;
4 u: N/ p8 E1 }4 b( ybuf = QString(serilaPort->readAll());
7 G1 c9 }1 K; Q( Y: Qui->recvEdit->appendPlainText(buf);
. J( v0 {: Y3 W$ _! v4 g6 W}
2 i" `% ~/ b6 x5 R0 G8 j写数据:获取 ui 界面填写的信息,ui->sendEdit->text(),使用 QSerialPort 的成员函数 write 将数据写到0 r c7 [9 i& m: A5 K, J4 ~- P- N& ?
串口。5 h! P: m4 A9 w
void Widget: n_sendBt_clicked()
0 i: _5 i+ Q4 H{
% }8 r% N4 h' n7 z eserilaPort->write(ui->sendEdit->text().toLocal8Bit().data());
; p1 a, s5 V6 i/ t( u! X}
# v# S2 E. z3 W8 ^5.关闭串口功能
4 d# [: ^. K! \) i: T; b使用 QSerialPort 的成员函数 close()关闭串口。
7 v% w M6 L+ f3 y1 A! ]void Widget: n_closeBt_clicked()
) V& C! @* f3 Q' O6 H5 N9 _* g2 A{+ [/ B# {$ D: _$ o _
serilaPort->close();
2 `% l* T+ D! p1 m8 g}" y; T+ m2 z2 K. m, s M
6.清空发送栏数据! |5 H+ z9 e8 m+ k
调用 ui 组件 lineEdit 的成员函数 clear 即可清空数据。% Q3 M& w% k" C9 c' s
void Widget::on_clearBt_clicked()0 h& Q1 L. ~# x0 L, ]: Y0 K
{
& J \) V0 P) A$ G+ M+ e# eui->recvEdit->clear();
" Z! j" V4 k$ c V}3 y0 t7 r4 Q4 B. c
编译测试,结果如图: s& w# D( |, c$ D7 X1 W
2 _! H R% F% i/ e' W3 Y- V
0 ~; G: x( x$ ^9 t0 M/ O
|
|