-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialconnectwindow.cpp
executable file
·189 lines (162 loc) · 6.55 KB
/
serialconnectwindow.cpp
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
#include "serialconnectwindow.h"
#include "ui_connectwindow.h"
#include "serialdeviceenumerator.h"
#include "abstractserial.h"
#include "mainwindow.h"
#include <QSettings>
ConnectWindow::ConnectWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ConnectWindow),
device(new AbstractSerial(this))
{
ui->setupUi(this);
SerialDeviceEnumerator* serDevEnum = SerialDeviceEnumerator::instance();
connect(serDevEnum, SIGNAL(hasChanged(QStringList)),
this, SLOT(updateDeviceList(QStringList)));
connect(device, SIGNAL(aboutToClose()), this, SLOT(show()));
updateDeviceList(serDevEnum->devicesAvailable());
populateBaudRates();
populateDataBits();
populateParity();
populateStopBits();
populateFlowControl();
}
ConnectWindow::~ConnectWindow()
{
delete ui;
}
void ConnectWindow::updateDeviceList(const QStringList &list)
{
ui->deviceNameComboBox->clear();
SerialDeviceEnumerator* serDevEnum = SerialDeviceEnumerator::instance();
QSettings settings;
foreach (QString s, list) {
if(!s.toLower().contains("usb")) continue;
serDevEnum->setDeviceName(s);
ui->deviceNameComboBox->addItem(serDevEnum->description()+" (" + s + ")", s);
if(settings.value("deviceName") == s) {
ui->deviceNameComboBox->setCurrentIndex(ui->deviceNameComboBox->count()-1);
}
}
}
#define ADD_ITEM(comboBox,enumName) comboBox->addItem(#enumName, AbstractSerial::enumName)
#define DIM(a,b) __typeof(b) a = b
void ConnectWindow::populateBaudRates()
{
QSettings settings;
DIM(baudRateMap, device->baudRateMap());
for(DIM(it, baudRateMap.constBegin()); it != baudRateMap.constEnd(); it++) {
ui->baudRateComboBox->addItem(it.value(), it.key());
if(it.key() == settings.value("baudRate")) {
ui->baudRateComboBox->setCurrentIndex(ui->baudRateComboBox->count()-1);
}
}
}
void ConnectWindow::populateDataBits()
{
QSettings settings;
DIM(dataBitsMap, device->dataBitsMap());
for(DIM(it, dataBitsMap.constBegin()); it != dataBitsMap.constEnd(); it++) {
ui->dataBitsComboBox->addItem(it.value(), it.key());
if(it.key() == settings.value("dataBits")) {
ui->dataBitsComboBox->setCurrentIndex(ui->dataBitsComboBox->count()-1);
}
}
}
void ConnectWindow::populateParity()
{
QSettings settings;
DIM(parityMap, device->parityMap());
for(DIM(it, parityMap.constBegin()); it != parityMap.constEnd(); it++) {
ui->parityComboBox->addItem(it.value(), it.key());
if(it.key() == settings.value("parity")) {
ui->parityComboBox->setCurrentIndex(ui->parityComboBox->count()-1);
}
}
}
void ConnectWindow::populateStopBits()
{
QSettings settings;
DIM(stopBitsMap, device->stopBitsMap());
for(DIM(it, stopBitsMap.constBegin()); it != stopBitsMap.constEnd(); it++) {
ui->stopBitsComboBox->addItem(it.value(), it.key());
if(it.key() == settings.value("stopBits")) {
ui->stopBitsComboBox->setCurrentIndex(ui->stopBitsComboBox->count()-1);
}
}
}
void ConnectWindow::populateFlowControl()
{
QSettings settings;
DIM(flowControlMap, device->flowControlMap());
for(DIM(it, flowControlMap.constBegin()); it != flowControlMap.constEnd(); it++) {
ui->flowControlComboBox->addItem(it.value(), it.key());
if(it.key() == settings.value("flowControl")) {
ui->flowControlComboBox->setCurrentIndex(ui->flowControlComboBox->count()-1);
}
}
}
#define COMBO_DATA_STR(comboBox) comboBox->itemData(comboBox->currentIndex()).toString()
#define COMBO_DATA_INT(comboBox) comboBox->itemData(comboBox->currentIndex()).toInt()
void ConnectWindow::on_connectPushButton_clicked()
{
QSettings settings;
device->setDeviceName(COMBO_DATA_STR(ui->deviceNameComboBox));
if(!device->open(AbstractSerial::ReadWrite)) {
qDebug() << "Connection error.";
statusBar()->showMessage(tr("Can't Connect to device!!! :-("));
return;
}
settings.setValue("deviceName", COMBO_DATA_STR(ui->deviceNameComboBox));
if (!device->setBaudRate((AbstractSerial::BaudRate)COMBO_DATA_INT(ui->baudRateComboBox))) {
qDebug() << "Set baud rate error.";
statusBar()->showMessage(tr("Set baud rate error."));
goto label;
};
settings.setValue("baudRate", COMBO_DATA_INT(ui->baudRateComboBox));
if (!device->setDataBits((AbstractSerial::DataBits)COMBO_DATA_INT(ui->dataBitsComboBox))) {
qDebug() << "Set data bits error.";
statusBar()->showMessage(tr("Set data bits error."));
goto label;
}
settings.setValue("dataBits", COMBO_DATA_INT(ui->dataBitsComboBox));
if (!device->setParity((AbstractSerial::Parity)COMBO_DATA_INT(ui->parityComboBox))) {
qDebug() << "Set parity error.";
statusBar()->showMessage(tr("Set parity error."));
goto label;
}
settings.setValue("parity", COMBO_DATA_INT(ui->parityComboBox));
if (!device->setStopBits((AbstractSerial::StopBits)COMBO_DATA_INT(ui->stopBitsComboBox))) {
qDebug() << "Set stop bits error.";
statusBar()->showMessage(tr("Set stop bits error."));
goto label;
}
settings.setValue("stopBits", COMBO_DATA_INT(ui->stopBitsComboBox));
if (!device->setFlowControl((AbstractSerial::Flow)COMBO_DATA_INT(ui->flowControlComboBox))) {
qDebug() << "Set flow control error.";
statusBar()->showMessage(tr("Set flow control error."));
goto label;
}
settings.setValue("flowControl", COMBO_DATA_INT(ui->flowControlComboBox));
goto showMain;
label:
device->close();
return;
showMain:
qDebug() << "= Default parameters =";
qDebug() << "Device name : " << device->deviceName();
qDebug() << "Baud rate : " << device->baudRate();
qDebug() << "Data bits : " << device->dataBits();
qDebug() << "Parity : " << device->parity();
qDebug() << "Stop bits : " << device->stopBits();
qDebug() << "Flow : " << device->flowControl();
qDebug() << "Char timeout, msec : " << device->charIntervalTimeout();
MainWindow *mainWin = new MainWindow(device);
mainWin->show();
this->hide();
connect(device, SIGNAL(aboutToClose()), mainWin, SLOT(deleteLater()));
}
void ConnectWindow::on_exitPushButton_clicked()
{
this->close();
}