-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
221 lines (191 loc) · 8.16 KB
/
mainwindow.cpp
File metadata and controls
221 lines (191 loc) · 8.16 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
MainWindow::MainWindow(QWidget *parent, const std::string &config_file) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// parse startup config file
load_config(config_file);
// make GUI connections
QObject::connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
QObject::connect(ui->linkButton, SIGNAL(clicked()), this, SLOT(on_link()));
QObject::connect(ui->actionLoad_Configuration, SIGNAL(triggered()), this, SLOT(load_config_dialog()));
QObject::connect(ui->actionSave_Configuration, SIGNAL(triggered()), this, SLOT(save_config_dialog()));
}
void MainWindow::load_config_dialog() {
QString sel = QFileDialog::getOpenFileName(this,"Load Configuration File","","Configuration Files (*.cfg)");
if (!sel.isEmpty())
load_config(sel.toStdString());
}
void MainWindow::save_config_dialog() {
QString sel = QFileDialog::getSaveFileName(this,"Save Configuration File","","Configuration Files (*.cfg)");
if (!sel.isEmpty())
save_config(sel.toStdString());
}
void MainWindow::closeEvent(QCloseEvent *ev) {
if (reader_thread_)
ev->ignore();
}
void MainWindow::load_config(const std::string &filename) {
using boost::property_tree::ptree;
ptree pt;
// parse file
try {
read_xml(filename, pt);
} catch(std::exception &e) {
QMessageBox::information(this,"Error",(std::string("Cannot read config file: ")+= e.what()).c_str(),QMessageBox::Ok);
return;
}
// get config values
try {
ui->comPort->setValue(pt.get<int>("coresettings.comport",1));
ui->baudRate->setValue(pt.get<int>("coresettings.baudrate",57600));
ui->samplingRate->setValue(pt.get<int>("streamsettings.samplingrate",0));
ui->chunkSize->setValue(pt.get<int>("streamsettings.chunksize",32));
ui->streamName->setText(pt.get<std::string>("streamsettings.streamname","SerialPort").c_str());
ui->dataBits->setCurrentIndex(pt.get<int>("miscsettings.databits",4));
ui->parity->setCurrentIndex(pt.get<int>("miscsettings.parity",0));
ui->stopBits->setCurrentIndex(pt.get<int>("miscsettings.stopbits",0));
ui->readIntervalTimeout->setValue(pt.get<int>("timeoutsettings.readintervaltimeout",500));
ui->readTotalTimeoutConstant->setValue(pt.get<int>("timeoutsettings.readtotaltimeoutconstant",50));
ui->readTotalTimeoutMultiplier->setValue(pt.get<int>("timeoutsettings.readtotaltimeoutmultiplier",10));
} catch(std::exception &) {
QMessageBox::information(this,"Error in Config File","Could not read out config parameters.",QMessageBox::Ok);
return;
}
}
void MainWindow::save_config(const std::string &filename) {
using boost::property_tree::ptree;
ptree pt;
// transfer UI content into property tree
try {
pt.put("coresettings.comport",ui->comPort->value());
pt.put("coresettings.baudrate",ui->baudRate->value());
pt.put("streamsettings.samplingrate",ui->samplingRate->value());
pt.put("streamsettings.chunksize",ui->chunkSize->value());
pt.put("streamsettings.streamname",ui->streamName->text().toStdString());
pt.put("miscsettings.databits",ui->dataBits->currentIndex());
pt.put("miscsettings.parity",ui->parity->currentIndex());
pt.put("miscsettings.stopbits",ui->stopBits->currentIndex());
pt.put("timeoutsettings.readintervaltimeout",ui->readIntervalTimeout->value());
pt.put("timeoutsettings.readtotaltimeoutconstant",ui->readTotalTimeoutConstant->value());
pt.put("timeoutsettings.readtotaltimeoutmultiplier",ui->readTotalTimeoutMultiplier->value());
} catch(std::exception &e) {
QMessageBox::critical(this,"Error",(std::string("Could not prepare settings for saving: ")+=e.what()).c_str(),QMessageBox::Ok);
}
// write to disk
try {
write_xml(filename, pt);
} catch(std::exception &e) {
QMessageBox::critical(this,"Error",(std::string("Could not write to config file: ")+=e.what()).c_str(),QMessageBox::Ok);
}
}
// start/stop the cognionics connection
void MainWindow::on_link() {
if (reader_thread_) {
// === perform unlink action ===
try {
shutdown_ = true;
reader_thread_->join();
reader_thread_.reset();
} catch(std::exception &e) {
QMessageBox::critical(this,"Error",(std::string("Could not stop the background processing: ")+=e.what()).c_str(),QMessageBox::Ok);
return;
}
// indicate that we are now successfully unlinked
ui->linkButton->setText("Link");
} else {
// === perform link action ===
HANDLE hPort = NULL;
try {
// get the UI parameters...
int comPort = ui->comPort->value();
int baudRate = ui->baudRate->value();
int samplingRate = ui->samplingRate->value();
int chunkSize = ui->chunkSize->value();
std::string streamName = ui->streamName->text().toStdString();
int dataBits = ui->dataBits->currentIndex()+4;
int parity = ui->parity->currentIndex();
int stopBits = ui->stopBits->currentIndex();
int readIntervalTimeout = ui->readIntervalTimeout->value();
int readTotalTimeoutConstant = ui->readTotalTimeoutConstant->value();
int readTotalTimeoutMultiplier = ui->readTotalTimeoutMultiplier->value();
// try to open the serial port
std::string fname = "\\\\.\\COM" + std::to_string(comPort);
hPort = CreateFileA(fname.c_str(),GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (hPort == INVALID_HANDLE_VALUE)
throw std::runtime_error("Could not open serial port. Please make sure that you are using the right COM port and that the device is ready.");
// try to set up serial port parameters
DCB dcbSerialParams = {0};
if (!GetCommState(hPort, &dcbSerialParams))
QMessageBox::critical(this,"Error","Could not get COM port state.",QMessageBox::Ok);
dcbSerialParams.BaudRate=baudRate;
dcbSerialParams.ByteSize=dataBits;
dcbSerialParams.StopBits=stopBits;
dcbSerialParams.Parity=parity;
if(!SetCommState(hPort, &dcbSerialParams))
QMessageBox::critical(this,"Error","Could not set baud rate.",QMessageBox::Ok);
// try to set timeouts
COMMTIMEOUTS timeouts = {0};
if (!GetCommTimeouts(hPort,&timeouts))
QMessageBox::critical(this,"Error","Could not get COM port timeouts.",QMessageBox::Ok);
timeouts.ReadIntervalTimeout = readIntervalTimeout;
timeouts.ReadTotalTimeoutConstant = readTotalTimeoutConstant;
timeouts.ReadTotalTimeoutMultiplier = readTotalTimeoutMultiplier;
if (!SetCommTimeouts(hPort,&timeouts))
QMessageBox::critical(this,"Error","Could not set COM port timeouts.",QMessageBox::Ok);
// start reading
shutdown_ = false;
reader_thread_ = std::make_unique<std::thread>(&MainWindow::read_thread, this, hPort, comPort, baudRate, samplingRate, chunkSize, streamName);
}
catch(std::exception &e) {
if (hPort != INVALID_HANDLE_VALUE)
CloseHandle(hPort);
QMessageBox::critical(this,"Error",(std::string("Error during initialization: ")+=e.what()).c_str(),QMessageBox::Ok);
return;
}
// done, all successful
ui->linkButton->setText("Unlink");
}
}
// background data reader thread
void MainWindow::read_thread(HANDLE hPort, int comPort, int baudRate, int samplingRate, int chunkSize, const std::string &streamName) {
try {
// create streaminfo
lsl::stream_info info(streamName,"Raw",1,samplingRate,lsl::cf_int16,std::string("SerialPort_") + streamName);
// append some meta-data
lsl::xml_element channels = info.desc().append_child("channels");
channels.append_child("channel")
.append_child_value("label","Channel1")
.append_child_value("type","Raw")
.append_child_value("unit","integer");
info.desc().append_child("acquisition")
.append_child("hardware")
.append_child_value("com_port", std::to_string(comPort))
.append_child_value("baud_rate",std::to_string(baudRate));
// make a new outlet
lsl::stream_outlet outlet(info,chunkSize);
// enter transmission loop
unsigned char byte;
short sample;
unsigned long bytes_read;
while (!shutdown_) {
// get a sample
ReadFile(hPort,&byte,1,&bytes_read,NULL); sample = byte;
// transmit it
if (bytes_read)
outlet.push_sample(&sample);
}
}
catch(std::exception &e) {
// any other error
QMessageBox::critical(this,"Error",(std::string("Error during processing: ")+=e.what()).c_str(),QMessageBox::Ok);
}
CloseHandle(hPort);
}
MainWindow::~MainWindow() {
delete ui;
}