-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
355 lines (314 loc) · 8.02 KB
/
serial.c
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
/* Serial port layer
*
* $Id: serial.c,v 1.6 2007-07-02 17:07:49 pzn Exp $
*
* (C) 2006 - Pedro Zorzenon Neto
*
* To use this this you must agree with its licence terms
* License: GNU Lesser General Public License
* http://www.fsf.org/licensing/licenses/lgpl.html
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include "serial.h"
#include "assure.h"
serial_t * serial_init (char * path, unsigned int bps, unsigned int mode) {
serial_t * self;
struct termios term;
speed_t spd = 0;
int i;
self = malloc (sizeof(serial_t));
assure (self != NULL);
self->initial_term = malloc (sizeof(struct termios));
assure (self->initial_term != NULL);
if (mode && SERIAL_AUTOFLUSH) {
self->autoflush = 1;
} else {
self->autoflush = 0;
}
self->fd = open(path, O_RDWR);
if (self->fd == -1) {
/* open error */
free (self);
return NULL;
}
/* save terminal setting for restoring at serial_destroy */
assure (tcgetattr (self->fd, self->initial_term) == 0);
/* get current settings */
assure (tcgetattr (self->fd, &term) == 0);
/* input mode flags */
term.c_iflag &= ~ ( BRKINT | ICRNL | IGNCR | INLCR | IGNPAR | INPCK |
PARMRK | ISTRIP | IXOFF | IXON);
term.c_iflag |= IGNBRK;
/* output mode flags */
term.c_oflag &= ~ ( OPOST | ONLCR | OCRNL | ONOCR | ONLRET | OFILL |
OFDEL | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY |
FFDLY);
/* local mode flags */
term.c_lflag &= ~ ( ECHO | ECHOE | ECHOK | ECHONL | ICANON | IEXTEN |
ISIG | NOFLSH | TOSTOP );
/* control mode flags */
term.c_cflag &= ~ ( HUPCL );
term.c_cflag |= CREAD;
/* parity settings */
i = mode & ( SERIAL_PAR_NONE | SERIAL_PAR_ODD | SERIAL_PAR_EVEN |
SERIAL_PAR_MARK | SERIAL_PAR_SPACE | SERIAL_PAR_KEEP);
switch(i) {
case SERIAL_PAR_NONE:
term.c_cflag &= ~PARENB;
break;
case SERIAL_PAR_ODD:
term.c_cflag |= PARENB | PARODD;
break;
case SERIAL_PAR_EVEN:
term.c_cflag |= PARENB;
term.c_cflag &= PARODD;
break;
case SERIAL_PAR_KEEP:
break;
case SERIAL_PAR_MARK: /* unsupported */
case SERIAL_PAR_SPACE: /* unsuported */
default:
assure(0);
}
/* data bits settings */
i = mode & ( SERIAL_BITS_8 | SERIAL_BITS_7 |
SERIAL_BITS_6 | SERIAL_BITS_5 | SERIAL_BITS_KEEP);
switch(i) {
case SERIAL_BITS_8:
term.c_cflag &= ~(CS5 | CS6 | CS7 | CS8);
term.c_cflag |= CS8;
break;
case SERIAL_BITS_7:
term.c_cflag &= ~(CS5 | CS6 | CS7 | CS8);
term.c_cflag |= CS7;
break;
case SERIAL_BITS_6:
term.c_cflag &= ~(CS5 | CS6 | CS7 | CS8);
term.c_cflag |= CS6;
break;
case SERIAL_BITS_5:
term.c_cflag &= ~(CS5 | CS6 | CS7 | CS8);
term.c_cflag |= CS5;
break;
case SERIAL_BITS_KEEP:
break;
default:
assure(0);
}
/* stop bit settings */
i = mode & ( SERIAL_STOP_1 | SERIAL_STOP_2 | SERIAL_STOP_KEEP);
switch (i) {
case SERIAL_STOP_1:
term.c_cflag &= ~CSTOPB;
break;
case SERIAL_STOP_2:
term.c_cflag |= CSTOPB;
break;
case SERIAL_STOP_KEEP:
break;
default:
assure(0);
}
/* flow control */
i = mode & ( SERIAL_FLOW_NONE | SERIAL_FLOW_HARD | SERIAL_FLOW_SOFT |
SERIAL_FLOW_KEEP );
switch (i) {
case SERIAL_FLOW_NONE:
term.c_cflag |= CLOCAL;
term.c_cflag &= ~CRTSCTS;
break;
case SERIAL_FLOW_HARD:
term.c_cflag &= ~CLOCAL;
term.c_cflag |= CRTSCTS;
break;
case SERIAL_FLOW_KEEP:
break;
case SERIAL_FLOW_SOFT: /* do not know if it makes sense non-canonical
XON/XOFF flow control... */
default:
assure(0);
}
/* control characters */
//term.c_cc[VINTR] = 0;
term.c_cc[VMIN] = 1;
//term.c_cc[VQUIT] = 0;
//term.c_cc[VSUSP] = 0;
term.c_cc[VTIME] = 5;
//term.c_cc[VSTART] = 0;
//term.c_cc[VSTOP] = 0;
/* set speed */
switch (bps) {
case SERIAL_300BPS: spd=B300; break;
case SERIAL_1200BPS: spd=B1200; break;
case SERIAL_2400BPS: spd=B2400; break;
case SERIAL_4800BPS: spd=B4800; break;
case SERIAL_9600BPS: spd=B9600; break;
case SERIAL_19200BPS: spd=B19200; break;
case SERIAL_38400BPS: spd=B38400; break;
case SERIAL_57600BPS: spd=B57600; break;
case SERIAL_115200BPS: spd=B115200; break;
case SERIAL_230400BPS: spd=B230400; break;
case SERIAL_460800BPS: spd=B460800; break;
case SERIAL_500000BPS: spd=B500000; break;
case SERIAL_576000BPS: spd=B576000; break;
case SERIAL_921600BPS: spd=B921600; break;
case SERIAL_1000000BPS: spd=B1000000; break;
case SERIAL_1152000BPS: spd=B1152000; break;
case SERIAL_1500000BPS: spd=B1500000; break;
case SERIAL_2000000BPS: spd=B2000000; break;
case SERIAL_2500000BPS: spd=B2500000; break;
case SERIAL_3000000BPS: spd=B3000000; break;
case SERIAL_3500000BPS: spd=B3500000; break;
case SERIAL_4000000BPS: spd=B4000000; break;
case SERIAL_BPS_KEEP: break;
default: assure(0);
}
if (spd != SERIAL_BPS_KEEP) {
assure(cfsetispeed (&term, spd) == 0);
assure(cfsetospeed (&term, spd) == 0);
}
/* set terminal attributes */
assure (tcsetattr(self->fd, TCSANOW, &term) == 0);
return self;
}
void serial_destroy (serial_t * self) {
if (self != NULL) {
int i;
i = tcsetattr (self->fd, TCSANOW, self->initial_term);
if (i != 0) {
/* could not restore terminal settings */
}
close (self->fd);
free (self);
self = NULL;
}
}
int serial_has_data (serial_t * self,
long int timeout_sec,
long int timeout_usec) {
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(self->fd, &rfds);
tv.tv_sec = timeout_sec;
tv.tv_usec = timeout_usec;
if (select(self->fd+1, &rfds, NULL, NULL, &tv) > 0) {
return 1;
}
return 0;
}
int serial_getc (serial_t * self) {
ssize_t s;
unsigned char c;
s = read(self->fd, &c, 1);
if (s != 1) {
return -1;
}
return (int) c;
}
void serial_putc (serial_t * self, unsigned char c) {
if (write(self->fd, &c, 1) != 1) {
/* write error */
}
if (self->autoflush) {
fdatasync(self->fd);
}
}
void serial_write (serial_t * self, unsigned char * s, unsigned int size) {
if (write(self->fd, s, size) != size) {
/* write error */
}
if (self->autoflush) {
fdatasync(self->fd);
}
}
void serial_puts (serial_t * self, unsigned char * s) {
int size;
size = strlen((char*)s);
if (write(self->fd, s, size) != size) {
/* write error */
}
if (self->autoflush) {
fdatasync(self->fd);
}
}
void serial_flush (serial_t * self) {
fdatasync(self->fd);
}
void serial_discard (serial_t * self, int max) {
int i;
do {
i = serial_has_data (self, 0, 0);
if (i) {
serial_getc(self);
if (max > 0) max--;
if (max == 0) i = 0; /* force a stop */
}
} while (i);
}
unsigned int serial_bps (int bps) {
switch (bps) {
case 1200:
return SERIAL_1200BPS;
case 2400:
return SERIAL_2400BPS;
case 4800:
return SERIAL_4800BPS;
case 9600:
return SERIAL_9600BPS;
case 19200:
return SERIAL_19200BPS;
case 38400:
return SERIAL_38400BPS;
case 57600:
return SERIAL_57600BPS;
case 115200:
return SERIAL_115200BPS;
default:
return 0;
}
}
#ifdef SERIAL_TEST
int main() {
serial_t * st;
int a = 0, i = 0, hd;
st = serial_init("/dev/ttyS0", SERIAL_9600BPS,
SERIAL_BITS_8 | SERIAL_PAR_NONE |
SERIAL_STOP_1 | SERIAL_FLOW_NONE |
SERIAL_AUTOFLUSH);
assure(st != NULL);
serial_puts(st, "\r\nStarting\r\n", 12);
while (i < 10) {
printf("loop %i\n",i++);
hd = serial_has_data (st, 2, 0);
if (hd == 1) {
a = serial_getc (st);
if (a != -1) {
printf("read 0x%02x",a);
if ((a>31) && (a<128)) {
printf(" '%c'",a);
}
printf("\n");
}
/* if char is uppercase, return the respective lowercase */
/* else, return a dot (.) */
if (( a >= 'A') && (a <= 'Z')) {
serial_putc(st, a+0x20);
} else {
serial_putc(st, '.');
}
}
}
serial_puts(st, "\r\nDone\r\n", 8);
serial_destroy(st);
return 0;
}
#endif /* x */