├── README.md
├── other
└── processing.png
└── src
├── Com.pro
├── Com.pro.user
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
└── mainwindow.ui
/README.md:
--------------------------------------------------------------------------------
1 | # Qt SerialPort Programming Demo
2 | 基于Qt的串口通信演示
3 | ##程序截图
4 | 
5 |
--------------------------------------------------------------------------------
/other/processing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/UtoYuri/Qt-SerialPort-Programming-Demo/f51432cbb48b390dc85e67a2aa9fde5050f84e71/other/processing.png
--------------------------------------------------------------------------------
/src/Com.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2016-07-23T16:37:40
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui serialport
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = Com
12 | TEMPLATE = app
13 |
14 |
15 | SOURCES += main.cpp\
16 | mainwindow.cpp
17 |
18 | HEADERS += mainwindow.h
19 |
20 | FORMS += mainwindow.ui
21 |
--------------------------------------------------------------------------------
/src/Com.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {a32dd7d9-feca-46ff-b9ca-bdb019bff7c2}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 80
37 | true
38 | true
39 | 1
40 | true
41 | false
42 | 0
43 | true
44 | true
45 | 0
46 | 8
47 | true
48 | 0
49 | true
50 | true
51 | true
52 | false
53 |
54 |
55 |
56 | ProjectExplorer.Project.PluginSettings
57 |
58 |
59 |
60 | ProjectExplorer.Project.Target.0
61 |
62 | Desktop Qt 5.7.0 MSVC2015 64bit
63 | Desktop Qt 5.7.0 MSVC2015 64bit
64 | {2adcaccb-144d-4731-aa8f-98e977141046}
65 | 0
66 | 0
67 | 0
68 |
69 | C:/Users/walking-frog/Desktop/Com/build-Com-Desktop_Qt_5_7_0_MSVC2015_64bit_2adcac-Debug
70 |
71 |
72 | true
73 | qmake
74 |
75 | QtProjectManager.QMakeBuildStep
76 | true
77 |
78 | false
79 | false
80 | false
81 |
82 |
83 | true
84 | Make
85 |
86 | Qt4ProjectManager.MakeStep
87 |
88 | false
89 |
90 |
91 |
92 | 2
93 | 构建
94 |
95 | ProjectExplorer.BuildSteps.Build
96 |
97 |
98 |
99 | true
100 | Make
101 |
102 | Qt4ProjectManager.MakeStep
103 |
104 | true
105 | clean
106 |
107 |
108 | 1
109 | 清理
110 |
111 | ProjectExplorer.BuildSteps.Clean
112 |
113 | 2
114 | false
115 |
116 | Debug
117 |
118 | Qt4ProjectManager.Qt4BuildConfiguration
119 | 2
120 | true
121 |
122 |
123 | C:/Users/walking-frog/Desktop/Com/build-Com-Desktop_Qt_5_7_0_MSVC2015_64bit_2adcac-Release
124 |
125 |
126 | true
127 | qmake
128 |
129 | QtProjectManager.QMakeBuildStep
130 | false
131 |
132 | false
133 | false
134 | false
135 |
136 |
137 | true
138 | Make
139 |
140 | Qt4ProjectManager.MakeStep
141 |
142 | false
143 |
144 |
145 |
146 | 2
147 | 构建
148 |
149 | ProjectExplorer.BuildSteps.Build
150 |
151 |
152 |
153 | true
154 | Make
155 |
156 | Qt4ProjectManager.MakeStep
157 |
158 | true
159 | clean
160 |
161 |
162 | 1
163 | 清理
164 |
165 | ProjectExplorer.BuildSteps.Clean
166 |
167 | 2
168 | false
169 |
170 | Release
171 |
172 | Qt4ProjectManager.Qt4BuildConfiguration
173 | 0
174 | true
175 |
176 |
177 | C:/Users/walking-frog/Desktop/Com/build-Com-Desktop_Qt_5_7_0_MSVC2015_64bit_2adcac-Profile
178 |
179 |
180 | true
181 | qmake
182 |
183 | QtProjectManager.QMakeBuildStep
184 | true
185 |
186 | false
187 | true
188 | false
189 |
190 |
191 | true
192 | Make
193 |
194 | Qt4ProjectManager.MakeStep
195 |
196 | false
197 |
198 |
199 |
200 | 2
201 | 构建
202 |
203 | ProjectExplorer.BuildSteps.Build
204 |
205 |
206 |
207 | true
208 | Make
209 |
210 | Qt4ProjectManager.MakeStep
211 |
212 | true
213 | clean
214 |
215 |
216 | 1
217 | 清理
218 |
219 | ProjectExplorer.BuildSteps.Clean
220 |
221 | 2
222 | false
223 |
224 | Profile
225 |
226 | Qt4ProjectManager.Qt4BuildConfiguration
227 | 0
228 | true
229 |
230 | 3
231 |
232 |
233 | 0
234 | 部署
235 |
236 | ProjectExplorer.BuildSteps.Deploy
237 |
238 | 1
239 | 在本地部署
240 |
241 | ProjectExplorer.DefaultDeployConfiguration
242 |
243 | 1
244 |
245 |
246 | false
247 | false
248 | 1000
249 |
250 | true
251 |
252 | false
253 | false
254 | false
255 | false
256 | true
257 | 0.01
258 | 10
259 | true
260 | 1
261 | 25
262 |
263 | 1
264 | true
265 | false
266 | true
267 | valgrind
268 |
269 | 0
270 | 1
271 | 2
272 | 3
273 | 4
274 | 5
275 | 6
276 | 7
277 | 8
278 | 9
279 | 10
280 | 11
281 | 12
282 | 13
283 | 14
284 |
285 | 2
286 |
287 | Com
288 |
289 | Qt4ProjectManager.Qt4RunConfiguration:C:/Users/walking-frog/Desktop/Com/src/Com.pro
290 | true
291 |
292 | Com.pro
293 | false
294 |
295 |
296 | 3768
297 | false
298 | true
299 | false
300 | false
301 | true
302 |
303 | 1
304 |
305 |
306 |
307 | ProjectExplorer.Project.TargetCount
308 | 1
309 |
310 |
311 | ProjectExplorer.Project.Updater.FileVersion
312 | 18
313 |
314 |
315 | Version
316 | 18
317 |
318 |
319 |
--------------------------------------------------------------------------------
/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/src/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 |
10 | this->serialPort = new QSerialPort;
11 | findFreePorts();
12 |
13 | connect(ui->openCom, &QCheckBox::toggled, [=](bool checked){
14 | if (checked){
15 | initSerialPort();
16 | ui->btnSend->setEnabled(true);
17 | }else{
18 | this->serialPort->close();
19 | ui->btnSend->setEnabled(false);
20 | ui->openCom->setChecked(false);
21 | }
22 | });
23 |
24 | connect(this->serialPort, SIGNAL(readyRead()), this, SLOT(recvMsg()));
25 | connect(ui->btnSend, &QPushButton::clicked, [=](){
26 | sendMsg(ui->message->toPlainText());
27 | });
28 | }
29 |
30 | MainWindow::~MainWindow()
31 | {
32 | delete ui;
33 | }
34 |
35 | //寻找空闲状态串口
36 | void MainWindow::findFreePorts(){
37 | QList ports = QSerialPortInfo::availablePorts();
38 | for (int i = 0; i < ports.size(); ++i){
39 | if (ports.at(i).isBusy()){
40 | ports.removeAt(i);
41 | continue;
42 | }
43 | ui->portName->addItem(ports.at(i).portName());
44 | }
45 | if (!ports.size()){
46 | QMessageBox::warning(NULL,"Tip",QStringLiteral("找不到空闲串口"));
47 | return;
48 | }
49 | }
50 | //初始化串口
51 | bool MainWindow::initSerialPort(){
52 | this->serialPort->setPortName(ui->portName->currentText());
53 | if (!this->serialPort->open(QIODevice::ReadWrite)){
54 | QMessageBox::warning(NULL,"Tip",QStringLiteral("串口打开失败"));
55 | return false;
56 | }
57 | this->serialPort->setBaudRate(ui->baudRate->currentText().toInt());
58 |
59 | if (ui->dataBits->currentText().toInt() == 8){
60 | this->serialPort->setDataBits(QSerialPort::Data8);
61 | }else if (ui->dataBits->currentText().toInt() == 7){
62 | this->serialPort->setDataBits(QSerialPort::Data7);
63 | }else if (ui->dataBits->currentText().toInt() == 6){
64 | this->serialPort->setDataBits(QSerialPort::Data6);
65 | }else if (ui->dataBits->currentText().toInt() == 5){
66 | this->serialPort->setDataBits(QSerialPort::Data5);
67 | }
68 |
69 | if (ui->stopBits->currentText().toInt() == 1){
70 | this->serialPort->setStopBits(QSerialPort::OneStop);
71 | }else if (ui->stopBits->currentText().toInt() == 2){
72 | this->serialPort->setStopBits(QSerialPort::TwoStop);
73 | }
74 |
75 |
76 | if(ui->parity->currentText() == "NoParity"){
77 | this->serialPort->setParity(QSerialPort::NoParity);
78 | }else if (ui->parity->currentText() == "EvenParity"){
79 | this->serialPort->setParity(QSerialPort::EvenParity);
80 | }else if (ui->parity->currentText() == "OddParity"){
81 | this->serialPort->setParity(QSerialPort::OddParity);
82 | }
83 | return true;
84 | }
85 | //向串口发送信息
86 | void MainWindow::sendMsg(const QString &msg){
87 | this->serialPort->write(QByteArray::fromHex(msg.toLatin1()));
88 | ui->comLog->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " [send] " + msg + "\n");
89 | }
90 | //接受来自串口的信息
91 | void MainWindow::recvMsg(){
92 | QByteArray msg = this->serialPort->readAll();
93 | //do something
94 | ui->comLog->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " [recieve] " + msg.toHex().data() + "\n");
95 | }
96 |
--------------------------------------------------------------------------------
/src/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include
9 | #include
10 | #include
11 |
12 | namespace Ui {
13 | class MainWindow;
14 | }
15 |
16 | class MainWindow : public QMainWindow
17 | {
18 | Q_OBJECT
19 |
20 | public:
21 | explicit MainWindow(QWidget *parent = 0);
22 | ~MainWindow();
23 |
24 | protected:
25 | void findFreePorts();
26 | bool initSerialPort();
27 | void sendMsg(const QString &msg);
28 |
29 | public slots:
30 | void recvMsg();
31 |
32 | private:
33 | Ui::MainWindow *ui;
34 |
35 | QSerialPort *serialPort;
36 | };
37 |
38 | #endif // MAINWINDOW_H
39 |
--------------------------------------------------------------------------------
/src/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 660
10 | 494
11 |
12 |
13 |
14 | Qt串口通信
15 |
16 |
17 |
18 |
19 |
20 | 10
21 | 10
22 | 361
23 | 351
24 |
25 |
26 |
27 | 通信日志
28 |
29 |
30 |
31 |
32 | 10
33 | 20
34 | 341
35 | 321
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | 380
44 | 10
45 | 271
46 | 351
47 |
48 |
49 |
50 | 串口参数
51 |
52 |
53 |
54 |
55 | 30
56 | 300
57 | 131
58 | 31
59 |
60 |
61 |
62 | 打开串口
63 |
64 |
65 |
66 |
67 |
68 | 20
69 | 30
70 | 231
71 | 261
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 | 8
103 |
104 |
105 | -
106 |
107 | 7
108 |
109 |
110 | -
111 |
112 | 6
113 |
114 |
115 | -
116 |
117 | 5
118 |
119 |
120 |
121 |
122 |
123 |
124 | -
125 |
126 |
-
127 |
128 |
129 | 波特率:
130 |
131 |
132 |
133 | -
134 |
135 |
-
136 |
137 | 9600
138 |
139 |
140 | -
141 |
142 | 4800
143 |
144 |
145 | -
146 |
147 | 19200
148 |
149 |
150 | -
151 |
152 | 38400
153 |
154 |
155 |
156 |
157 |
158 |
159 | -
160 |
161 |
-
162 |
163 |
164 | 停止位:
165 |
166 |
167 |
168 | -
169 |
170 |
-
171 |
172 | 1
173 |
174 |
175 | -
176 |
177 | 2
178 |
179 |
180 |
181 |
182 |
183 |
184 | -
185 |
186 |
-
187 |
188 |
189 | 校验位:
190 |
191 |
192 |
193 | -
194 |
195 |
-
196 |
197 | NoParity
198 |
199 |
200 | -
201 |
202 | EvenParity
203 |
204 |
205 | -
206 |
207 | OddParity
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 | 10
221 | 370
222 | 641
223 | 111
224 |
225 |
226 |
227 | 发送
228 |
229 |
230 |
231 |
232 | 10
233 | 20
234 | 621
235 | 81
236 |
237 |
238 |
239 | -
240 |
241 |
242 | -
243 |
244 |
245 | false
246 |
247 |
248 | 发送
249 |
250 |
251 |
252 |
253 |
254 | message
255 | message
256 | btnSend
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
--------------------------------------------------------------------------------