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