-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial.cpp
423 lines (346 loc) · 9.84 KB
/
serial.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
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
/*
* File: serial.cpp
* Author: Mark Furneaux
* Copyright: 2014 Romaco Canada, Mark Furneaux
*
* Created on December 27, 2014, 10:05 PM
*
* This file is part of PSUControl.
*/
#include <cstdlib>
#include <ncurses.h>
#include <errno.h>
#include <math.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <string>
#include "serial.h"
/*
* Sets the output of the PSU on or off. Note that the protocol is inverted.
*/
void setOutput(int fd, bool on) {
std::string cmd = "SOUT";
cmd.append((on) ? "0" : "1");
cmd.append("\r");
sendData(fd, cmd.c_str());
readData(fd);
}
/*
* Sets the desired voltage of the PSU.
*/
void setVoltage(int fd, double voltage) {
std::string cmd = "VOLT";
char param[4];
//do nothing for invalid voltage setting
if (voltage > maxVoltage || voltage < 0) {
return;
}
voltage *= 10;
sprintf(param, "%03d", (int) round(voltage));
cmd.append(param);
cmd.append("\r");
sendData(fd, cmd.c_str());
readData(fd);
}
/*
* Sets the desired current of the PSU.
*/
void setCurrent(int fd, double current) {
std::string cmd = "CURR";
char param[4];
//do nothing for invalid current setting
if (current > maxCurrent || current < 0) {
return;
}
current *= currentDivider;
sprintf(param, "%03d", (int) round(current));
cmd.append(param);
cmd.append("\r");
sendData(fd, cmd.c_str());
readData(fd);
}
/*
* Prompts the user for the serial port location and retrieves the input.
*/
bool getSerialPort(char* portAddr) {
printw("Enter the serial port to use [or press Enter for /dev/ttyUSB0]: ");
if (scanw("%s", portAddr) == 1) {
if (strlen(portAddr) != 0) {
//TODO check if exists
return true;
}
} else {
strcpy(portAddr, "/dev/ttyUSB0");
return true;
}
return false;
}
/*
* Sets up the serial port for the PSU.
* TODO none of the errors are visible because endwin() is called after
*/
int set_interface_attribs(int fd, speed_t speed, int parity) {
struct termios config;
if (!isatty(fd)) {
printw("Error: Not a TTY!");
refresh();
return -1;
}
if (tcgetattr(fd, &config) < 0) {
printw("Error: Could not get port attributes!");
refresh();
return -2;
}
//input flags - Turn off input processing
//convert break to null byte,
//no NL to CR translation, don't mark parity errors or breaks
//no input parity check, don't strip high bit off,
//no XON/XOFF software flow control
config.c_iflag &= ~(IGNBRK | BRKINT |
INLCR | PARMRK | INPCK | ISTRIP | IXON);
//convert CR to NL as the PSU only sends CR
config.c_iflag |= ICRNL;
//output flags - Turn off output processing
//no CR to NL translation, no NL to CR-NL translation,
//no NL to CR translation, no column 0 CR suppression,
//no Ctrl-D suppression, no fill characters, no case mapping,
//no local output processing
//
// config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
// ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
config.c_oflag = 0;
//no line processing:
//echo off, echo newline off,
//extended input processing off, signal chars off
config.c_lflag &= ~(ECHO | ECHONL | IEXTEN | ISIG);
//canonical mode on
config.c_lflag |= ICANON;
//turn off character processing
//clear current char size mask, no parity checking,
//no output processing, force 8 bit input
config.c_cflag &= ~(CSIZE | PARENB | CLOCAL | CRTSCTS);
config.c_cflag |= CS8;
//no blocking on read
//inter-character timer to 10s
config.c_cc[VMIN] = 0;
config.c_cc[VTIME] = 100;
//set communication speed
if (cfsetispeed(&config, speed) < 0 || cfsetospeed(&config, speed) < 0) {
printw("Error: Could not set port speed!");
refresh();
return -3;
}
//apply the configuration
if (tcsetattr(fd, TCSANOW, &config) < 0) {
printw("Error: Could not set attributes!");
refresh();
return -4;
}
return 0;
}
/*
* TODO ?????????????????????????????????????????????????????????
*/
bool isOK(const char* recvBuff) {
std::string buf = recvBuff;
if (buf.find("OK") == std::string::npos) {
return false;
}
return true;
}
/*
* Read a buffer (line) from the serial port.
*/
std::string readData(int fd) {
int bytesRead = 0;
char recvBuff[100]{};
bytesRead = read(fd, recvBuff, sizeof recvBuff);
std::string buf = recvBuff;
//DEBUG to be removed
// move(LINES, 0);
// clrtoeol();
// printw("%d bytes read from SP", bytesRead);
// refresh();
// if (!isOK(recvBuff)) {
// move(14, 0);
// printw("ERROR: PSU returned a NOT OK on the last command!");
// move(15, 0);
// printw("Buffer returned was: %s", recvBuff);
// refresh();
// }
// if (buf.find("\rOK") != std::string::npos) {
// buf = buf.substr(0, buf.length() - 3);
// } else if (buf.find("OK") != std::string::npos) {
// buf = buf.substr(0, buf.length() - 2);
// }
return buf;
}
/*
* Get the current settings of the PSU.
*/
void getSettings(int fd) {
sendData(fd, "GETS");
std::string data = readData(fd);
if (data.length() == 0) {
return;
}
readData(fd);
// if (!isOK(readData(fd))) {
// move(14, 0);
// printw("ERROR: PSU returned a NOT OK on the last command!");
// move(15, 0);
// printw("Buffer returned was: %s", recvBuff);
// refresh();
// }
//separate voltage and current values
double voltage = std::stod(data.substr(0, 3), NULL);
double current = std::stod(data.substr(3, 3), NULL);
voltage /= 10;
current /= currentDivider;
currVoltage = voltage;
currCurrent = current;
move((LINES / 2) - 2, (COLS / 2) - 23);
clrtoeol();
printw("Set Voltage: ");
attron(A_BOLD);
printw("%2.1fV", voltage);
attroff(A_BOLD);
move((LINES / 2) - 2, (COLS / 2) + 2);
clrtoeol();
printw("Set Current: ");
attron(A_BOLD);
printw("%2.1fA", current);
attroff(A_BOLD);
refresh();
}
/*
* Get the current live values as read by the PSU.
*/
void getCurrent(int fd) {
sendData(fd, "GETD");
std::string data = readData(fd);
if (data.length() == 0 || data.length() < 9) {
return;
}
readData(fd);
// if (!isOK(readData(fd))) {
// move(14, 0);
// printw("ERROR: PSU returned a NOT OK on the last command!");
// move(15, 0);
// printw("Buffer returned was: %s", recvBuff);
// refresh();
// }
//separate the voltage, current, and mode
double voltage = std::stod(data.substr(0, 4), NULL);
double current = std::stod(data.substr(4, 4), NULL);
bool mode = (bool)std::atoi(data.substr(8, 1).c_str());
//get the output mode
bool output = getOutputStatus(fd);
voltage /= 100;
// Empirically, the 1685B still reports its operating current in the same
// format, so we do *not* use currentDivider here
current /= 100;
move((LINES / 2) - 1, (COLS / 2) - 23);
clrtoeol();
printw("Actual Voltage: ");
attron(A_BOLD);
printw("%2.2fV", voltage);
attroff(A_BOLD);
move((LINES / 2) - 1, (COLS / 2) + 2);
printw("Actual Current: ");
attron(A_BOLD);
printw("%2.2fA", current);
attroff(A_BOLD);
move((LINES / 2) + 1, (COLS / 2) - 23);
clrtoeol();
printw("Mode: ");
attron(A_BOLD);
if (mode) {
printw("Constant Current");
} else {
printw("Constant Voltage");
}
attroff(A_BOLD);
move((LINES / 2) + 1, (COLS / 2) + 2);
printw("Output: ");
//highlight if the output if off
if (output) {
attron(A_REVERSE | A_BOLD);
printw("OFF");
attroff(A_REVERSE | A_BOLD);
} else {
attron(A_BOLD);
printw("ON");
attroff(A_BOLD);
}
refresh();
}
/*
* Send a string to the serial port.
*/
int sendData(int fd, const char* data) {
std::string dataStr = data;
dataStr.append("\r");
return write(fd, dataStr.c_str(), dataStr.length());
}
/*
* Get the state of the output, on or off.
*/
bool getOutputStatus(int fd) {
sendData(fd, "GOUT");
std::string data = readData(fd);
readData(fd);
outputOn = (bool)std::atoi(data.c_str());
return outputOn;
}
/*
* Toggle locking of the front panel knobs.
* This is an undocumented command, so it is very flaky.
*/
void toggleRemote(int fd) {
//can't combine these as it doesn't work...
sendData(fd, "SESS\r");
readData(fd);
sendData(fd, "SESS\r");
readData(fd);
}
/*
* Get the max current and voltage of the PSU.
*/
void getDeviceCapabilities(int fd) {
sendData(fd, "GMAX");
std::string data = readData(fd);
if (data.length() == 0) {
return;
}
readData(fd);
// if (!isOK(readData(fd))) {
// move(14, 0);
// printw("ERROR: PSU returned a NOT OK on the last command!");
// move(15, 0);
// printw("Buffer returned was: %s", recvBuff);
// refresh();
// }
//separate the voltage and current
double voltage = std::stod(data.substr(0, 3), NULL);
double current = std::stod(data.substr(3, 3), NULL);
voltage /= 10;
if (voltage >= 60.0) {
// the 1685B has one more digit of current precision everywhere
currentDivider = 100;
} else {
currentDivider = 10;
}
current /= currentDivider;
maxCurrent = current;
maxVoltage = voltage;
move(0, COLS - 38);
clrtoeol();
printw("Max Voltage: %2.1fV Max Current: %2.1fA", maxVoltage, maxCurrent);
refresh();
}