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