-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_helper.cpp
455 lines (374 loc) · 11.3 KB
/
serial_helper.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
448
449
450
451
452
453
454
455
/*
* serial_helper
* =============
* by DJ0ABR
*
* this program solves the problem of ttyUSB numbering. Everytime a serialUSB converter is connected it gets a ttyUSBxxx number.
* This number can change all the time, its never sure which number it gets.
* Using the serial-ID and Vendor-ID of the USBserial adapter we can easily identify the correct serial port.
*
* * identifies a serialUSB adapter
* * opens the serial interface
* * start an RX thread
* * provides a function to read received data async via a thread safe pipe
* * provides a function to send data
*
* int init_serial_interface(char *idserial, char *idVendor, int speed) ...
* create a worker thread and a pipe for a new serial port, then open the port using the SerialID and VendorID of a USBserial Interface
* return ... ID of the serial interface (which is a simple index, starting with 0)
*
* int read_pipe(int idx, char direction) ...
* read data from the serial interface
* idx ... ID returned by init_serial_interface()
* direction ... only 'r' is allowed ('w' could be used, if implemented, by the thread to read data from the user and to send it via serial interface)
*
* usage:
* 1) get the serial- and Vendor ID of your USBserial Interface (see comments in indentifySerUSB.c)
* 2) make a call to init_serial_interface with above IDs and specify the serial speed
* 3) repeadately call read_pipe(idx, 'r') to get the data received from the serial port
*
*
* */
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include "kmfifo.h"
#include "pylonmonitor.h"
#include "identifySerUSB.h"
#include "serial_helper.h"
//#define VERBOSE // activate to print received data into the terminal
int open_serial(char *serdevice, int *pfd_ser, int speed);
int read_serport(int *pfd_ser);
int write_serport(int *pfd_ser, int data);
void initpipes();
void showData(char *devname, int databyte, int pipeidx);
int serport_idx = 0; // increments which each used serial port, used as an index
void *serialproc(void *pdata);
pthread_t serial_tid[MAXSERIALDEVICES];
int sstarted = 0;
typedef struct {
char serdevname[20];
int serspeed;
int seridx;
int fd_serial;
char idSerial[256];
char idVendor[256];
} SERDATA;
SERDATA serdata[MAXSERIALDEVICES];
// ret: serID, -1=error
int init_serial_interface(char *idserial, char *idVendor, int speed)
{
int serid = serport_idx;
initpipes();
// create a new thread for this device
strcpy(serdata[serid].idSerial, idserial);
strcpy(serdata[serid].idVendor, idVendor);
serdata[serid].serspeed = speed;
serdata[serid].seridx = serid;
serdata[serid].fd_serial = -1;
int ret = pthread_create(&serial_tid[serid],NULL,serialproc, &(serdata[serid]));
if(ret)
{
printf("serial process %d NOT started\n",serid);
return -1;
}
serport_idx++;
usleep(100000);
return serid;
}
void changeBaudrate(int serid, int speed)
{
serdata[serid].serspeed = speed;
}
int isOpen(int serid)
{
if(serdata[serid].fd_serial != -1) return 1;
return 0;
}
void *serialproc(void *pdata)
{
int fd_ser = -1; // handle of the ser interface
char devname[20];
int speed;
int idx;
pthread_detach(pthread_self());
SERDATA *psd = (SERDATA *)pdata;
//strcpy(devname,psd->serdevname);
speed = psd->serspeed;
idx = psd->seridx;
printf("process for %s, %s started\n",psd->idSerial,psd->idVendor);
sstarted = 1;
while(keeprunning)
{
// change baud rate
if(psd->serspeed != speed)
{
speed = psd->serspeed;
if(fd_ser != -1) close(fd_ser);
fd_ser = -1;
}
// if the device is closed, open it
if(fd_ser == -1)
{
char *sif = get_serial_device_name(psd->idSerial, psd->idVendor);
if(sif)
{
strcpy(devname,sif); // sif has the valid device name
open_serial(devname,&fd_ser,speed);
psd->fd_serial = fd_ser;
usleep(1000);
}
else
usleep(1000000); // device not found, wait a little longer
}
else
{
int action = 0;
// serial IF is open, read data unblocked
int d = read_serport(&fd_ser);
if(d != -1)
{
showData(devname,d,idx);
write_serpipe(idx,d,'r');
action = 1;
}
// check if we got data to send
int rp = read_serpipe(idx,'w');
if(rp != -1)
{
write_serport(&fd_ser,rp);
action = 1;
}
if(action == 0)
usleep(10000); // nothing to do, wait 10ms
}
}
printf("exit serial thread\n");
if(fd_ser != -1) close(fd_ser);
pthread_exit(NULL);
}
#define MAXTEXTLEN 100
void showData(char *devname, int databyte, int pipeidx)
{
#ifdef VERBOSE
static int charnum[MAXSERIALDEVICES];
static char text[MAXSERIALDEVICES][(MAXTEXTLEN+1)*3];
static int f = 1;
if(f)
{
f = 0;
memset(text,0,(MAXTEXTLEN+1)*3*MAXSERIALDEVICES);
memset(charnum,0,MAXSERIALDEVICES*sizeof(int));
}
// a new line is either a \n or if the maximum number of chars per line is exceeded
if(databyte == '\n' || charnum[pipeidx] >= MAXTEXTLEN)
{
charnum[pipeidx] = 0;
printf("\n%s %d: %s",devname,pipeidx,text[pipeidx]);
*text[pipeidx] = 0;
}
if(databyte != '\n')
{
if(databyte >= ' ' && databyte <= 'z')
sprintf(text[pipeidx]+strlen(text[pipeidx]),"%c",databyte);
else
sprintf(text[pipeidx]+strlen(text[pipeidx]),"x%02X",databyte);
charnum[pipeidx]++;
}
#endif
}
// open serial interface
// 1=ok, 0=error
int open_serial(char *serdevice, int *pfd_ser, int speed)
{
struct termios tty;
if(*pfd_ser != -1) close(*pfd_ser);
printf("Open: %s\n",serdevice);
*pfd_ser = open(serdevice, O_RDWR | O_NDELAY);
if (*pfd_ser < 0) {
printf("error when opening %s, errno=%d\n", serdevice,errno);
return 0;
}
if (tcgetattr(*pfd_ser, &tty) != 0) {
printf("error %d from tcgetattr %s\n", errno,serdevice);
return 0;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag &= ~ICRNL; // binary mode (no CRLF conversion)
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 0; // 0=nonblocking read, 1=blocking read
tty.c_cc[VTIME] = 0;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD | CSTOPB);
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(*pfd_ser, TCSANOW, &tty) != 0) {
printf("error %d from tcsetattr %s\n", errno, serdevice);
*pfd_ser = -1;
return 0;
}
// set RTS/DTR
int flags;
ioctl(*pfd_ser, TIOCMGET, &flags);
flags &= ~TIOCM_DTR;
flags &= ~TIOCM_RTS;
ioctl(*pfd_ser, TIOCMSET, &flags);
printf("%s opened\n",serdevice);
return 1;
}
void setRTS(int *pfd_ser)
{
}
// read one byte non blocking
// ret: byte or -1 if no data
int read_serport(int *pfd_ser)
{
static unsigned char c;
// test if serial IF is still available
int flags;
if(ioctl(*pfd_ser, TIOCMGET, &flags) == -1)
{
// serial IF lost
*pfd_ser = -1;
return -1;
}
if(*pfd_ser != -1)
{
int rxlen = read(*pfd_ser, &c, 1);
if( rxlen <= 0)
return -1;
return (unsigned int)c;
}
return -1;
}
int write_serport(int *pfd_ser, int data)
{
// test if serial IF is still available
int flags;
if(ioctl(*pfd_ser, TIOCMGET, &flags) == -1)
{
// serial IF lost
*pfd_ser = -1;
return -1;
}
if(*pfd_ser != -1)
{
uint8_t d = data & 0xff;
int wrlen = write(*pfd_ser,&d,1);
if(wrlen != 1)
return -1;
return 0;
}
return -1;
}
// ================== serial pipes =======================
#define NUM_OF_PIPES MAXSERIALDEVICES*2 // *2 weil einmal für direction 'r' und einmal für 'w'
pthread_mutex_t crit_sec[NUM_OF_PIPES];
pthread_mutex_t waitmutex[NUM_OF_PIPES];
pthread_cond_t semaphore[NUM_OF_PIPES];
#define LOCK(pn) pthread_mutex_lock(&(crit_sec[pn]))
#define UNLOCK(pn) pthread_mutex_unlock(&(crit_sec[pn]))
#define WAKEUP(pn) pthread_cond_signal(&(semaphore[pn]));
#define BUFFER_LENGTH 1000
int wridx[NUM_OF_PIPES];
int rdidx[NUM_OF_PIPES];
uint8_t pipebuffer[NUM_OF_PIPES][BUFFER_LENGTH];
void initpipes()
{
static int f=1;
if(f==0) return; // already initialized
for (int i = 0; i < NUM_OF_PIPES; i++)
{
pthread_mutex_init(&(crit_sec[i]),NULL);
pthread_cond_init(&(semaphore[i]),NULL);
pthread_mutex_init(&(waitmutex[i]),NULL);
rdidx[i] = wridx[i] = 0;
}
f=0;
}
// returns: -1=pipe full ,0=ok
int check_pipe(int idx, char direction)
{
int pipenum;
pipenum = idx*2;
if(direction == 'w') pipenum+=1;
if (pipenum >= NUM_OF_PIPES || pipenum < 0)
{
printf("invalid write_pipe pipnum: %d idx:%d\n", pipenum,idx);
exit(0);
}
LOCK(pipenum);
int *pwridx = &(wridx[pipenum]);
int v = ((*pwridx + 1) % BUFFER_LENGTH) == rdidx[pipenum];
UNLOCK(pipenum);
if (v)
return -1;
return 0;
}
// ret: 0=ok, -1=error
int write_serpipe(int idx, int data, char direction)
{
int pipenum;
pipenum = idx*2;
if(direction == 'w') pipenum+=1;
if (pipenum >= NUM_OF_PIPES || pipenum < 0)
{
printf("invalid write_pipe pipnum: %d idx:%d\n", pipenum,idx);
exit(0);
}
LOCK(pipenum);
int *pwridx = &(wridx[pipenum]);
// prüfe ob im Fifo noch Platz ist
if (((*pwridx + 1) % BUFFER_LENGTH) == rdidx[pipenum])
{
UNLOCK(pipenum);
printf("no space left in pipenum: %d\n", pipenum);
return -1;
}
//printf("write pipe %d: %02X\n",pipenum,data);
// schreibe Daten in den Fifo
pipebuffer[pipenum][*pwridx] = (unsigned char)data;
// stelle Schreibzeiger weiter
if (++*pwridx == BUFFER_LENGTH) *pwridx = 0;
UNLOCK(pipenum);
return 0;
}
int read_serpipe(int idx, char direction)
{
int pipenum;
pipenum = idx*2;
if(direction == 'w') pipenum+=1;
if (pipenum >= NUM_OF_PIPES)
{
printf("invalid read_pipe pipnum: %d\n", pipenum);
exit(0);
}
LOCK(pipenum);
int *prdidx = &(rdidx[pipenum]);
if (*prdidx == wridx[pipenum])
{
// Fifo ist leer
UNLOCK(pipenum);
return -1;
}
// lese Daten
int d = pipebuffer[pipenum][*prdidx];
// stelle Lesezeiger weiter
if (++*prdidx == BUFFER_LENGTH) *prdidx = 0;
UNLOCK(pipenum);
// printf("%d read: %02X\n",pipenum,d);
return d;
}