Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 554b353

Browse files
committed
QtSerial: lists all serial ports
1 parent 8c513c6 commit 554b353

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

QtSerial/QtSerial.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Go to the Project properties,
2+
# click on the Run tab,
3+
# and check the box "Run in terminal".
4+
CONFIG += console
5+
16
QT += core serialport
27

38
SOURCES += \

QtSerial/main.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
1+
/* Copyright (C) 2013-2015 Karl Phillip Buhr <[email protected]>
22
*
33
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
44
* To view a copy of this license, visit:
@@ -19,32 +19,55 @@ int main(int argc, char *argv[])
1919
{
2020
QCoreApplication coreApplication(argc, argv);
2121

22-
std::cout << "* Number of serial ports available: " << QSerialPortInfo::availablePorts().count() << std::endl;
23-
24-
QString port("COM6");
25-
std::cout << "* Trying to find " << port.toStdString() << " ..." << std::endl;
22+
if (!QSerialPortInfo::availablePorts().count())
23+
{
24+
std::cout << "!!! No serial ports found!" << std::endl;
25+
return 0;
26+
}
2627

27-
for (int i = 0; i < QSerialPortInfo::availablePorts().count(); i++)
28+
int option = -1;
29+
while (1)
2830
{
29-
QSerialPortInfo serial_info = QSerialPortInfo::availablePorts()[i];
30-
if (port == serial_info.portName())
31+
std::cout << "===== List of available serial ports (" <<
32+
QSerialPortInfo::availablePorts().count() << ") =====" << std::endl;
33+
std::cout << "[0] - Exit" << std::endl;
34+
35+
for (int i = 0; i < QSerialPortInfo::availablePorts().count(); i++)
3136
{
32-
std::cout << "* OK, found it: " << serial_info.portName().toStdString() << std::endl;
33-
std::cout << "* Location: " << serial_info.systemLocation().toStdString() << std::endl;
34-
std::cout << "* Description: " << serial_info.description().toStdString() << std::endl;
35-
std::cout << "* Manufacturer: " << serial_info.manufacturer().toStdString() << std::endl;
36-
std::cout << "* Vendor Identifier: " << (serial_info.hasVendorIdentifier() ?
37+
QSerialPortInfo serial_info = QSerialPortInfo::availablePorts()[i];
38+
std::cout << "[" << i+1 << "] - " << serial_info.portName().toStdString() << std::endl;
39+
40+
std::cout << "\tLocation: " << serial_info.systemLocation().toStdString() << std::endl;
41+
std::cout << "\tDescription: " << serial_info.description().toStdString() << std::endl;
42+
std::cout << "\tManufacturer: " << serial_info.manufacturer().toStdString() << std::endl;
43+
std::cout << "\tVendor Identifier: " << (serial_info.hasVendorIdentifier() ?
3744
QString::number(serial_info.vendorIdentifier()).toStdString() :
3845
"") << std::endl;
39-
std::cout << "* Product Identifier: " << (serial_info.hasProductIdentifier() ?
46+
std::cout << "\tProduct Identifier: " << (serial_info.hasProductIdentifier() ?
4047
QString::number(serial_info.productIdentifier()).toStdString() :
4148
"") << std::endl;
42-
std::cout << "* Busy: " << (serial_info.isBusy() ? "Yes" : "No") << std::endl;
49+
std::cout << "\tBusy: " << (serial_info.isBusy() ? "Yes" : "No") << std::endl;
4350
}
51+
52+
std::cout << "\nYour choice: ";
53+
std::cin >> option;
54+
if (option >= 0 && option <= QSerialPortInfo::availablePorts().count())
55+
break;
56+
std::cout << std::endl;
4457
}
4558

59+
if (option == 0)
60+
{
61+
std::cout << "* OK, bye bye!" << std::endl;
62+
return 0;
63+
}
64+
65+
QSerialPortInfo serial_info = QSerialPortInfo::availablePorts()[option-1];
66+
std::cout << "* OK, " << serial_info.portName().toStdString() <<
67+
" selected!" << std::endl;
68+
4669
QSerialPort serial;
47-
serial.setPortName(port);
70+
serial.setPortName(serial_info.portName());
4871
serial.setBaudRate(QSerialPort::Baud9600);
4972
serial.setDataBits(QSerialPort::Data8);
5073
serial.setStopBits(QSerialPort::OneStop);
@@ -53,13 +76,13 @@ int main(int argc, char *argv[])
5376

5477
if (!serial.open(QIODevice::ReadOnly))
5578
{
56-
std::cout << "!!! Failed to open port " << port.toStdString() <<
79+
std::cout << "!!! Failed to open port " << serial_info.portName().toStdString() <<
5780
". Error: " << serial.errorString().toStdString() << std::endl;
5881
return 1;
5982
}
6083

6184
SerialPortReader reader(&serial);
62-
std::cout << "* Output:" << std::endl;
85+
std::cout << "===== Output =====" << std::endl;
6386

6487
return coreApplication.exec();
6588
}

QtSerial/serialportreader.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
1+
/* Copyright (C) 2013-2015 Karl Phillip Buhr <[email protected]>
22
*
33
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
44
* To view a copy of this license, visit:
@@ -12,6 +12,8 @@
1212

1313
#include <QCoreApplication>
1414
#include <QDebug>
15+
#include <QByteArray>
16+
#include <QTextCodec>
1517

1618

1719
SerialPortReader::SerialPortReader(QSerialPort* serial, QObject* parent)
@@ -48,9 +50,15 @@ void SerialPortReader::_read_callback()
4850
* hence the *if* statement below just to print the value I need.
4951
*/
5052
if (sz == 3 && (int)data[1] == 13 && (int)data[2] == 10)
53+
{
5154
std::cout << data[0] << std::endl;
55+
}
5256
else
53-
std::cout << data.toStdString() << std::endl;
57+
{
58+
// To print data from other serial devices
59+
QString str = QString::fromUtf8(data);
60+
std::cout << str.toStdString();
61+
}
5462
}
5563

5664
void SerialPortReader::_error_callback(QSerialPort::SerialPortError error)

QtSerial/serialportreader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
1+
/* Copyright (C) 2013-2015 Karl Phillip Buhr <[email protected]>
22
*
33
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
44
* To view a copy of this license, visit:

0 commit comments

Comments
 (0)