##// END OF EJS Templates
Improved beaglebone support.
jeandet -
r36:566051021c9c alexis
parent child
Show More
@@ -1,516 +1,525
1 1 #include <stdio.h>
2 2 #include <unistd.h>
3 3 #include <fcntl.h>
4 4 #include <string.h>
5 5 #include <errno.h>
6 6 #include <malloc.h>
7 7 #include <dirent.h>
8 8 #include <limits.h>
9 9 #include <stdlib.h>
10 10 #include <sys/stat.h>
11 11 #include "rs232config.h"
12 12
13 13 #ifdef HAVE_TERMIOS_H
14 14 #include <termios.h>
15 15 #endif
16 16 #ifdef HAVE_TERMIO_H
17 17 #include <termio.h>
18 18 #endif
19 19 #include "RS232.h"
20 20
21 21 #ifdef HAVE_WINDOWS_H
22 22 #else
23 23 #ifdef HAVE_TERMIOS_H
24 24 rs232speed_t rs232cfspeed(unsigned int BaudeRate);
25 25
26 26 rs232port_t rs232open(char* psPortName)
27 27 {
28 rs232port_t fd;
29 fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
30 fcntl((int)fd, F_SETFL, 0);
31 //fd = open(psPortName, O_RDWR | O_NOCTTY);
32 #ifdef debug
33 if(fd==-1)printf("can't open Port\n");
34 #endif
35 return fd;
28 rs232port_t fd;
29 fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
30 fcntl((int)fd, F_SETFL, FNDELAY);
31 //fd = open(psPortName, O_RDWR | O_NOCTTY);
32 #ifdef debug
33 if(fd==-1)printf("can't open Port\n");
34 #endif
35 return fd;
36 36 }
37 37
38 38 int rs232close(rs232port_t fd)
39 39 {
40 40 if ((int)fd == -1)
41 41 {
42 42 return -1;
43 43 }
44 44 else
45 45 {
46 46 close(fd);
47 47 return 0;
48 48 }
49 49 }
50 50
51 51 rs232portslist_t* rs232getportlist()
52 52 {
53 53 struct dirent *dp;
54 54 char* path="/dev/serial/by-id";
55 55 DIR *dir = opendir(path);
56 56 rs232portslist_t* firstitem=NULL;
57 57 rs232portslist_t* previtem=NULL;
58 58 char linkname[1024];
59 59 int i=0;
60 60 if(dir!=NULL)
61 61 {
62 62 while ((dp=readdir(dir)) != NULL)
63 63 {
64 64 char* name = (char*)malloc(1024);
65 65 for(i=0;i<1024;i++)
66 66 {
67 67 name[i]='\0';
68 68 linkname[i]='\0';
69 69 }
70 70 struct stat statbuf;
71 71 strcpy(linkname,path);
72 72 strcat(linkname,"/");
73 73 strcat(linkname,dp->d_name);
74 74 lstat(linkname, &statbuf);
75 75 if(S_ISLNK(statbuf.st_mode))
76 76 {
77 77 if(-1!=readlink(linkname,name,1024))
78 78 {
79 79 for(i=0;i<1024;i++)
80 80 {
81 81 linkname[i]='\0';
82 82 }
83 83 strcpy(linkname,path);
84 84 strcat(linkname,"/");
85 85 strcat(linkname,name);
86 86 for(i=0;i<1024;i++)
87 87 {
88 88 name[i]='\0';
89 89 }
90 90 if(NULL!=realpath(linkname, name))
91 91 {
92 92 rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t));
93 93 item->name = name;
94 94 item->next = NULL;
95 95 if(NULL!=previtem)previtem->next = item;
96 96 previtem = item;
97 97 if(NULL==firstitem)firstitem = item;
98 98 }
99 99
100 100 }
101 101 }
102 102
103 103 }
104 104 }
105 105 return firstitem;
106 106 }
107 107
108 108 void rs232deleteportlist(rs232portslist_t* list)
109 109 {
110 110 if(list!=NULL)
111 111 {
112 112 if(list->next != NULL)
113 113 rs232deleteportlist(list->next);
114 114 free(list);
115 115 }
116 116 }
117 117
118 118 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
119 119 {
120 120 if ((int)fd == -1)
121 121 {
122 122 return -1;
123 123 }
124 124 else
125 125 {
126 126 struct termios terminos;
127 127 tcgetattr(fd, &terminos);
128 terminos.c_iflag=0;
129 terminos.c_oflag=0;
130 terminos.c_cflag=0;
131 terminos.c_lflag=0;
128 132 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
129 133 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
130 134 terminos.c_cflag |= (CLOCAL | CREAD);
131 135 rs232cfparity((int)fd, &terminos, Parity);
132 136 rs232cfnbstop((int)fd, &terminos, NbStop);
133 137 rs232cfcsize((int)fd, &terminos, ChSize);
134 138 terminos.c_cc[VMIN]=0;
135 139 terminos.c_cc[VTIME]=1;
136 140 tcflush(fd, TCIFLUSH);
137 141 #ifdef debug
138 142 if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n");
139 143 #else
140 144 tcsetattr(fd, TCSANOW, &terminos);
141 145 #endif
142 146 return 0;
143 147 }
144 148 }
145 149
146 150 int rs232setbaudrate(rs232port_t fd, int baudrate)
147 151 {
148 152 if ((int)fd == -1)
149 153 {
150 154 return fd;
151 155 }
152 156 else
153 157 {
154 158 struct termios terminos;
155 159 tcgetattr((int)fd, &terminos);
156 160 cfsetispeed(&terminos, rs232cfspeed(baudrate));
157 161 cfsetospeed(&terminos, rs232cfspeed(baudrate));
158 162 tcsetattr((int)fd, TCSANOW, &terminos);
159 163 return 0;
160 164 }
161 165 }
162 166
163 167 int rs232setparity(rs232port_t fd, rs232parity Parity)
164 168 {
165 169 if ((int)fd == -1)
166 170 {
167 171 return fd;
168 172 }
169 173 else
170 174 {
171 175 struct termios terminos;
172 176 tcgetattr((int)fd, &terminos);
173 177 terminos.c_cflag &= ~PARENB;
174 178 terminos.c_cflag &= ~PARODD;
175 179 switch(Parity)
176 180 {
177 181 case rs232parityNo:
178 182 terminos.c_cflag &= ~PARENB;
179 183 break;
180 184 case rs232parityOdd:
181 185 terminos.c_cflag |= PARENB;
182 186 terminos.c_cflag |= PARODD;
183 187 break;
184 188 case rs232parityEven:
185 189 terminos.c_cflag |= PARENB;
186 190 terminos.c_cflag &= ~PARODD;
187 191 break;
188 192 default:
189 193 terminos.c_cflag &= ~PARENB;
190 194 break;
191 195 }
192 196 tcsetattr((int)fd, TCSANOW, &terminos);
193 197 return 0;
194 198 }
195 199 }
196 200
197 201 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
198 202 {
199 203 if ((int)fd == -1)
200 204 {
201 205 return fd;
202 206 }
203 207 else
204 208 {
205 209 struct termios terminos;
206 210 tcgetattr((int)fd, &terminos);
207 211 switch(NbStop)
208 212 {
209 213 case rs232OneStop:
210 214 terminos.c_cflag &= ~CSTOPB;
211 215 break;
212 216 case rs232One5Stop:
213 217 terminos.c_cflag |= CSTOPB;
214 218 break;
215 219 case rs232TwoStop:
216 220 terminos.c_cflag |= CSTOPB;
217 221 break;
218 222 default:
219 223 terminos.c_cflag &= ~CSTOPB;
220 224 break;
221 225 }
222 226 tcsetattr((int)fd, TCSANOW, &terminos);
223 227 return 0;
224 228 }
225 229 }
226 230
227 231
228 232 int rs232setcsize(rs232port_t fd, int ChSize)
229 233 {
230 234 if ((int)fd == -1)
231 235 {
232 236 return fd;
233 237 }
234 238 else
235 239 {
236 240 struct termios terminos;
237 241 tcgetattr((int)fd, &terminos);
238 242 terminos.c_cflag &= ~CSIZE;
239 243 switch(ChSize)
240 244 {
241 245 case 5:
242 246 terminos.c_cflag |= CS5;
243 247 break;
244 248 case 6:
245 249 terminos.c_cflag |= CS6;
246 250 break;
247 251 case 7:
248 252 terminos.c_cflag |= CS7;
249 253 break;
250 254 default:
251 255 terminos.c_cflag |= CS8;
252 256 break;
253 257 }
254 258 tcsetattr((int)fd, TCSANOW, &terminos);
255 259 return 0;
256 260 }
257 261 }
258 262
259 263 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
260 264 {
261 265 if(BaudeRate<25)
262 266 return B0;
263 267
264 268 if(BaudeRate<67)
265 269 return B50;
266 270
267 271 if(BaudeRate<93)
268 272 return B75;
269 273
270 274 if(BaudeRate<123)
271 275 return B110;
272 276
273 277 if(BaudeRate<142)
274 278 return B134;
275 279
276 280 if(BaudeRate<175)
277 281 return B150;
278 282
279 283 if(BaudeRate<250)
280 284 return B200;
281 285
282 286 if(BaudeRate<450)
283 287 return B300;
284 288
285 289 if(BaudeRate<900)
286 290 return B600;
287 291
288 292 if(BaudeRate<1500)
289 293 return B1200;
290 294
291 295 if(BaudeRate<2100)
292 296 return B1800;
293 297
294 298 if(BaudeRate<3600)
295 299 return B2400;
296 300
297 301 if(BaudeRate<7200)
298 302 return B4800;
299 303
300 304 if(BaudeRate<1400)
301 305 return B9600;
302 306
303 307 if(BaudeRate<28800)
304 308 return B19200;
305 309
306 310 if(BaudeRate<48000)
307 311 return B38400;
308 312
309 313 if(BaudeRate<86400)
310 314 return B57600;
311 315
312 316 if(BaudeRate<172800)
313 317 return B115200;
314 318 else
315 319 return B230400;
316 320 }
317 321
318 322
319 323 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
320 324 {
321 325 if ((int)fd == -1)
322 326 {
323 327 return fd;
324 328 }
325 329 else
326 330 {
327 331 terminos->c_cflag &= ~PARENB;
328 332 terminos->c_cflag &= ~PARODD;
329 333 switch(Parity)
330 334 {
331 335 case rs232parityNo:
332 336 terminos->c_cflag &= ~PARENB;
333 337 terminos->c_cflag &= ~PARODD;
334 338 break;
335 339 case rs232parityOdd:
336 340 terminos->c_cflag |= PARENB;
337 341 terminos->c_cflag |= PARODD;
338 342 break;
339 343 case rs232parityEven:
340 344 terminos->c_cflag |= PARENB;
341 345 terminos->c_cflag &= ~PARODD;
342 346 break;
343 347 default:
344 348 terminos->c_cflag &= ~PARENB;
345 349 terminos->c_cflag &= ~PARODD;
346 350 break;
347 351 }
348 352 return 0;
349 353 }
350 354 }
351 355
352 356 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
353 357 {
354 358 if ((int)fd == -1)
355 359 {
356 360 return fd;
357 361 }
358 362 else
359 363 {
360 364 switch(NbStop)
361 365 {
362 366 case 2:
363 367 terminos->c_cflag |= CSTOPB;
364 368 break;
365 369 default:
366 370 terminos->c_cflag &= ~CSTOPB;
367 371 break;
368 372 }
369 373 return 0;
370 374 }
371 375 }
372 376
373 377
374 378 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
375 379 {
376 380 if ((int)fd == -1)
377 381 {
378 382 return fd;
379 383 }
380 384 else
381 385 {
382 386 terminos->c_cflag &= ~CSIZE;
383 387 switch(ChSize)
384 388 {
385 389 case 5:
386 390 terminos->c_cflag |= CS5;
387 391 break;
388 392 case 6:
389 393 terminos->c_cflag |= CS6;
390 394 break;
391 395 case 7:
392 396 terminos->c_cflag |= CS7;
393 397 break;
394 398 default:
395 399 terminos->c_cflag |= CS8;
396 400 break;
397 401 }
398 402 return 0;
399 403 }
400 404 }
401 405
402 406
403 407 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
404 408 {
405 409 if ((int)fd == -1)
406 410 {
407 411 return -1;
408 412 }
409 413 else
410 414 {
411 415 return write((int)fd, psWrite, WriteBufferSize);
412 416 }
413 417 }
414 418
415 419
416 420 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
417 421 {
418 422
419 423 if ((int)fd == -1)
420 424 {
421 425 return -1;
422 426 }
423 427 else
424 428 {
425 429 return read((int)fd, psReadHex, ReadBufferSize);
426 430 }
427 431
428 432 }
429 433
430 434
431 435 int rs232setRTS(rs232port_t fd)
432 436 {
433 437 int status;
434 438 ioctl((int)fd, TIOCMGET, &status);
435 439 status &= ~TIOCM_RTS;
436 440 if (ioctl((int)fd, TIOCMSET, &status))
437 441 {
438 442 return -1;
439 443 }
440 444 return 0;
441 445 }
442 446
443 447 int rs232clearRTS(rs232port_t fd)
444 448 {
445 449 int status;
446 450 ioctl((int)fd, TIOCMGET, &status);
447 451 status |= TIOCM_RTS;
448 452 if (ioctl((int)fd, TIOCMSET, &status))
449 453 {
450 454 return -1;
451 455 }
452 456 return 0;
453 457 }
454 458
455 459 int rs232setDTR(rs232port_t fd)
456 460 {
457 461 int status;
458 462 ioctl((int)fd, TIOCMGET, &status);
459 463 status &= ~TIOCM_DTR;
460 464 if (ioctl((int)fd, TIOCMSET, &status))
461 465 {
462 466 return -1;
463 467 }
464 468 return 0;
465 469 }
466 470
467 471
468 472 int rs232clearDTR(rs232port_t fd)
469 473 {
470 474 int status;
471 475 ioctl((int)fd, TIOCMGET, &status);
472 476 status |= TIOCM_DTR;
473 477 if (ioctl((int)fd, TIOCMSET, &status))
474 478 {
475 479 return -1;
476 480 }
477 481 return 0;
478 482 }
479 483
480 484
481 485
482 486 int rs232saferead(rs232port_t fd,char* data,int count )
483 487 {
484 488 int read=0;
485 489 int i=0;
486 for(i=0;i<100;i++)
490 for(i=0;i<1000;i++)
487 491 {
488 492 read = rs232read((int)fd,data,count);
489 count -=read;
493 //printf("read %d bytes\n",read);
494 if(read==-1)read=0;
495 count-=read;
490 496 data+=read;
491 497 if(count==0)
492 498 return 0;
499 usleep(10);
493 500 }
494 501 return -1;
495 502 }
496 503
497 504
498 505
499 506 int rs232safewrite(rs232port_t fd,char* data,int count)
500 507 {
501 508 int written=0;
502 509 int i=0;
503 510 for(i=0;i<1000;i++)
504 511 {
505 512 written = rs232write((int)fd,data+written,count);
513 //printf("%d bytes written\n",written);
514 if(written==-1)written=0;
506 515 count-=written;
507 516 data+=written;
508 517 if(count==0)
509 518 return 0;
510 519 }
511 520 return -1;
512 521 }
513 522
514 523 #endif
515 524 #endif //#ifdef HAVE_TERMIOS_H
516 525
General Comments 0
You need to be logged in to leave comments. Login now