forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.cpp
More file actions
792 lines (692 loc) · 20.7 KB
/
Serial.cpp
File metadata and controls
792 lines (692 loc) · 20.7 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
* Copyright (c) 2020-2023 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <IO/Manager.h>
#include <IO/Drivers/Serial.h>
#include <Misc/Utilities.h>
#include <Misc/TimerEvents.h>
//----------------------------------------------------------------------------------------
// Constructor/destructor & singleton access functions
//----------------------------------------------------------------------------------------
/**
* Constructor function
*/
IO::Drivers::Serial::Serial()
: m_port(Q_NULLPTR)
, m_autoReconnect(false)
, m_lastSerialDeviceIndex(0)
, m_portIndex(0)
{
// Read settings
readSettings();
// Init serial port configuration variables
setBaudRate(9600);
disconnectDevice();
setDataBits(dataBitsList().indexOf("8"));
setStopBits(stopBitsList().indexOf("1"));
setParity(parityList().indexOf(tr("None")));
setFlowControl(flowControlList().indexOf(tr("None")));
// clang-format off
// Build serial devices list and refresh it every second
connect(&Misc::TimerEvents::instance(), &Misc::TimerEvents::timeout1Hz,
this, &IO::Drivers::Serial::refreshSerialDevices);
// Update connect button status when user selects a serial device
connect(this, &IO::Drivers::Serial::portIndexChanged,
this, &IO::Drivers::Serial::configurationChanged);
// clang-format on
}
/**
* Destructor function, closes the serial port before exiting the application and saves
* the user's baud rate list settings.
*/
IO::Drivers::Serial::~Serial()
{
writeSettings();
if (port())
disconnectDevice();
}
/**
* Returns the only instance of the class
*/
IO::Drivers::Serial &IO::Drivers::Serial::instance()
{
static Serial singleton;
return singleton;
}
//----------------------------------------------------------------------------------------
// HAL-driver implementation
//----------------------------------------------------------------------------------------
/**
* Closes the current serial port connection
*/
void IO::Drivers::Serial::close()
{
if (isOpen())
port()->close();
}
/**
* Returns @c true if a serial port connection is currently open
*/
bool IO::Drivers::Serial::isOpen() const
{
if (port())
return port()->isOpen();
return false;
}
/**
* Returns @c true if the current serial device is readable
*/
bool IO::Drivers::Serial::isReadable() const
{
if (isOpen())
return port()->isReadable();
return false;
}
/**
* Returns @c true if the current serial device is writable
*/
bool IO::Drivers::Serial::isWritable() const
{
if (isOpen())
return port()->isWritable();
return false;
}
/**
* Returns @c true if the user selects the appropiate controls & options to be able
* to connect to a serial device
*/
bool IO::Drivers::Serial::configurationOk() const
{
return portIndex() > 0;
}
/**
* Writes the given @a data to the serial device and returns the number of bytes written
*/
quint64 IO::Drivers::Serial::write(const QByteArray &data)
{
if (isWritable())
return port()->write(data);
return -1;
}
/**
* Connects to the currently selected serial port device, returns @c true on success
*/
bool IO::Drivers::Serial::open(const QIODevice::OpenMode mode)
{
// Ignore the first item of the list (Select Port)
auto ports = validPorts();
auto portId = portIndex() - 1;
if (portId >= 0 && portId < validPorts().count())
{
// Update port index variable & disconnect from current serial port
disconnectDevice();
m_portIndex = portId + 1;
m_lastSerialDeviceIndex = m_portIndex;
Q_EMIT portIndexChanged();
// Create new serial port handler
m_port = new QSerialPort(ports.at(portId));
// Configure serial port
port()->setParity(parity());
port()->setBaudRate(baudRate());
port()->setDataBits(dataBits());
port()->setStopBits(stopBits());
port()->setFlowControl(flowControl());
// Connect signals/slots
connect(port(), SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
// Open device
if (port()->open(mode))
{
connect(port(), &QIODevice::readyRead, this,
&IO::Drivers::Serial::onReadyRead);
return true;
}
}
// Disconnect serial port
disconnectDevice();
return false;
}
//----------------------------------------------------------------------------------------
// Driver specifics
//----------------------------------------------------------------------------------------
/**
* Returns the name of the current serial port device
*/
QString IO::Drivers::Serial::portName() const
{
if (port())
return port()->portName();
return tr("No Device");
}
/**
* Returns the pointer to the current serial port handler
*/
QSerialPort *IO::Drivers::Serial::port() const
{
return m_port;
}
/**
* Returns @c true if auto-reconnect is enabled
*/
bool IO::Drivers::Serial::autoReconnect() const
{
return m_autoReconnect;
}
/**
* Returns the index of the current serial device selected by the program.
*/
quint8 IO::Drivers::Serial::portIndex() const
{
return m_portIndex;
}
/**
* Returns the correspoding index of the parity configuration in relation
* to the @c StringList returned by the @c parityList() function.
*/
quint8 IO::Drivers::Serial::parityIndex() const
{
return m_parityIndex;
}
/**
* Returns the correspoding index of the data bits configuration in relation
* to the @c StringList returned by the @c dataBitsList() function.
*/
quint8 IO::Drivers::Serial::dataBitsIndex() const
{
return m_dataBitsIndex;
}
/**
* Returns the correspoding index of the stop bits configuration in relation
* to the @c StringList returned by the @c stopBitsList() function.
*/
quint8 IO::Drivers::Serial::stopBitsIndex() const
{
return m_stopBitsIndex;
}
/**
* Returns the correspoding index of the flow control config. in relation
* to the @c StringList returned by the @c flowControlList() function.
*/
quint8 IO::Drivers::Serial::flowControlIndex() const
{
return m_flowControlIndex;
}
/**
* Returns a list with the available serial devices/ports to use.
* This function can be used with a combo box to build nice UIs.
*
* @note The first item of the list will be invalid, since it's value will
* be "Select Serial Device". This is inteded to make the user interface
* a little more friendly.
*/
StringList IO::Drivers::Serial::portList() const
{
return m_portList;
}
/**
* Returns a list with the available parity configurations.
* This function can be used with a combo-box to build UIs.
*/
StringList IO::Drivers::Serial::parityList() const
{
StringList list;
list.append(tr("None"));
list.append(tr("Even"));
list.append(tr("Odd"));
list.append(tr("Space"));
list.append(tr("Mark"));
return list;
}
/**
* Returns a list with the available baud rate configurations.
* This function can be used with a combo-box to build UIs.
*/
StringList IO::Drivers::Serial::baudRateList() const
{
return m_baudRateList;
}
/**
* Returns a list with the available data bits configurations.
* This function can be used with a combo-box to build UIs.
*/
StringList IO::Drivers::Serial::dataBitsList() const
{
return StringList { "5", "6", "7", "8" };
}
/**
* Returns a list with the available stop bits configurations.
* This function can be used with a combo-box to build UIs.
*/
StringList IO::Drivers::Serial::stopBitsList() const
{
return StringList { "1", "1.5", "2" };
}
/**
* Returns a list with the available flow control configurations.
* This function can be used with a combo-box to build UIs.
*/
StringList IO::Drivers::Serial::flowControlList() const
{
StringList list;
list.append(tr("None"));
list.append("RTS/CTS");
list.append("XON/XOFF");
return list;
}
/**
* Returns the current parity configuration used by the serial port
* handler object.
*/
QSerialPort::Parity IO::Drivers::Serial::parity() const
{
return m_parity;
}
/**
* Returns the current baud rate configuration used by the serial port
* handler object.
*/
qint32 IO::Drivers::Serial::baudRate() const
{
return m_baudRate;
}
/**
* Returns the current data bits configuration used by the serial port
* handler object.
*/
QSerialPort::DataBits IO::Drivers::Serial::dataBits() const
{
return m_dataBits;
}
/**
* Returns the current stop bits configuration used by the serial port
* handler object.
*/
QSerialPort::StopBits IO::Drivers::Serial::stopBits() const
{
return m_stopBits;
}
/**
* Returns the current flow control configuration used by the serial
* port handler object.
*/
QSerialPort::FlowControl IO::Drivers::Serial::flowControl() const
{
return m_flowControl;
}
/**
* Disconnects from the current serial device and clears temp. data
*/
void IO::Drivers::Serial::disconnectDevice()
{
// Check if serial port pointer is valid
if (port() != Q_NULLPTR)
{
// Disconnect signals/slots
port()->disconnect(this, SLOT(onReadyRead()));
port()->disconnect(this, SLOT(handleError(QSerialPort::SerialPortError)));
// Close & delete serial port handler
port()->close();
port()->deleteLater();
}
// Reset pointer
m_port = Q_NULLPTR;
Q_EMIT portChanged();
Q_EMIT availablePortsChanged();
}
/**
* Changes the baud @a rate of the serial port
*/
void IO::Drivers::Serial::setBaudRate(const qint32 rate)
{
// Asserts
Q_ASSERT(rate > 10);
// Update baud rate
m_baudRate = rate;
// Update serial port config
if (port())
port()->setBaudRate(baudRate());
// Update user interface
Q_EMIT baudRateChanged();
}
/**
* Changes the port index value, this value is later used by the @c openSerialPort()
* function.
*/
void IO::Drivers::Serial::setPortIndex(const quint8 portIndex)
{
auto portId = portIndex - 1;
if (portId >= 0 && portId < validPorts().count())
m_portIndex = portIndex;
else
m_portIndex = 0;
Q_EMIT portIndexChanged();
}
/**
* @brief IO::Drivers::Serial::setParity
* @param parityIndex
*/
void IO::Drivers::Serial::setParity(const quint8 parityIndex)
{
// Argument verification
Q_ASSERT(parityIndex < parityList().count());
// Update current index
m_parityIndex = parityIndex;
// Set parity based on current index
switch (parityIndex)
{
case 0:
m_parity = QSerialPort::NoParity;
break;
case 1:
m_parity = QSerialPort::EvenParity;
break;
case 2:
m_parity = QSerialPort::OddParity;
break;
case 3:
m_parity = QSerialPort::SpaceParity;
break;
case 4:
m_parity = QSerialPort::MarkParity;
break;
}
// Update serial port config.
if (port())
port()->setParity(parity());
// Notify user interface
Q_EMIT parityChanged();
}
/**
* Registers the new baud rate to the list
*/
void IO::Drivers::Serial::appendBaudRate(const QString &baudRate)
{
if (!m_baudRateList.contains(baudRate))
{
m_baudRateList.append(baudRate);
writeSettings();
Q_EMIT baudRateListChanged();
Misc::Utilities::showMessageBox(
tr("Baud rate registered successfully"),
tr("Rate \"%1\" has been added to baud rate list").arg(baudRate));
}
}
/**
* Changes the data bits of the serial port.
*
* @note This function is meant to be used with a combobox in the
* QML interface
*/
void IO::Drivers::Serial::setDataBits(const quint8 dataBitsIndex)
{
// Argument verification
Q_ASSERT(dataBitsIndex < dataBitsList().count());
// Update current index
m_dataBitsIndex = dataBitsIndex;
// Obtain data bits value from current index
switch (dataBitsIndex)
{
case 0:
m_dataBits = QSerialPort::Data5;
break;
case 1:
m_dataBits = QSerialPort::Data6;
break;
case 2:
m_dataBits = QSerialPort::Data7;
break;
case 3:
m_dataBits = QSerialPort::Data8;
break;
}
// Update serial port configuration
if (port())
port()->setDataBits(dataBits());
// Update user interface
Q_EMIT dataBitsChanged();
}
/**
* Changes the stop bits of the serial port.
*
* @note This function is meant to be used with a combobox in the
* QML interface
*/
void IO::Drivers::Serial::setStopBits(const quint8 stopBitsIndex)
{
// Argument verification
Q_ASSERT(stopBitsIndex < stopBitsList().count());
// Update current index
m_stopBitsIndex = stopBitsIndex;
// Obtain stop bits value from current index
switch (stopBitsIndex)
{
case 0:
m_stopBits = QSerialPort::OneStop;
break;
case 1:
m_stopBits = QSerialPort::OneAndHalfStop;
break;
case 2:
m_stopBits = QSerialPort::TwoStop;
break;
}
// Update serial port configuration
if (port())
port()->setStopBits(stopBits());
// Update user interface
Q_EMIT stopBitsChanged();
}
/**
* Enables or disables the auto-reconnect feature
*/
void IO::Drivers::Serial::setAutoReconnect(const bool autoreconnect)
{
m_autoReconnect = autoreconnect;
Q_EMIT autoReconnectChanged();
}
/**
* Changes the flow control option of the serial port.
*
* @note This function is meant to be used with a combobox in the
* QML interface
*/
void IO::Drivers::Serial::setFlowControl(const quint8 flowControlIndex)
{
// Argument verification
Q_ASSERT(flowControlIndex < flowControlList().count());
// Update current index
m_flowControlIndex = flowControlIndex;
// Obtain flow control value from current index
switch (flowControlIndex)
{
case 0:
m_flowControl = QSerialPort::NoFlowControl;
break;
case 1:
m_flowControl = QSerialPort::HardwareControl;
break;
case 2:
m_flowControl = QSerialPort::SoftwareControl;
break;
}
// Update serial port configuration
if (port())
port()->setFlowControl(flowControl());
// Update user interface
Q_EMIT flowControlChanged();
}
/**
* Scans for new serial ports available & generates a StringList with current
* serial ports.
*/
void IO::Drivers::Serial::refreshSerialDevices()
{
// Create device list, starting with dummy header
// (for a more friendly UI when no devices are attached)
StringList ports;
ports.append(tr("Select port"));
// Search for available ports and add them to the lsit
auto validPortList = validPorts();
Q_FOREACH (QSerialPortInfo info, validPortList)
{
if (!info.isNull())
ports.append(info.portName());
}
// Update list only if necessary
if (portList() != ports)
{
// Update list
m_portList = ports;
// Update current port index
if (port())
{
auto name = port()->portName();
for (int i = 0; i < validPortList.count(); ++i)
{
auto info = validPortList.at(i);
if (info.portName() == name)
{
m_portIndex = i + 1;
break;
}
}
}
// Auto reconnect
if (Manager::instance().selectedDriver() == Manager::SelectedDriver::Serial)
{
if (autoReconnect() && m_lastSerialDeviceIndex > 0)
{
if (m_lastSerialDeviceIndex < portList().count())
{
setPortIndex(m_lastSerialDeviceIndex);
Manager::instance().connectDevice();
}
}
}
// Update UI
Q_EMIT availablePortsChanged();
}
}
/**
* @brief IO::Drivers::Serial::handleError
* @param error
*/
void IO::Drivers::Serial::handleError(QSerialPort::SerialPortError error)
{
if (error != QSerialPort::NoError)
Manager::instance().disconnectDriver();
}
/**
* Reads all the data from the serial port & sends it to the @c IO::Manager class
*/
void IO::Drivers::Serial::onReadyRead()
{
if (isOpen())
Q_EMIT dataReceived(port()->readAll());
}
/**
* Read saved settings (if any)
*/
void IO::Drivers::Serial::readSettings()
{
// Register standard baud rates
QStringList stdBaudRates
= { "300", "1200", "2400", "4800", "9600", "19200", "38400", "57600",
"74880", "115200", "230400", "250000", "500000", "1000000", "2000000" };
// Get value from settings
QStringList list;
list = m_settings.value("IO_DataSource_Serial__BaudRates", stdBaudRates)
.toStringList();
// Convert QStringList to QVector
m_baudRateList.clear();
for (int i = 0; i < list.count(); ++i)
m_baudRateList.append(list.at(i));
// Sort baud rate list
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
for (auto i = 0; i < m_baudRateList.count() - 1; ++i)
{
for (auto j = 0; j < m_baudRateList.count() - i - 1; ++j)
{
auto a = m_baudRateList.at(j).toInt();
auto b = m_baudRateList.at(j + 1).toInt();
if (a > b)
m_baudRateList.swapItemsAt(j, j + 1);
}
}
#endif
// Notify UI
Q_EMIT baudRateListChanged();
}
/**
* Save settings between application runs
*/
void IO::Drivers::Serial::writeSettings()
{
// Sort baud rate list
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
for (auto i = 0; i < m_baudRateList.count() - 1; ++i)
{
for (auto j = 0; j < m_baudRateList.count() - i - 1; ++j)
{
auto a = m_baudRateList.at(j).toInt();
auto b = m_baudRateList.at(j + 1).toInt();
if (a > b)
{
m_baudRateList.swapItemsAt(j, j + 1);
Q_EMIT baudRateListChanged();
}
}
}
#endif
// Convert QVector to QStringList
QStringList list;
for (int i = 0; i < baudRateList().count(); ++i)
list.append(baudRateList().at(i));
// Save list to memory
m_settings.setValue("IO_DataSource_Serial__BaudRates", list);
}
/**
* Returns a list with all the valid serial port objects
*/
QVector<QSerialPortInfo> IO::Drivers::Serial::validPorts() const
{
// Search for available ports and add them to the list
QVector<QSerialPortInfo> ports;
Q_FOREACH (QSerialPortInfo info, QSerialPortInfo::availablePorts())
{
if (!info.isNull())
{
// Only accept *.cu devices on macOS (remove *.tty)
// https://stackoverflow.com/a/37688347
#ifdef Q_OS_MACOS
if (info.portName().toLower().startsWith("tty."))
continue;
#endif
// Append port to list
ports.append(info);
}
}
// Return list
return ports;
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC
# include "moc_Serial.cpp"
#endif