-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpumpinterface.cpp
More file actions
443 lines (375 loc) · 14.2 KB
/
pumpinterface.cpp
File metadata and controls
443 lines (375 loc) · 14.2 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
#include "pumpinterface.h"
#include <QDebug>
#include <QTimer>
/* Interface for the two New Era NE-1002X pumps
* Example usage:
* pumpInterface->broadcastCommand(PumpCommand::Start);
* pumpInterface->sendToPump("Pump A", PumpCommand::SetFlowRate, 1.25);
*/
PumpInterface::PumpInterface(QObject *parent)
: QObject(parent), serial(new QSerialPort(this)) {
connect(serial, &QSerialPort::readyRead, this, &PumpInterface::handleReadyRead);
connect(serial, &QSerialPort::errorOccurred, this, &PumpInterface::handleError);
// Initialize pumps
pumps = {
{0, "PumpA"},
{1, "PumpB"}
};
workerThread = new QThread;
commandWorker = new PumpCommandWorker(this, nullptr);
commandWorker->moveToThread(workerThread);
workerThread->start();
connect(this, &PumpInterface::sendCommandToQueue, commandWorker, &PumpCommandWorker::enqueueCommand, Qt::QueuedConnection);
connect(commandWorker, &PumpCommandWorker::pumpCommandReady, this, &PumpInterface::handlePumpCommand, Qt::QueuedConnection); // <- critical!
}
void PumpInterface::shutdown() {
if (workerThread) {
workerThread->quit();
workerThread->wait();
delete workerThread;
workerThread = nullptr;
}
// this is equivalent to closePort()
if (serial->isOpen()) {
serial->close();
}
}
PumpInterface::~PumpInterface() {
shutdown();
}
void PumpInterface::handlePumpCommand(const QString& name, PumpCommand cmd, QString value) {
sendToPump(name, cmd, value);
}
bool PumpInterface::connectToPumps(const QString &portName, qint32 baudRate) {
if (serial->isOpen()) {
serial->close();
}
serial->setPortName(portName);
serial->setBaudRate(baudRate);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (!serial->open(QIODevice::ReadWrite)) {
emit errorOccurred("Failed to open port: " + serial->errorString());
return false;
}
serial->setDataTerminalReady(true);
serial->setRequestToSend(true);
//qDebug() << "Port opened successfully:" << serial->portName();
// Send initial commands (like GetVersion)
//emit dataReceived("Connecting to pumps! ");
broadcastCommand(PumpCommand::GetVersion);
broadcastCommand(PumpCommand::SetVolUnits); // hardcoded to uL
return true;
}
void PumpInterface::broadcastCommand(PumpCommand cmd, QString value) {
for (const Pump &pump : pumps) {
AddressedCommand command;
command.name = pump.name;
command.cmd = cmd;
command.value = value;
emit sendCommandToQueue(command);
}
}
void PumpInterface::sendToPump(const QString &name, PumpCommand cmd, QString value) {
// Primary reference function -- used externally. Requires the name of the pump
// (which is PumpA or PumpB)
for (const Pump &pump : pumps) {
if (pump.name == name) {
//qDebug() << "sendToPump: #"<<pump.address;
sendCommand(pump.address, cmd, value);
return;
}
}
emit errorOccurred("Pump with name " + name + " not found.");
}
void PumpInterface::setPhases(const QVector<QVector<PumpPhase>> &phases)
{
QVector<PumpPhase> aPhases = phases.at(0);
QVector<PumpPhase> bPhases = phases.at(1);
QVector<AddressedCommand> cmdList;
foreach (PumpPhase phase, aPhases)
{
AddressedCommand phaseNumber;
phaseNumber.name = "PumpA";
phaseNumber.cmd = PumpCommand::SetPhase;
phaseNumber.value = QString::number(phase.phaseNumber);
emit sendCommandToQueue(phaseNumber);
if (phase.function == "RAT")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpA";
phaseFunc.cmd = PumpCommand::RateFunction;
emit sendCommandToQueue(phaseFunc);
AddressedCommand phaseRate;
phaseRate.name = "PumpA";
phaseRate.cmd = PumpCommand::SetFlowRate;
phaseRate.value = QString::number(phase.rate);
emit sendCommandToQueue(phaseRate);
if (phase.volume > 0){
// if volume is zero, lets it run forever
AddressedCommand phaseVol;
phaseVol.name = "PumpA";
phaseVol.cmd = PumpCommand::SetVolume;
phaseVol.value = QString::number(phase.volume);
emit sendCommandToQueue(phaseVol);
}
AddressedCommand phaseDir;
phaseDir.name = "PumpA";
phaseDir.cmd = PumpCommand::SetFlowDirection;
emit sendCommandToQueue(phaseDir);
}
else if (phase.function == "LIN")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpA";
phaseFunc.cmd = PumpCommand::RampFunction;
AddressedCommand phaseRate;
phaseRate.name = "PumpA";
phaseRate.cmd = PumpCommand::SetFlowRate;
phaseRate.value = QString::number(phase.rate);
AddressedCommand phaseTime;
phaseTime.name = "PumpA";
phaseTime.cmd = PumpCommand::SetRampTime;
phaseTime.value = phase.time;
AddressedCommand phaseDir;
phaseDir.name = "PumpA";
phaseDir.cmd = PumpCommand::SetFlowDirection;
emit sendCommandToQueue(phaseFunc);
emit sendCommandToQueue(phaseRate);
emit sendCommandToQueue(phaseTime);
emit sendCommandToQueue(phaseDir);
}
else if (phase.function == "PAUSE")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpA";
phaseFunc.cmd = PumpCommand::PauseFunction;
phaseFunc.value = phase.time;
//AddressedCommand phaseTime;
//phaseTime.name = "PumpA";
//phaseTime.cmd = PumpCommand::SetPause;
//phaseTime.value = phase.time;
emit sendCommandToQueue(phaseFunc);
//emit sendCommandToQueue(phaseTime);
}
else if (phase.function == "STOP"){
AddressedCommand stopPhase;
stopPhase.name = "PumpA";
stopPhase.cmd = PumpCommand::StopFunction;
emit sendCommandToQueue(stopPhase);
}
}
//qDebug() << "PUMP B CMDS!!!!";
foreach (PumpPhase phase, bPhases)
{
AddressedCommand phaseNumber;
phaseNumber.name = "PumpB";
phaseNumber.cmd = PumpCommand::SetPhase;
phaseNumber.value = QString::number(phase.phaseNumber);
emit sendCommandToQueue(phaseNumber);
if (phase.function == "RAT")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpB";
phaseFunc.cmd = PumpCommand::RateFunction;
emit sendCommandToQueue(phaseFunc);
AddressedCommand phaseRate;
phaseRate.name = "PumpB";
phaseRate.cmd = PumpCommand::SetFlowRate;
phaseRate.value = QString::number(phase.rate);
emit sendCommandToQueue(phaseRate);
if (phase.volume > 0){
// if volume is zero, lets it run forever
AddressedCommand phaseVol;
phaseVol.name = "PumpB";
phaseVol.cmd = PumpCommand::SetVolume;
phaseVol.value = QString::number(phase.volume);
emit sendCommandToQueue(phaseVol);
}
AddressedCommand phaseDir;
phaseDir.name = "PumpB";
phaseDir.cmd = PumpCommand::SetFlowDirection;
emit sendCommandToQueue(phaseDir);
}
else if (phase.function == "LIN")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpB";
phaseFunc.cmd = PumpCommand::RampFunction;
AddressedCommand phaseRate;
phaseRate.name = "PumpB";
phaseRate.cmd = PumpCommand::SetFlowRate;
phaseRate.value = QString::number(phase.rate);
AddressedCommand phaseTime;
phaseTime.name = "PumpB";
phaseTime.cmd = PumpCommand::SetRampTime;
phaseTime.value = phase.time;
AddressedCommand phaseDir;
phaseDir.name = "PumpB";
phaseDir.cmd = PumpCommand::SetFlowDirection;
emit sendCommandToQueue(phaseFunc);
emit sendCommandToQueue(phaseRate);
emit sendCommandToQueue(phaseTime);
emit sendCommandToQueue(phaseDir);
}
else if (phase.function == "PAUSE")
{
AddressedCommand phaseFunc;
phaseFunc.name = "PumpB";
phaseFunc.cmd = PumpCommand::PauseFunction;
phaseFunc.value = phase.time;
//AddressedCommand phaseTime;
//phaseTime.name = "PumpB";
//phaseTime.cmd = PumpCommand::SetPause;
//phaseTime.value = phase.time;
emit sendCommandToQueue(phaseFunc);
//emit sendCommandToQueue(phaseTime);
//qDebug() << "TIME: " << phase.time;
}
else if (phase.function == "STOP"){
AddressedCommand stopPhase;
stopPhase.name = "PumpB";
stopPhase.cmd = PumpCommand::StopFunction;
emit sendCommandToQueue(stopPhase);
}
}
}
bool PumpInterface::startPumps(int phase)
{
if (!serial->isOpen()) {
emit errorOccurred("Serial port not open.");
return false;
}
QByteArray command = QString("RUN%1").arg(phase).toUtf8();
QByteArray packet;
packet.append(static_cast<char>('0')+0);
packet.append(command);
packet.append(static_cast<char>('*'));
packet.append(static_cast<char>('0')+1);
packet.append(command);
packet.append(static_cast<char>('*'));
packet.append('\r');
qint64 bytesWritten = serial->write(packet);
//qDebug() << "Sending to pump: " << packet;
return bytesWritten == packet.size();
}
bool PumpInterface::stopPumps()
{
if (!serial->isOpen()) {
emit errorOccurred("Serial port not open.");
return false;
}
QByteArray command = "STP";
QByteArray packet;
packet.append(static_cast<char>('0')+0);
packet.append(command);
packet.append(static_cast<char>('*'));
packet.append(static_cast<char>('0')+1);
packet.append(command);
packet.append(static_cast<char>('*'));
packet.append('\r');
qint64 bytesWritten = serial->write(packet);
//qDebug() << "Sending to pump: " << packet;
QThread::msleep(30);
qint64 bytesWritten2 = serial->write(packet);
return bytesWritten+bytesWritten2 == 2*packet.size();
}
// Private functions
bool PumpInterface::sendCommand(int addr, PumpCommand cmd, QString value) {
// Internal function, not for public use.
// Write the packet to the address of the pump
if (!serial->isOpen()) {
emit errorOccurred("Serial port not open.");
return false;
}
QByteArray command = buildCommand(cmd, value);
QByteArray packet;
packet.append(static_cast<char>('0' + addr));
packet.append(command);
qint64 bytesWritten = serial->write(packet);
qDebug() << "Sending to pump" << addr << ":" << packet;
return bytesWritten == packet.size();
}
QByteArray PumpInterface::buildCommand(PumpCommand cmd, QString value) {
// Base function. Look up the command in our enum, then convert to the
// correct pump terminology
QByteArray payload;
switch (cmd) {
case PumpCommand::Start:
// RUN [phase] will start either the current phase or this number
payload = QString("RUN%1").arg(value.toFloat(),0,'f', 0).toUtf8();
break;
case PumpCommand::Stop:
payload = "STP";
break;
case PumpCommand::RateFunction:
payload = QString("FUNRAT").toUtf8();
break;
case PumpCommand::SetFlowRate:
payload = QString("RAT%1UM").arg(value.toFloat(), 0, 'f', 1).toUtf8();
break;
case PumpCommand::SetVolume:
payload = QString("VOL%1").arg(value.toFloat(),0,'f',0).toUtf8();
break;
case PumpCommand::SetVolUnits:
payload = "VOLUL";
break;
case PumpCommand::SetFlowDirection:
// Any value given will set to withdraw
payload = (value.toInt() > 0) ? "DIRWDR" : "DIRINF";
break;
case PumpCommand::SetPhase:
payload = QString("PHN%1").arg(value.toInt()).toUtf8();
break;
case PumpCommand::RampFunction:
payload = "FUNLIN";
break;
case PumpCommand::StopFunction:
payload = "FUNSTP";
break;
case PumpCommand::PauseFunction:
payload = QString("FUNPAS%1").arg(value.toInt()).toUtf8();
break;
case PumpCommand::SetPause:
payload = QString("PAS%1").arg(value.toInt()).toUtf8();
break;
case PumpCommand::SetRampTime:
// will be in format (00:00), for HH:MM or SS:Tenths depending on phase
payload = QString("TIM%1").arg(value).toUtf8();
break;
case PumpCommand::GetVersion:
payload = "VER";
break;
}
payload.append('\r'); // Required carriage return
//qDebug() << "buildCommand built as: " << payload;
return payload;
}
void PumpInterface::handleReadyRead() {
serialBuffer.append(serial->readAll());
while (true) {
int startIndex = serialBuffer.indexOf(static_cast<char>(0x02));
int endIndex = serialBuffer.indexOf(static_cast<char>(0x03), startIndex + 1);
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) {
// We found a complete frame
QByteArray payload = serialBuffer.mid(startIndex + 1, endIndex - startIndex - 1);
QString readable = QString::fromLatin1(payload);
qDebug() << "Parsed response:" << readable;
emit dataReceived(readable);
// Remove processed data from buffer
serialBuffer.remove(0, endIndex + 1);
} else {
// Either no full frame yet, or partial data still arriving
break;
}
}
// Optionally print buffer contents during debugging
//qDebug() << "Current buffer:" << serialBuffer.toHex(' ');
}
void PumpInterface::handleError(QSerialPort::SerialPortError error) {
if (error == QSerialPort::NoError)
return;
emit errorOccurred("Serial error: " + serial->errorString());
}