-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.cpp
447 lines (370 loc) · 10.4 KB
/
comm.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* @file comm.cpp
* @brief C++ Implementation: comm
*
* @details This file holds the Comm class implementation which creates communication channel to microcontroller,
polls events from controller, realizes communication protocol.
*
* Copyright: See COPYING file that comes with this distribution
*
* @author Valdur Kaldvee (C) 2008
* @author Margus Ernits <[email protected]>, (C) 2008
* @author Mauno Pihelgas <[email protected]>, (C) 2010
* @author Erik Kaju
*/
#include "comm.h"
#include <iostream>
#include <errno.h>
#include <stdlib.h>
#include <QDebug>
#include <iterator>
Comm::Comm()
{
Config & temp =Config::getConfig();
conf = & temp;
for(int i=0; i<MAX_DEVICES; i++) {
fds[i]=-1;
}
}
Comm::~Comm()
{
close(fd);
}
/*!
\fn Comm::openSerial()
*/
int Comm::openSerial(const char * modemDevice)
{
return openSerial(modemDevice, 0);
}
/*!
\fn Comm::openSerial()
*/
int Comm::openSerial(const char * modemDevice, const int nr)
{
char serialDevice[150] = "/dev/ttyS0";
serialDevice[11] = nr + '0';
struct termios options;
std::cout<<"Serial Open "<<serialDevice<<std::endl;
/* open the port */
// if (modemDevice!=NULL){
//TODO: getmodemDevice peaks ka nr ette saama
fds[nr] = open(serialDevice, O_RDWR | O_NOCTTY | O_NDELAY);
/*if(0 && conf->getmodemDevice()!= NULL){
fds[nr] = open(conf->getmodemDevice(), O_RDWR | O_NOCTTY | O_NDELAY);
}
else{
fds[nr] = open("/dev/usbserial", O_RDWR | O_NOCTTY | O_NDELAY);
}*/
if (fds[nr] == -1) {
//Could not open the port.
perror("open_port: Unable to open device - ");
}
else fcntl(fds[nr], F_SETFL, 0);
/* get the current options */
tcgetattr(fds[nr], &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
/* 8N1 */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
/* set the options */
tcsetattr(fds[nr], TCSANOW, &options);
fd=fds[nr];
return (fds[nr]);
}
/*!
\fn Comm::sendCommand()
@Author Valdur Kaldvee
*/
int Comm::sendCommand(char addr, int value)
{
return sendCommand(addr, value, 0);
}
int Comm::sendCommand(char addr, int value, int nr)
{
// isLocked.wait(&mutex);
QMutexLocker locker(&mutex);
if((conf->getSendCmdEnabled())==0) return 1;
int len;
unsigned char tmp = 0;
char buf[50];
buf[49] = 0;
sprintf(buf, "\r");
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("sendCommand write error - ");
Comm::reopen("", nr);
return 1;
}
len = 0;
tmp = 128 | addr;
snprintf(buf, sizeof buf, "%d\r%d\r", tmp, value);
if (strlen(buf) < 50) {
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
}
if (len < 0) {
perror("sendCommand write error - ");
Comm::reopen("", nr);
return 1;
}
return 0;
}
int Comm::sendCommand3(int addr, int value1, int value2, int value3)
{
// isLocked.wait(&mutex);
QMutexLocker locker(&mutex);
if((conf->getSendCmdEnabled())==0) return 1;
int nr = 0;
int len;
char buf[50];
buf[49] = 0;
sprintf(buf, "\r");
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("sendCommand write error - ");
Comm::reopen("", nr);
return 1;
}
len = 0;
snprintf(buf, sizeof buf, "%d\r%d,%d,%d\r", addr, value1, value2, value3);
if (strlen(buf) < 50) {
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
}
if (len < 0) {
perror("sendCommand write error - ");
Comm::reopen("", nr);
return 1;
}
return 0;
}
int Comm::readSerialMulti(char addr, int * microControllerData)
{
return readSerialMulti(addr, microControllerData, 0);
}
int Comm::readSerialMulti(char addr, int * microControllerData, int nr)
{
// isLocked.wait(&mutex);
QMutexLocker locker(&mutex);
len = 0;
int buf_len = 0;
memset(buf, 0, sizeof buf);
sprintf(buf, "\r");
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("readSerialMulti write error - ");
Comm::reopen("", nr);
return 1;
}
len = 0;
snprintf(buf, sizeof buf, "%d\r", addr);
if (strlen(buf) < 255) {
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
}
if (len < 0) {
perror("readSerialMulti write error - ");
Comm::reopen("", nr);
return 1;
}
do {
len = read(fds[nr], &buf[buf_len], 255 - buf_len);
buf_len += len;
if (len < 1 || buf_len > 254) {
return 1;
}
} while (buf[buf_len-1] != '\n');
int i, j;
int start = -1;
j = 0;
for (i = 0; i < buf_len; i++) {
if (isdigit(buf[i]) && start == -1) start = 0;
if ((buf[i] == ',' || buf[i] == '\n') && start > -1) {
microControllerData[j] = atoi(&buf[start]);
j++;
start = i+1;
}
if (j > 15 ) break;
}
if (j != 16) {
std::cout<<"Values: "<<j<<" Buf: "<<buf<<" Len: "<<strlen(buf)<<std::endl;
std::cout<<microControllerData[0]<<std::endl;
std::cout<<microControllerData[1]<<std::endl;
std::cout<<microControllerData[2]<<std::endl;
std::cout<<microControllerData[3]<<std::endl;
std::cout<<microControllerData[4]<<std::endl;
std::cout<<microControllerData[5]<<std::endl;
std::cout<<microControllerData[6]<<std::endl;
std::cout<<microControllerData[7]<<std::endl;
std::cout<<microControllerData[8]<<std::endl;
std::cout<<microControllerData[9]<<std::endl;
std::cout<<microControllerData[10]<<std::endl;
std::cout<<microControllerData[11]<<std::endl;
std::cout<<microControllerData[12]<<std::endl;
std::cout<<microControllerData[13]<<std::endl;
std::cout<<microControllerData[14]<<std::endl;
std::cout<<microControllerData[15]<<std::endl;
}
return 1;
}
int Comm::requestSerialMulti(char addr)
{
return requestSerialMulti(addr, 0);
}
int Comm::requestSerialMulti(char addr, int nr)
{
// isLocked.wait(&mutex);
len = 0;
QMutexLocker locker(&mutex);
//if((conf->getSendCmdEnabled())==0) return 1;
memset(buf, 0, sizeof buf);
sprintf(buf, "\r");
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("readSerialMulti write error - ");
Comm::reopen("", nr);
return 1;
}
len = 0;
snprintf(buf, sizeof buf, "%d\r", addr);
if (strlen(buf) < 255) {
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
}
if (len < 0) {
perror("readSerialMulti write error - ");
Comm::reopen("", nr);
return 1;
}
return 1;
}
int Comm::serialMultiResponse(int * microcontrollerData, int nr){
int buf_len = 0;
do {
len = read(fds[nr], &buf[buf_len], 255 - buf_len);
buf_len += len;
if (len < 1 || buf_len > 254) {
return 1;
}
} while (buf[buf_len-1] != '\n');
int serialBufferIndex, microControllerDataIndex;
microControllerDataIndex = 0;
int data[MSG_MC_DATA_NR_ELEMENTS];
bool checksumValid = false;
bool readingNextIntChar = false;
for (serialBufferIndex = 0; serialBufferIndex < buf_len; serialBufferIndex++) {
if (serialBufferIndex == 0) {
if (buf[serialBufferIndex] == MSG_MC_DATA_HEADER) {
readingNextIntChar = true;
continue;
} else {
qDebug() << "Reading microcontroller data failed. Wrong message header:" << buf[serialBufferIndex];
return 1;
}
}
if (buf[serialBufferIndex] == ',') {
readingNextIntChar = true;
continue;
} else if (isdigit(buf[serialBufferIndex]) || buf[serialBufferIndex] == '-'){
if (readingNextIntChar) {
if (microControllerDataIndex < MSG_MC_DATA_NR_ELEMENTS) {
data[microControllerDataIndex] = atoi(&buf[serialBufferIndex]);
microControllerDataIndex++;
readingNextIntChar = false;
} else {
int checksumInTheMessage = atoi(&buf[serialBufferIndex]);
checksumValid = checksumInTheMessage == fletcherChecksum(data,MSG_MC_DATA_NR_ELEMENTS);
if (!checksumValid) qDebug() << "Reading microcontroller data failed. Checksum was not right: " << checksumInTheMessage;
break;
}
}
} else {
qDebug() << "Reading microcontroller data failed. Unexpected character at index:" << serialBufferIndex << "=" << buf[serialBufferIndex];
return 1;
}
}
if (checksumValid) {
for (int i=0; i < MSG_MC_DATA_NR_ELEMENTS; i++) microcontrollerData[i] = data[i];
return 0;
} else {
qDebug() << "Reading microcontroller data failed. Checksum was not right";
return 1;
}
}
uint8_t Comm::fletcherChecksum(int * microcontrollerData, int dataLength) {
uint8_t sum1 = 0;
uint8_t sum2 = 0;
while (dataLength--){
sum1 += *microcontrollerData++;
sum2 += sum1;
}
return (sum1 & 0xF) | (sum2 << 4);
}
/*!
\fn Comm::readSerial(char)
*/
int Comm::readSerial(char addr)
{
return readSerial(addr, 0);
}
int Comm::readSerial(char addr, int nr)
{
int len;
int buf_len = 0;
char buf[10];
// isLocked.wait(&mutex);
QMutexLocker locker(&mutex);
sprintf(buf, "\r");
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("readSerial write error - ");
Comm::reopen("", nr);
return 1;
}
sprintf(buf, "%d\r", addr);
len = write(fds[nr], buf, strlen(buf));
tcflush(fds[nr], TCIFLUSH);
if (len < 0) {
perror("readSerial write error - ");
Comm::reopen("", nr);
return 1;
}
do {
len = read(fds[nr], &buf[buf_len], 10);
buf_len += len;
if (len == 0 || buf_len > 10) return 2;
} while (buf[buf_len-1] != '\n');
return atoi(buf);
}
//TODO Siin ka korda seada õige serial device kasutamine.
inline void Comm::reopen(const char * device)
{
reopen(device, 0);
return;
}
inline void Comm::reopen(const char * device, int nr)
{
close(fds[nr]);
Comm::openSerial(device, nr);
if(device!=""){
Comm::openSerial(device, nr);
return;
}
Comm::openSerial(device, nr);
return;
}