通过好几天的学习,终于写出了一个用于串口通信的上位机。下面开始介绍串口类的使用。
首先,QT5是自带QSerialPort这个类的,使用时需要在pro文件里面添加一行:
QT += serialport
然后直接引用头文件就可以使用了。
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo>
QSerialPort:提供访问串口的功能
QSerialPortInfo:提供系统中存在的串口的信息
接下来需要创建一个QSerialPort的对象,对串口的名称、波特率、数据位、校验位、停止位等参数进行设置,然后才进行串口读写操作。
大概总结了一下,设置、读、写的过程。
一、设置(举例)
1 QSerialPort *serial = new QSerialPort; 2 //设置串口名
3 serial->setPortName(name); 4 //打开串口
5 serial->open(QIODevice::ReadWrite); 6 //设置波特率
7 serial->setBaudRate(BaudRate); 8 //设置数据位数
9 serial->setDataBits(QSerialPort::Data8); 10 //设置奇偶校验
11 serial->setParity(QSerialPort::NoParity); 12 //设置停止位
13 serial->setStopBits(QSerialPort::OneStop); 14 //设置流控制
15 serial->setFlowControl(QSerialPort::NoFlowControl);
这里设置了串口名为name,打开串口并设置为可读可写,波特率为BaudRate,数据位为8位,没有奇偶校验位,停止位为1位,没有流控制。设置完这些就能进行读写操作了。作为一名新手,发现遇到不懂得可以在QtCreator里面可以选择关键字,按F1打开文档看类、函数等数据的手册。
二、读取数据
1 void MainWindow::Read_Data() 2 { 3 QByteArray buf; 4 buf = serial->readAll(); 5 }
当串口收到数据并且接收完毕后,会发出一个readyRead()的信号,因此只需要编写一个槽函数Read_Data(),设置信号槽,并在槽函数中使用readAll()把收到的数据读到buf中。
三、发送数据
serial->write(data);
使用write函数便可以把字符串data一个个字节发送出去。
使用串口就只需以上步骤,使用完后只需要执行
serial->close();
就可以关闭串口了。我使用了ui界面设计来编写上位机的,界面如下:
代码如下:
1 //mianwindow.h
2 #ifndef MAINWINDOW_H 3 #define MAINWINDOW_H
4 #include <QMainWindow>
5 #include <QDebug>
6 #include <QtSerialPort/QSerialPort>
7 #include <QtSerialPort/QSerialPortInfo>
8 namespace Ui { 9 class MainWindow; 10 } 11 class MainWindow : public QMainWindow 12 { 13 Q_OBJECT 14 public: 15 explicit MainWindow(QWidget *parent = 0); 16 ~MainWindow(); 17 private slots: 18 void on_clearButton_clicked(); 19 void on_sendButton_clicked(); 20 void on_openButton_clicked(); 21 void Read_Data(); 22 private: 23 Ui::MainWindow *ui; 24 QSerialPort *serial; 25 }; 26 #endif // MAINWINDOW_H
1 //mainwindow.c
2 #include "mainwindow.h"
3 #include "ui_mainwindow.h"
4
5 MainWindow::MainWindow(QWidget *parent) : 6 QMainWindow(parent), 7 ui(new Ui::MainWindow) 8 { 9 ui->setupUi(this); 10 //查找可用的串口
11 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) 12 { 13 QSerialPort serial; 14 serial.setPort(info); 15 if(serial.open(QIODevice::ReadWrite)) 16 { 17 ui->PortBox->addItem(serial.portName()); 18 serial.close(); 19 } 20 } 21 //设置波特率下拉菜单默认显示第三项
22 ui->BaudBox->setCurrentIndex(3); 23 //关闭发送按钮的使能
24 ui->sendButton->setEnabled(false); 25 qDebug() << tr("界面设定成功!"); 26 } 27 MainWindow::~MainWindow() 28 { 29 delete ui; 30 } 31 //清空接受窗口
32 void MainWindow::on_clearButton_clicked() 33 { 34 ui->textEdit->clear(); 35 } 36 //发送数据
37 void MainWindow::on_sendButton_clicked() 38 { 39 serial->write(ui->textEdit_2->toPlainText().toLatin1()); 40 } 41 //读取接收到的数据
42 void MainWindow::Read_Data() 43 { 44 QByteArray buf; 45 buf = serial->readAll(); 46 if(!buf.isEmpty()) 47 { 48 QString str = ui->textEdit->toPlainText(); 49 str+=tr(buf); 50 ui->textEdit->clear(); 51 ui->textEdit->append(str); 52 } 53 buf.clear(); 54 } 55 void MainWindow::on_openButton_clicked() 56 { 57 if(ui->openButton->text()==tr("打开串口")) 58 { 59 serial = new QSerialPort; 60 //设置串口名
61 serial->setPortName(ui->PortBox->currentText()); 62 //打开串口
63 serial->open(QIODevice::ReadWrite); 64 //设置波特率
65 serial->setBaudRate(ui->BaudBox->currentText().toInt()); 66 //设置数据位数
67 switch(ui->BitNumBox->currentIndex()) 68 { 69 case 8: serial->setDataBits(QSerialPort::Data8); break; 70 default: break; 71 } 72 //设置奇偶校验
73 switch(ui->ParityBox->currentIndex()) 74 { 75 case 0: serial->setParity(QSerialPort::NoParity); break; 76 default: break; 77 } 78 //设置停止位
79 switch(ui->StopBox->currentIndex()) 80 { 81 case 1: serial->setStopBits(QSerialPort::OneStop); break; 82 case 2: serial->setStopBits(QSerialPort::TwoStop); break; 83 default: break; 84 } 85 //设置流控制
86 serial->setFlowControl(QSerialPort::NoFlowControl); 87 //关闭设置菜单使能
88 ui->PortBox->setEnabled(false); 89 ui->BaudBox->setEnabled(false); 90 ui->BitNumBox->setEnabled(false); 91 ui->ParityBox->setEnabled(false); 92 ui->StopBox->setEnabled(false); 93 ui->openButton->setText(tr("关闭串口")); 94 ui->sendButton->setEnabled(true); 95 //连接信号槽
96 QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::Read_Data); 97 } 98 else
99 { 100 //关闭串口
101 serial->clear(); 102 serial->close(); 103 serial->deleteLater(); 104 //恢复设置使能
105 ui->PortBox->setEnabled(true); 106 ui->BaudBox->setEnabled(true); 107 ui->BitNumBox->setEnabled(true); 108 ui->ParityBox->setEnabled(true); 109 ui->StopBox->setEnabled(true); 110 ui->openButton->setText(tr("打开串口")); 111 ui->sendButton->setEnabled(false); 112 } 113 }
1 //main.c
2 #include "mainwindow.h"
3 #include <QApplication>
4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 MainWindow w; 8 w.show(); 9 return a.exec(); 10 }