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