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