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