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