1
- /* Copyright (C) 2015 Karl Phillip Buhr <[email protected] >
1
+ /* Copyright (C) 2013- 2015 Karl Phillip Buhr <[email protected] >
2
2
*
3
3
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
4
4
* To view a copy of this license, visit:
@@ -19,32 +19,55 @@ int main(int argc, char *argv[])
19
19
{
20
20
QCoreApplication coreApplication (argc, argv);
21
21
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
+ }
26
27
27
- for (int i = 0 ; i < QSerialPortInfo::availablePorts ().count (); i++)
28
+ int option = -1 ;
29
+ while (1 )
28
30
{
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++)
31
36
{
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 << " \t Location: " << serial_info.systemLocation ().toStdString () << std::endl;
41
+ std::cout << " \t Description: " << serial_info.description ().toStdString () << std::endl;
42
+ std::cout << " \t Manufacturer: " << serial_info.manufacturer ().toStdString () << std::endl;
43
+ std::cout << " \t Vendor Identifier: " << (serial_info.hasVendorIdentifier () ?
37
44
QString::number (serial_info.vendorIdentifier ()).toStdString () :
38
45
" " ) << std::endl;
39
- std::cout << " * Product Identifier: " << (serial_info.hasProductIdentifier () ?
46
+ std::cout << " \t Product Identifier: " << (serial_info.hasProductIdentifier () ?
40
47
QString::number (serial_info.productIdentifier ()).toStdString () :
41
48
" " ) << std::endl;
42
- std::cout << " * Busy : " << (serial_info.isBusy () ? " Yes" : " No" ) << std::endl;
49
+ std::cout << " \t Busy : " << (serial_info.isBusy () ? " Yes" : " No" ) << std::endl;
43
50
}
51
+
52
+ std::cout << " \n Your choice: " ;
53
+ std::cin >> option;
54
+ if (option >= 0 && option <= QSerialPortInfo::availablePorts ().count ())
55
+ break ;
56
+ std::cout << std::endl;
44
57
}
45
58
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
+
46
69
QSerialPort serial;
47
- serial.setPortName (port );
70
+ serial.setPortName (serial_info. portName () );
48
71
serial.setBaudRate (QSerialPort::Baud9600);
49
72
serial.setDataBits (QSerialPort::Data8);
50
73
serial.setStopBits (QSerialPort::OneStop);
@@ -53,13 +76,13 @@ int main(int argc, char *argv[])
53
76
54
77
if (!serial.open (QIODevice::ReadOnly))
55
78
{
56
- std::cout << " !!! Failed to open port " << port .toStdString () <<
79
+ std::cout << " !!! Failed to open port " << serial_info. portName () .toStdString () <<
57
80
" . Error: " << serial.errorString ().toStdString () << std::endl;
58
81
return 1 ;
59
82
}
60
83
61
84
SerialPortReader reader (&serial);
62
- std::cout << " * Output: " << std::endl;
85
+ std::cout << " ===== Output ===== " << std::endl;
63
86
64
87
return coreApplication.exec ();
65
88
}
0 commit comments