##// END OF EJS Templates
Satrted Port enumeration feature.
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL -
r33:36f4e246ea65 alexis
parent child
Show More
@@ -1,38 +1,52
1 1 // SOPSUYSI_RS232.h
2 2 #ifndef RS232_H
3 3 #define RS232_H
4 4 #define debug
5 5
6 6 #define badPortValue -1
7 7 #define rs232noerr 0
8 8 typedef int rs232port_t;
9 9 typedef int rs232speed_t;
10 10 typedef enum {rs232OneStop,rs232One5Stop,rs232TwoStop}rs232stop;
11 11 typedef enum {rs232parityNo,rs232parityOdd,rs232parityEven}rs232parity;
12 12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 typedef struct rs232portslist_t
17 {
18 char* name;
19 struct rs232portslist_t* next;
20 }rs232portslist_t;
21 #ifdef __cplusplus
22 }
23 #endif
24
13 25
14 26 #ifdef __cplusplus
15 27 #define rs232extern extern "C"
16 28 #else
17 29 #define rs232extern extern
18 30 #endif
19 31 rs232extern rs232port_t rs232open(char* psPortName);
32 rs232extern rs232portslist_t* rs232getportlist();
33 rs232extern void rs232deleteportlist(rs232portslist_t* list);
20 34 rs232extern int rs232close(rs232port_t fd);
21 35 rs232extern int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop);
22 36 rs232extern int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize);
23 37 rs232extern int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize);
24 38 rs232extern int rs232setparity(rs232port_t fd, rs232parity Parity);
25 39 rs232extern int rs232setnbstop(rs232port_t fd, rs232stop NbStop);
26 40 rs232extern int rs232setcsize(rs232port_t fd, int ChSize);
27 41 rs232extern int rs232setbaudrate(rs232port_t fd, int baudrate);
28 42 rs232extern int rs232setRTS(rs232port_t fd);
29 43 rs232extern int rs232clearRTS(rs232port_t fd);
30 44 rs232extern int rs232setDTR(rs232port_t fd);
31 45 rs232extern int rs232clearDTR(rs232port_t fd);
32 46 rs232extern int rs232saferead(rs232port_t fd,char* data,int count );
33 47 rs232extern int rs232safewrite(rs232port_t fd,char* data,int count);
34 48
35 49 #endif
36 50
37 51
38 52
@@ -1,444 +1,455
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 #include <malloc.h>
6 7 #include "rs232config.h"
7 8 #ifdef HAVE_TERMIOS_H
8 9 #include <termios.h>
9 10 #endif
10 11 #ifdef HAVE_TERMIO_H
11 12 #include <termio.h>
12 13 #endif
13 14 #include "RS232.h"
14 15
15 16 #ifdef HAVE_WINDOWS_H
16 17 #else
17 18 #ifdef HAVE_TERMIOS_H
18 19 rs232speed_t rs232cfspeed(unsigned int BaudeRate);
19 20
20 21 rs232port_t rs232open(char* psPortName)
21 22 {
22 23 rs232port_t fd;
23 24 fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
24 25 fcntl((int)fd, F_SETFL, 0);
25 26 //fd = open(psPortName, O_RDWR | O_NOCTTY);
26 27 #ifdef debug
27 28 if(fd==-1)printf("can't open Port\n");
28 29 #endif
29 30 return fd;
30 31 }
31 32
32 33 int rs232close(rs232port_t fd)
33 34 {
34 35 if ((int)fd == -1)
35 36 {
36 37 return -1;
37 38 }
38 39 else
39 40 {
40 41 close(fd);
41 42 return 0;
42 43 }
43 44 }
44 45
46 rs232portslist_t* rs232getportlist()
47 {
48
49
50 }
51
52 void rs232deleteportlist(rs232portslist_t* list)
53 {
54
55 }
45 56
46 57 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
47 58 {
48 59 if ((int)fd == -1)
49 60 {
50 61 return -1;
51 62 }
52 63 else
53 64 {
54 65 struct termios terminos;
55 66 tcgetattr(fd, &terminos);
56 67 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
57 68 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
58 69 terminos.c_cflag |= (CLOCAL | CREAD);
59 70 rs232cfparity((int)fd, &terminos, Parity);
60 71 rs232cfnbstop((int)fd, &terminos, NbStop);
61 72 rs232cfcsize((int)fd, &terminos, ChSize);
62 73 terminos.c_cc[VMIN]=0;
63 74 terminos.c_cc[VTIME]=1;
64 75 tcflush(fd, TCIFLUSH);
65 76 #ifdef debug
66 77 if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n");
67 78 #else
68 79 tcsetattr(fd, TCSANOW, &terminos);
69 80 #endif
70 81 return 0;
71 82 }
72 83 }
73 84
74 85 int rs232setbaudrate(rs232port_t fd, int baudrate)
75 86 {
76 87 if ((int)fd == -1)
77 88 {
78 89 return fd;
79 90 }
80 91 else
81 92 {
82 93 struct termios terminos;
83 94 tcgetattr((int)fd, &terminos);
84 95 cfsetispeed(&terminos, rs232cfspeed(baudrate));
85 96 cfsetospeed(&terminos, rs232cfspeed(baudrate));
86 97 tcsetattr((int)fd, TCSANOW, &terminos);
87 98 return 0;
88 99 }
89 100 }
90 101
91 102 int rs232setparity(rs232port_t fd, rs232parity Parity)
92 103 {
93 104 if ((int)fd == -1)
94 105 {
95 106 return fd;
96 107 }
97 108 else
98 109 {
99 110 struct termios terminos;
100 111 tcgetattr((int)fd, &terminos);
101 112 terminos.c_cflag &= ~PARENB;
102 113 terminos.c_cflag &= ~PARODD;
103 114 switch(Parity)
104 115 {
105 116 case rs232parityNo:
106 117 terminos.c_cflag &= ~PARENB;
107 118 break;
108 119 case rs232parityOdd:
109 120 terminos.c_cflag |= PARENB;
110 121 terminos.c_cflag |= PARODD;
111 122 break;
112 123 case rs232parityEven:
113 124 terminos.c_cflag |= PARENB;
114 125 terminos.c_cflag &= ~PARODD;
115 126 break;
116 127 default:
117 128 terminos.c_cflag &= ~PARENB;
118 129 break;
119 130 }
120 131 tcsetattr((int)fd, TCSANOW, &terminos);
121 132 return 0;
122 133 }
123 134 }
124 135
125 136 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
126 137 {
127 138 if ((int)fd == -1)
128 139 {
129 140 return fd;
130 141 }
131 142 else
132 143 {
133 144 struct termios terminos;
134 145 tcgetattr((int)fd, &terminos);
135 146 switch(NbStop)
136 147 {
137 148 case rs232OneStop:
138 149 terminos.c_cflag &= ~CSTOPB;
139 150 break;
140 151 case rs232One5Stop:
141 152 terminos.c_cflag |= CSTOPB;
142 153 break;
143 154 case rs232TwoStop:
144 155 terminos.c_cflag |= CSTOPB;
145 156 break;
146 157 default:
147 158 terminos.c_cflag &= ~CSTOPB;
148 159 break;
149 160 }
150 161 tcsetattr((int)fd, TCSANOW, &terminos);
151 162 return 0;
152 163 }
153 164 }
154 165
155 166
156 167 int rs232setcsize(rs232port_t fd, int ChSize)
157 168 {
158 169 if ((int)fd == -1)
159 170 {
160 171 return fd;
161 172 }
162 173 else
163 174 {
164 175 struct termios terminos;
165 176 tcgetattr((int)fd, &terminos);
166 177 terminos.c_cflag &= ~CSIZE;
167 178 switch(ChSize)
168 179 {
169 180 case 5:
170 181 terminos.c_cflag |= CS5;
171 182 break;
172 183 case 6:
173 184 terminos.c_cflag |= CS6;
174 185 break;
175 186 case 7:
176 187 terminos.c_cflag |= CS7;
177 188 break;
178 189 default:
179 190 terminos.c_cflag |= CS8;
180 191 break;
181 192 }
182 193 tcsetattr((int)fd, TCSANOW, &terminos);
183 194 return 0;
184 195 }
185 196 }
186 197
187 198 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
188 199 {
189 200 if(BaudeRate<25)
190 201 return B0;
191 202
192 203 if(BaudeRate<67)
193 204 return B50;
194 205
195 206 if(BaudeRate<93)
196 207 return B75;
197 208
198 209 if(BaudeRate<123)
199 210 return B110;
200 211
201 212 if(BaudeRate<142)
202 213 return B134;
203 214
204 215 if(BaudeRate<175)
205 216 return B150;
206 217
207 218 if(BaudeRate<250)
208 219 return B200;
209 220
210 221 if(BaudeRate<450)
211 222 return B300;
212 223
213 224 if(BaudeRate<900)
214 225 return B600;
215 226
216 227 if(BaudeRate<1500)
217 228 return B1200;
218 229
219 230 if(BaudeRate<2100)
220 231 return B1800;
221 232
222 233 if(BaudeRate<3600)
223 234 return B2400;
224 235
225 236 if(BaudeRate<7200)
226 237 return B4800;
227 238
228 239 if(BaudeRate<1400)
229 240 return B9600;
230 241
231 242 if(BaudeRate<28800)
232 243 return B19200;
233 244
234 245 if(BaudeRate<48000)
235 246 return B38400;
236 247
237 248 if(BaudeRate<86400)
238 249 return B57600;
239 250
240 251 if(BaudeRate<172800)
241 252 return B115200;
242 253 else
243 254 return B230400;
244 255 }
245 256
246 257
247 258 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
248 259 {
249 260 if ((int)fd == -1)
250 261 {
251 262 return fd;
252 263 }
253 264 else
254 265 {
255 266 terminos->c_cflag &= ~PARENB;
256 267 terminos->c_cflag &= ~PARODD;
257 268 switch(Parity)
258 269 {
259 270 case rs232parityNo:
260 271 terminos->c_cflag &= ~PARENB;
261 272 terminos->c_cflag &= ~PARODD;
262 273 break;
263 274 case rs232parityOdd:
264 275 terminos->c_cflag |= PARENB;
265 276 terminos->c_cflag |= PARODD;
266 277 break;
267 278 case rs232parityEven:
268 279 terminos->c_cflag |= PARENB;
269 280 terminos->c_cflag &= ~PARODD;
270 281 break;
271 282 default:
272 283 terminos->c_cflag &= ~PARENB;
273 284 terminos->c_cflag &= ~PARODD;
274 285 break;
275 286 }
276 287 return 0;
277 288 }
278 289 }
279 290
280 291 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
281 292 {
282 293 if ((int)fd == -1)
283 294 {
284 295 return fd;
285 296 }
286 297 else
287 298 {
288 299 switch(NbStop)
289 300 {
290 301 case 2:
291 302 terminos->c_cflag |= CSTOPB;
292 303 break;
293 304 default:
294 305 terminos->c_cflag &= ~CSTOPB;
295 306 break;
296 307 }
297 308 return 0;
298 309 }
299 310 }
300 311
301 312
302 313 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
303 314 {
304 315 if ((int)fd == -1)
305 316 {
306 317 return fd;
307 318 }
308 319 else
309 320 {
310 321 terminos->c_cflag &= ~CSIZE;
311 322 switch(ChSize)
312 323 {
313 324 case 5:
314 325 terminos->c_cflag |= CS5;
315 326 break;
316 327 case 6:
317 328 terminos->c_cflag |= CS6;
318 329 break;
319 330 case 7:
320 331 terminos->c_cflag |= CS7;
321 332 break;
322 333 default:
323 334 terminos->c_cflag |= CS8;
324 335 break;
325 336 }
326 337 return 0;
327 338 }
328 339 }
329 340
330 341
331 342 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
332 343 {
333 344 if ((int)fd == -1)
334 345 {
335 346 return -1;
336 347 }
337 348 else
338 349 {
339 350 return write((int)fd, psWrite, WriteBufferSize);
340 351 }
341 352 }
342 353
343 354
344 355 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
345 356 {
346 357
347 358 if ((int)fd == -1)
348 359 {
349 360 return -1;
350 361 }
351 362 else
352 363 {
353 364 return read((int)fd, psReadHex, ReadBufferSize);
354 365 }
355 366
356 367 }
357 368
358 369
359 370 int rs232setRTS(rs232port_t fd)
360 371 {
361 372 int status;
362 373 ioctl((int)fd, TIOCMGET, &status);
363 374 status &= ~TIOCM_RTS;
364 375 if (ioctl((int)fd, TIOCMSET, &status))
365 376 {
366 377 return -1;
367 378 }
368 379 return 0;
369 380 }
370 381
371 382 int rs232clearRTS(rs232port_t fd)
372 383 {
373 384 int status;
374 385 ioctl((int)fd, TIOCMGET, &status);
375 386 status |= TIOCM_RTS;
376 387 if (ioctl((int)fd, TIOCMSET, &status))
377 388 {
378 389 return -1;
379 390 }
380 391 return 0;
381 392 }
382 393
383 394 int rs232setDTR(rs232port_t fd)
384 395 {
385 396 int status;
386 397 ioctl((int)fd, TIOCMGET, &status);
387 398 status &= ~TIOCM_DTR;
388 399 if (ioctl((int)fd, TIOCMSET, &status))
389 400 {
390 401 return -1;
391 402 }
392 403 return 0;
393 404 }
394 405
395 406
396 407 int rs232clearDTR(rs232port_t fd)
397 408 {
398 409 int status;
399 410 ioctl((int)fd, TIOCMGET, &status);
400 411 status |= TIOCM_DTR;
401 412 if (ioctl((int)fd, TIOCMSET, &status))
402 413 {
403 414 return -1;
404 415 }
405 416 return 0;
406 417 }
407 418
408 419
409 420
410 421 int rs232saferead(rs232port_t fd,char* data,int count )
411 422 {
412 423 int read=0;
413 424 int i=0;
414 425 for(i=0;i<100;i++)
415 426 {
416 427 read = rs232read((int)fd,data,count);
417 428 count -=read;
418 429 data+=read;
419 430 if(count==0)
420 431 return 0;
421 432 }
422 433 return -1;
423 434 }
424 435
425 436
426 437
427 438 int rs232safewrite(rs232port_t fd,char* data,int count)
428 439 {
429 440 int written=0;
430 441 int i=0;
431 442 for(i=0;i<1000;i++)
432 443 {
433 444 written = rs232write((int)fd,data+written,count);
434 445 count-=written;
435 446 data+=written;
436 447 if(count==0)
437 448 return 0;
438 449 }
439 450 return -1;
440 451 }
441 452
442 453 #endif
443 454 #endif //#ifdef HAVE_TERMIOS_H
444 455
@@ -1,322 +1,334
1 1 #include "rs232config.h"
2 2 #include <stdio.h>
3 3 #include <unistd.h>
4 4 #include <fcntl.h>
5 5 #include <string.h>
6 6 #include <errno.h>
7 #include <malloc.h>
7 8
8 9 #ifdef HAVE_WINDOWS_H
9 10 #include <windows.h>
10 11 #endif
11 12 #include "RS232.h"
12 13
13 14 #ifdef HAVE_WINDOWS_H
14 15
15 16
16 17 int privatedecodeparity(rs232parity Parity )
17 18 {
18 19 switch(Parity)
19 20 {
20 21 case rs232parityNo:
21 22 return NOPARITY;
22 23 break;
23 24 case rs232parityOdd:
24 25 return ODDPARITY;
25 26 break;
26 27 case rs232parityEven:
27 28 return EVENPARITY;
28 29 break;
29 30 default:
30 31 return NOPARITY;
31 32 break;
32 33 }
33 34 }
34 35
35 36 int privatedecodestop(rs232stop NbStop)
36 37 {
37 38 switch(NbStop)
38 39 {
39 40 case rs232OneStop:
40 41 return ONESTOPBIT;
41 42 break;
42 43 case rs232One5Stop:
43 44 return ONE5STOPBITS;
44 45 break;
45 46 case rs232TwoStop:
46 47 return TWOSTOPBITS;
47 48 break;
48 49 default:
49 50 return ONESTOPBIT;
50 51 break;
51 52 }
52 53 }
53 54
54 55
55 56
56 57 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
57 58 {
58 59
59 60 if(BaudeRate<123)
60 61 return (rs232speed_t)CBR_110;
61 62
62 63 if(BaudeRate<450)
63 64 return (rs232speed_t)CBR_300;
64 65
65 66 if(BaudeRate<900)
66 67 return (rs232speed_t)CBR_600;
67 68
68 69 if(BaudeRate<1500)
69 70 return (rs232speed_t)CBR_1200;
70 71
71 72 if(BaudeRate<3600)
72 73 return (rs232speed_t)CBR_2400;
73 74
74 75 if(BaudeRate<7200)
75 76 return (rs232speed_t)CBR_4800;
76 77
77 78 if(BaudeRate<14000)
78 79 return (rs232speed_t)CBR_9600;
79 80
80 81 if(BaudeRate<16800)
81 82 return (rs232speed_t)CBR_14400;
82 83
83 84 if(BaudeRate<28800)
84 85 return (rs232speed_t)CBR_19200;
85 86
86 87 if(BaudeRate<48000)
87 88 return (rs232speed_t)CBR_38400;
88 89
89 90 if(BaudeRate<86400)
90 91 return (rs232speed_t)CBR_57600;
91 92
92 93 if(BaudeRate<172800)
93 94 return (rs232speed_t)CBR_115200;
94 95 else
95 96 return (rs232speed_t)CBR_256000;
96 97 }
97 98
98 99
99 100 rs232port_t rs232open(char* psPortName)
100 101 {
101 102 rs232port_t fd;
102 103 fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
103 104 #ifdef debug
104 105 if(fd==(rs232port_t)INVALID_HANDLE_VALUE)
105 106 {
106 107 printf("can't open Port\n");
107 108 return (rs232port_t)badPortValue;
108 109 }
109 110 #endif
110 111 return fd;
111 112 }
112 113
113 114 int rs232close(rs232port_t fd)
114 115 {
115 116 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
116 117 {
117 118 return (rs232port_t)badPortValue;
118 119 }
119 120 else
120 121 {
121 122 CloseHandle((HANDLE)fd);
122 123 return rs232noerr;
123 124 }
124 125 }
125 126
127 rs232portslist_t* rs232getportlist()
128 {
129
130 }
131
132 void rs232deleteportlist(rs232portslist_t* list)
133 {
134
135 }
136
137
126 138 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
127 139 {
128 140 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
129 141 {
130 142 return (rs232port_t)badPortValue;
131 143 }
132 144 else
133 145 {
134 146 DCB dcbSerialParams = {0};
135 147 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
136 148 GetCommState((HANDLE)fd, &dcbSerialParams);
137 149 dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
138 150 dcbSerialParams.ByteSize=ChSize;
139 151 dcbSerialParams.StopBits=privatedecodestop(NbStop);
140 152 dcbSerialParams.Parity=privatedecodeparity(Parity);
141 153 SetCommState((HANDLE)fd, &dcbSerialParams);
142 154 COMMTIMEOUTS timeouts={0};
143 155 timeouts.ReadIntervalTimeout=100;
144 156 timeouts.ReadTotalTimeoutConstant=100;
145 157 timeouts.ReadTotalTimeoutMultiplier=1;
146 158 timeouts.WriteTotalTimeoutConstant=100;
147 159 timeouts.WriteTotalTimeoutMultiplier=10;
148 160 SetCommTimeouts((HANDLE)fd, &timeouts);
149 161 return rs232noerr;
150 162 }
151 163 }
152 164
153 165 int rs232setbaudrate(rs232port_t fd, int baudrate)
154 166 {
155 167 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
156 168 {
157 169 return (rs232port_t)badPortValue;
158 170 }
159 171 else
160 172 {
161 173 DCB dcbSerialParams = {0};
162 174 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
163 175 GetCommState((HANDLE)fd, &dcbSerialParams);
164 176 dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
165 177 SetCommState((HANDLE)fd, &dcbSerialParams);
166 178 return rs232noerr;
167 179 }
168 180 }
169 181
170 182 int rs232setparity(rs232port_t fd, rs232parity Parity)
171 183 {
172 184 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
173 185 {
174 186 return (rs232port_t)badPortValue;
175 187 }
176 188 else
177 189 {
178 190 DCB dcbSerialParams = {0};
179 191 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
180 192 GetCommState((HANDLE)fd, &dcbSerialParams);
181 193 dcbSerialParams.Parity = privatedecodeparity(Parity);
182 194 SetCommState((HANDLE)fd, &dcbSerialParams);
183 195 return rs232noerr;
184 196 }
185 197 }
186 198
187 199 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
188 200 {
189 201 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
190 202 {
191 203 return (rs232port_t)badPortValue;
192 204 }
193 205 else
194 206 {
195 207 DCB dcbSerialParams = {0};
196 208 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
197 209 GetCommState((HANDLE)fd, &dcbSerialParams);
198 210 dcbSerialParams.StopBits = privatedecodestop(NbStop);
199 211 SetCommState((HANDLE)fd, &dcbSerialParams);
200 212 return rs232noerr;
201 213 }
202 214 }
203 215
204 216
205 217 int rs232setcsize(rs232port_t fd, int ChSize)
206 218 {
207 219 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
208 220 {
209 221 return (rs232port_t)badPortValue;
210 222 }
211 223 else
212 224 {
213 225 DCB dcbSerialParams = {0};
214 226 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
215 227 GetCommState((HANDLE)fd, &dcbSerialParams);
216 228 dcbSerialParams.ByteSize = ChSize;
217 229 SetCommState((HANDLE)fd, &dcbSerialParams);
218 230 return rs232noerr;
219 231 }
220 232 }
221 233
222 234
223 235
224 236
225 237
226 238
227 239 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
228 240 {
229 241
230 242 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
231 243 {
232 244 return (rs232port_t)badPortValue;
233 245 }
234 246 else
235 247 {
236 248 DWORD dwBytesWriten = 0;
237 249 WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
238 250 return dwBytesWriten;
239 251 }
240 252 }
241 253
242 254
243 255 int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
244 256 {
245 257
246 258 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
247 259 {
248 260 return (rs232port_t)badPortValue;
249 261 }
250 262 else
251 263 {
252 264 DWORD dwBytesRead = 0;
253 265 ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
254 266 return dwBytesRead;
255 267 }
256 268
257 269 }
258 270
259 271
260 272 int rs232saferead(rs232port_t fd,char* data,int count )
261 273 {
262 274 int read=0;
263 275 int i=0;
264 276 for(i=0;i<100;i++)
265 277 {
266 278 read = rs232read(fd,data,count);
267 279 count -=read;
268 280 data+=read;
269 281 if(count==0)
270 282 return rs232noerr;
271 283 }
272 284 return -1;
273 285 }
274 286
275 287
276 288
277 289 int rs232safewrite(rs232port_t fd,char* data,int count)
278 290 {
279 291 int written=0;
280 292 int i=0;
281 293 for(i=0;i<1000;i++)
282 294 {
283 295 written = rs232write(fd,data+written,count);
284 296 count-=written;
285 297 data+=written;
286 298 if(count==0)
287 299 return rs232noerr;
288 300 }
289 301 return -1;
290 302 }
291 303
292 304
293 305 int rs232setRTS(rs232port_t fd)
294 306 {
295 307 if(EscapeCommFunction((HANDLE)fd, CLRRTS))
296 308 return rs232noerr;
297 309 return -1;
298 310 }
299 311
300 312 int rs232clearRTS(rs232port_t fd)
301 313 {
302 314 if(EscapeCommFunction((HANDLE)fd, SETRTS))
303 315 return rs232noerr;
304 316 return -1;
305 317 }
306 318
307 319 int rs232setDTR(rs232port_t fd)
308 320 {
309 321 if(EscapeCommFunction((HANDLE)fd, CLRDTR))
310 322 return rs232noerr;
311 323 return -1;
312 324 }
313 325
314 326
315 327 int rs232clearDTR(rs232port_t fd)
316 328 {
317 329 if(EscapeCommFunction((HANDLE)fd, SETDTR))
318 330 return rs232noerr;
319 331 return -1;
320 332 }
321 333
322 334 #endif //#ifdef HAVE_WINDOWS_H
General Comments 0
You need to be logged in to leave comments. Login now