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