-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_comm.cpp
More file actions
258 lines (223 loc) · 7.39 KB
/
serial_comm.cpp
File metadata and controls
258 lines (223 loc) · 7.39 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
/*
Copyright (C) 2011 Georgia Institute of Technology
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "serial_comm.h"
std::list<std::string> getComList();
extern "C" Plugin::Object *createRTXIPlugin(void)
{
return new SerialComm();
}
static DefaultGUIModel::variable_t vars[] =
{
{"Baud Rate", "", DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER,},
{"Timeout", "Connection Timeout (ms)", DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER,},
{"Sampling Rate", "Hz", DefaultGUIModel::PARAMETER, },
{"Command 1", "Serial command to send to device", DefaultGUIModel::COMMENT, },
{"Command 2", "Serial command to send to device", DefaultGUIModel::COMMENT, },
{"Command Interval (s)", "Time between sending command 1 and command 2", DefaultGUIModel::PARAMETER, },
};
static size_t num_vars = sizeof(vars)/sizeof(DefaultGUIModel::variable_t);
SerialComm::SerialComm(void) : DefaultGUIModel("SerialComm",::vars,::num_vars)
{
createGUI(vars, num_vars);
update(INIT);
customizeGUI();
refresh();
QTimer::singleShot(0, this, SLOT(resizeMe()));
QThread* thread = new QThread;
Worker* worker = new Worker();
worker->moveToThread(thread);
QObject::connect(worker, SIGNAL (error(QString)), this, SLOT (errorString(QString)));
QObject::connect(thread, SIGNAL (started()), worker, SLOT (process()));
QObject::connect(worker, SIGNAL (finished()), thread, SLOT (quit()));
QObject::connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
QObject::connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));
thread->start();
}
SerialComm::~SerialComm(void) { }
/*
* No data acquisition takes place in execute due to the
* non-RT nature of serial port connections
*/
void SerialComm::execute(void)
{
}
void SerialComm::update(DefaultGUIModel::update_flags_t flag)
{
switch(flag) {
case INIT:
fd = -1;
fs = 10;
baudrate = 9600;
timeout = 5000;
eolchar = '\n';
memset(buf,0,buf_max);
interval = 0;
curr_command = 0;
setParameter("Baud Rate", baudrate);
setParameter("Timeout", timeout);
setParameter("Sampling Rate", fs);
setParameter("Command Interval (s)", interval);
setComment("Command 1","No Command");
setComment("Command 2","No Command");
break;
case MODIFY:
baudrate = getParameter("Baud Rate").toInt();
timeout = getParameter("Timeout").toInt();
fs = getParameter("Sampling Rate").toDouble();
interval = getParameter("Command Interval (s)").toDouble();
curr_command = 0;
break;
case PAUSE:
timer->stop();
curr_command = 0;
break;
case UNPAUSE:
sendCommand();
timer->start(interval*1000);
break;
default:
break;
}
}
void SerialComm::customizeGUI(void)
{
QGridLayout *customlayout = DefaultGUIModel::getLayout();
QGroupBox *modeBox = new QGroupBox("Port:");
QVBoxLayout *modeBoxLayout = new QVBoxLayout(modeBox);
portList = new QComboBox;
modeBoxLayout->addWidget(portList);
// Retrive list of available serial ports
std::list<std::string> listOfPorts = getComList();
size_t i = 0;
for(std::list<std::string>::iterator it = listOfPorts.begin(); it != listOfPorts.end(); it++)
{
portList->insertItem(i, QString::fromUtf8(std::string(*it).c_str()));
i++;
}
// Connect button
QPushButton *connectButton = new QPushButton("Connect");
customlayout->addWidget(connectButton);
QObject::connect(connectButton, SIGNAL(clicked()), this, SLOT(connectSerialComm()), static_cast<Qt::ConnectionType>(Qt::BlockingQueuedConnection));
modeBoxLayout->addWidget(connectButton);
QPushButton *sendButton = new QPushButton("Send Command");
customlayout->addWidget(sendButton);
QObject::connect(sendButton, SIGNAL(clicked()), this, SLOT(sendCommand()), static_cast<Qt::ConnectionType>(Qt::BlockingQueuedConnection));
modeBoxLayout->addWidget(sendButton);
QPushButton *readButton = new QPushButton("Read Command");
customlayout->addWidget(readButton);
QObject::connect(readButton, SIGNAL(clicked()), this, SLOT(readCommand()), static_cast<Qt::ConnectionType>(Qt::BlockingQueuedConnection));
modeBoxLayout->addWidget(readButton);
// Status bar
QGroupBox *status_group = new QGroupBox;
QVBoxLayout *status_layout = new QVBoxLayout;
status_group->setLayout(status_layout);
statusBar = new QStatusBar();
statusBar->setSizeGripEnabled(false);
statusBar->showMessage(tr("Not connected to device."));
status_layout->addWidget(statusBar, 2, 0);
messageBar = new QStatusBar();
messageBar->setSizeGripEnabled(false);
messageBar->showMessage(tr(""));
status_layout->addWidget(messageBar, 4, 0);
// Timer
timer = new QTimer();
timer->setTimerType(Qt::CoarseTimer);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendCommand()));
// FInal layout
customlayout->addWidget(modeBox, 0,0);
customlayout->addWidget(status_group, 2, 0);
setLayout(customlayout);
}
void SerialComm::readCommand()
{
serialport_read_until(fd, buf, '\n', buf_max, timeout);
messageBar->showMessage(tr(buf));
}
void SerialComm::sendCommand()
{
QString command;
if (curr_command == 0)
{
command = getComment("Command 1");
curr_command++;
}
else if (curr_command == 1)
{
command = getComment("Command 2");
curr_command++;
}
else
{
command = "";
}
std::string command_str = command.toStdString();
command_str = command_str + '\r';
const char* command_c = command_str.c_str();
serialport_write(fd, command_c);
readCommand();
if (curr_command == 2)
{
pauseButton->setChecked(true);
}
}
void SerialComm::sendCommand(const char* str)
{
serialport_write(fd, str);
readCommand();
}
void SerialComm::connectSerialComm()
{
if(fd = serialport_init(portList->currentText().toStdString().c_str(), baudrate))
{
statusBar->showMessage(tr("Connected."));
serialport_flush(fd);
sendCommand("*IDN?\r\n");
}
else
{
statusBar->showMessage(tr("Connection failed. Check port configuration."));
}
}
std::list<std::string> getComList() {
int n;
struct dirent **namelist;
const char* sysdir = "/dev/";
std::string devicedir = sysdir;
std::list<std::string> comList;
// Scan and get all devices from /dev
n = scandir(sysdir, &namelist, NULL, NULL);
if (n < 0)
perror("scandir");
else {
while (n--) {
if (strstr(namelist[n]->d_name,"tty"))
comList.push_back(devicedir + namelist[n]->d_name);
}
free(namelist);
}
// Return the lsit of detected comports
return comList;
}
Worker::Worker() { // Constructor
// you could copy data from constructor arguments to internal variables here.
}
Worker::~Worker() { // Destructor
// free resources
}
void Worker::process() { // Process. Start processing data.
// allocate resources using new here
qDebug("Hello World!");
printf("running...\n");
emit finished();
}