forked from karlphillip/GraphicsProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·88 lines (74 loc) · 3.44 KB
/
main.cpp
File metadata and controls
executable file
·88 lines (74 loc) · 3.44 KB
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
/* Copyright (C) 2013-2015 Karl Phillip Buhr <karlphillip@gmail.com>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "serialportreader.h"
#include <iostream>
#include <QCoreApplication>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
int main(int argc, char *argv[])
{
QCoreApplication coreApplication(argc, argv);
if (!QSerialPortInfo::availablePorts().count())
{
std::cout << "!!! No serial ports found!" << std::endl;
return 0;
}
int option = -1;
while (1)
{
std::cout << "===== List of available serial ports (" <<
QSerialPortInfo::availablePorts().count() << ") =====" << std::endl;
std::cout << "[0] - Exit" << std::endl;
for (int i = 0; i < QSerialPortInfo::availablePorts().count(); i++)
{
QSerialPortInfo serial_info = QSerialPortInfo::availablePorts()[i];
std::cout << "[" << i+1 << "] - " << serial_info.portName().toStdString() << std::endl;
std::cout << "\tLocation: " << serial_info.systemLocation().toStdString() << std::endl;
std::cout << "\tDescription: " << serial_info.description().toStdString() << std::endl;
std::cout << "\tManufacturer: " << serial_info.manufacturer().toStdString() << std::endl;
std::cout << "\tVendor Identifier: " << (serial_info.hasVendorIdentifier() ?
QString::number(serial_info.vendorIdentifier()).toStdString() :
"") << std::endl;
std::cout << "\tProduct Identifier: " << (serial_info.hasProductIdentifier() ?
QString::number(serial_info.productIdentifier()).toStdString() :
"") << std::endl;
std::cout << "\tBusy: " << (serial_info.isBusy() ? "Yes" : "No") << std::endl;
}
std::cout << "\nYour choice: ";
std::cin >> option;
if (option >= 0 && option <= QSerialPortInfo::availablePorts().count())
break;
std::cout << std::endl;
}
if (option == 0)
{
std::cout << "* OK, bye bye!" << std::endl;
return 0;
}
QSerialPortInfo serial_info = QSerialPortInfo::availablePorts()[option-1];
std::cout << "* OK, " << serial_info.portName().toStdString() <<
" selected!" << std::endl;
QSerialPort serial;
serial.setPortName(serial_info.portName());
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setStopBits(QSerialPort::OneStop);
serial.setParity(QSerialPort::NoParity);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (!serial.open(QIODevice::ReadOnly))
{
std::cout << "!!! Failed to open port " << serial_info.portName().toStdString() <<
". Error: " << serial.errorString().toStdString() << std::endl;
return 1;
}
SerialPortReader reader(&serial);
std::cout << "===== Output =====" << std::endl;
return coreApplication.exec();
}