1// Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com>
2// Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com>
3// Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
6#include "qserialportinfo.h"
7#include "qserialportinfo_p.h"
8#include "qserialport.h"
9#include "qserialport_p.h"
10
11QT_BEGIN_NAMESPACE
12
13// We changed from QScopedPointer to std::unique_ptr, make sure it's
14// binary compatible. The QScopedPointer had a non-default deleter, but
15// the deleter just provides a static function to use for deletion so we don't
16// include it in this template definition (the deleter-class was deleted).
17static_assert(sizeof(std::unique_ptr<QSerialPortInfoPrivate>)
18 == sizeof(QScopedPointer<QSerialPortInfoPrivate>));
19
20/*!
21 \class QSerialPortInfo
22
23 \brief Provides information about existing serial ports.
24
25 \ingroup serialport-main
26 \inmodule QtSerialPort
27 \since 5.1
28
29 Use the static \l availablePorts() function to generate a list of
30 QSerialPortInfo objects. Each QSerialPortInfo object in the list represents
31 a single serial port and can be queried for the \l {portName}{port name},
32 \l {systemLocation}{system location}, \l description, \l manufacturer, and
33 some other hardware parameters. The QSerialPortInfo class can also be
34 used as an input parameter for the \l {QSerialPort::}{setPort()} method of
35 the QSerialPort class.
36
37 \section1 Example Usage
38
39 The example code enumerates all available serial ports and prints their
40 parameters to console:
41
42 \snippet doc_src_serialport.cpp enumerate_ports
43
44 \sa QSerialPort
45*/
46
47/*!
48 Constructs an empty QSerialPortInfo object.
49
50 \sa isNull()
51*/
52QSerialPortInfo::QSerialPortInfo()
53{
54}
55
56/*!
57 Constructs a copy of \a other.
58*/
59QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other)
60 : d_ptr(other.d_ptr ? new QSerialPortInfoPrivate(*other.d_ptr) : nullptr)
61{
62}
63
64/*!
65 Constructs a QSerialPortInfo object from serial \a port.
66*/
67QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)
68 : QSerialPortInfo(port.portName())
69{
70}
71
72/*!
73 Constructs a QSerialPortInfo object from serial port \a name.
74
75 This constructor finds the relevant serial port among the available ones
76 according to the port name \a name, and constructs the serial port info
77 instance for that port.
78*/
79QSerialPortInfo::QSerialPortInfo(const QString &name)
80{
81 const auto infos = QSerialPortInfo::availablePorts();
82 for (const QSerialPortInfo &info : infos) {
83 if (name == info.portName()) {
84 *this = info;
85 break;
86 }
87 }
88}
89
90QSerialPortInfo::QSerialPortInfo(const QSerialPortInfoPrivate &dd)
91 : d_ptr(new QSerialPortInfoPrivate(dd))
92{
93}
94
95/*!
96 Destroys the QSerialPortInfo object. References to the values in the
97 object become invalid.
98*/
99QSerialPortInfo::~QSerialPortInfo()
100{
101}
102
103/*!
104 Swaps QSerialPortInfo \a other with this QSerialPortInfo. This operation is
105 very fast and never fails.
106*/
107void QSerialPortInfo::swap(QSerialPortInfo &other)
108{
109 d_ptr.swap(u&: other.d_ptr);
110}
111
112/*!
113 Sets the QSerialPortInfo object to be equal to \a other.
114*/
115QSerialPortInfo& QSerialPortInfo::operator=(const QSerialPortInfo &other)
116{
117 QSerialPortInfo(other).swap(other&: *this);
118 return *this;
119}
120
121/*!
122 Returns the name of the serial port.
123
124 \sa systemLocation()
125*/
126QString QSerialPortInfo::portName() const
127{
128 Q_D(const QSerialPortInfo);
129 return !d ? QString() : d->portName;
130}
131
132/*!
133 Returns the system location of the serial port.
134
135 \sa portName()
136*/
137QString QSerialPortInfo::systemLocation() const
138{
139 Q_D(const QSerialPortInfo);
140 return !d ? QString() : d->device;
141}
142
143/*!
144 Returns the description string of the serial port,
145 if available; otherwise returns an empty string.
146
147 \sa manufacturer(), serialNumber()
148*/
149QString QSerialPortInfo::description() const
150{
151 Q_D(const QSerialPortInfo);
152 return !d ? QString() : d->description;
153}
154
155/*!
156 Returns the manufacturer string of the serial port,
157 if available; otherwise returns an empty string.
158
159 \sa description(), serialNumber()
160*/
161QString QSerialPortInfo::manufacturer() const
162{
163 Q_D(const QSerialPortInfo);
164 return !d ? QString() : d->manufacturer;
165}
166
167/*!
168 \since 5.3
169
170 Returns the serial number string of the serial port,
171 if available; otherwise returns an empty string.
172
173 \note The serial number may include letters.
174
175 \sa description(), manufacturer()
176*/
177QString QSerialPortInfo::serialNumber() const
178{
179 Q_D(const QSerialPortInfo);
180 return !d ? QString() : d->serialNumber;
181}
182
183/*!
184 Returns the 16-bit vendor number for the serial port, if available;
185 otherwise returns zero.
186
187 \sa hasVendorIdentifier(), productIdentifier(), hasProductIdentifier()
188*/
189quint16 QSerialPortInfo::vendorIdentifier() const
190{
191 Q_D(const QSerialPortInfo);
192 return !d ? 0 : d->vendorIdentifier;
193}
194
195/*!
196 Returns the 16-bit product number for the serial port, if available;
197 otherwise returns zero.
198
199 \sa hasProductIdentifier(), vendorIdentifier(), hasVendorIdentifier()
200*/
201quint16 QSerialPortInfo::productIdentifier() const
202{
203 Q_D(const QSerialPortInfo);
204 return !d ? 0 : d->productIdentifier;
205}
206
207/*!
208 Returns \c true if there is a valid \c 16-bit vendor number present; otherwise
209 returns \c false.
210
211 \sa vendorIdentifier(), productIdentifier(), hasProductIdentifier()
212*/
213bool QSerialPortInfo::hasVendorIdentifier() const
214{
215 Q_D(const QSerialPortInfo);
216 return !d ? false : d->hasVendorIdentifier;
217}
218
219/*!
220 Returns \c true if there is a valid \c 16-bit product number present; otherwise
221 returns \c false.
222
223 \sa productIdentifier(), vendorIdentifier(), hasVendorIdentifier()
224*/
225bool QSerialPortInfo::hasProductIdentifier() const
226{
227 Q_D(const QSerialPortInfo);
228 return !d ? false : d->hasProductIdentifier;
229}
230
231/*!
232 \fn bool QSerialPortInfo::isNull() const
233
234 Returns whether this QSerialPortInfo object holds a
235 serial port definition.
236*/
237
238/*!
239 \fn QList<qint32> QSerialPortInfo::standardBaudRates()
240
241 Returns a list of available standard baud rates supported
242 by the target platform.
243*/
244QList<qint32> QSerialPortInfo::standardBaudRates()
245{
246 return QSerialPortPrivate::standardBaudRates();
247}
248
249/*!
250 \fn QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
251
252 Returns a list of available serial ports on the system.
253*/
254
255QT_END_NAMESPACE
256

source code of qtserialport/src/serialport/qserialportinfo.cpp