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