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