##// END OF EJS Templates
Now totaly stable on linux read timeout = 100ms
jeandet -
r13:b220a9e23d54 alexis
parent child
Show More
1 NO CONTENT: modified file chmod 100755 => 100644
NO CONTENT: modified file chmod 100755 => 100644
@@ -1,343 +1,343
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 #include "RS232.h"
10 #include "RS232.h"
11
11
12 #ifdef HAVE_WINDOWS_H
12 #ifdef HAVE_WINDOWS_H
13 #else
13 #else
14 #ifdef HAVE_TERMIOS_H
14 #ifdef HAVE_TERMIOS_H
15 rs232speed_t rs232cfspeed(unsigned int BaudeRate);
15 rs232speed_t rs232cfspeed(unsigned int BaudeRate);
16
16
17 rs232port_t rs232open(char* psPortName)
17 rs232port_t rs232open(char* psPortName)
18 {
18 {
19 rs232port_t fd;
19 rs232port_t fd;
20 fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
20 fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
21 fcntl(fd, F_SETFL, 0);
21 fcntl(fd, F_SETFL, 0);
22 //fd = open(psPortName, O_RDWR | O_NOCTTY);
22 //fd = open(psPortName, O_RDWR | O_NOCTTY);
23 #ifdef debug
23 #ifdef debug
24 if(fd==-1)printf("can't open Port\n");
24 if(fd==-1)printf("can't open Port\n");
25 #endif
25 #endif
26 return fd;
26 return fd;
27 }
27 }
28
28
29 int rs232close(rs232port_t fd)
29 int rs232close(rs232port_t fd)
30 {
30 {
31 if ((int)fd == -1)
31 if ((int)fd == -1)
32 {
32 {
33 return -1;
33 return -1;
34 }
34 }
35 else
35 else
36 {
36 {
37 close(fd);
37 close(fd);
38 return 0;
38 return 0;
39 }
39 }
40 }
40 }
41
41
42
42
43 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
43 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
44 {
44 {
45 if ((int)fd == -1)
45 if ((int)fd == -1)
46 {
46 {
47 return -1;
47 return -1;
48 }
48 }
49 else
49 else
50 {
50 {
51 struct termios terminos;
51 struct termios terminos;
52 tcgetattr(fd, &terminos);
52 tcgetattr(fd, &terminos);
53 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
53 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
54 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
54 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
55 terminos.c_cflag |= (CLOCAL | CREAD);
55 terminos.c_cflag |= (CLOCAL | CREAD);
56 rs232cfparity(fd, &terminos, Parity);
56 rs232cfparity(fd, &terminos, Parity);
57 rs232cfnbstop(fd, &terminos, NbStop);
57 rs232cfnbstop(fd, &terminos, NbStop);
58 rs232cfcsize(fd, &terminos, ChSize);
58 rs232cfcsize(fd, &terminos, ChSize);
59 terminos.c_cc[VMIN]=0;
59 terminos.c_cc[VMIN]=0;
60 terminos.c_cc[VTIME]=20;
60 terminos.c_cc[VTIME]=1;
61 tcflush(fd, TCIFLUSH);
61 tcflush(fd, TCIFLUSH);
62 #ifdef debug
62 #ifdef debug
63 if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n");
63 if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n");
64 #else
64 #else
65 tcsetattr(fd, TCSANOW, &terminos);
65 tcsetattr(fd, TCSANOW, &terminos);
66 #endif
66 #endif
67 return 0;
67 return 0;
68 }
68 }
69 }
69 }
70
70
71 int rs232setbaudrate(rs232port_t fd, int baudrate)
71 int rs232setbaudrate(rs232port_t fd, int baudrate)
72 {
72 {
73 if ((int)fd == -1)
73 if ((int)fd == -1)
74 {
74 {
75 return fd;
75 return fd;
76 }
76 }
77 else
77 else
78 {
78 {
79 struct termios terminos;
79 struct termios terminos;
80 tcgetattr(fd, &terminos);
80 tcgetattr(fd, &terminos);
81 cfsetispeed(&terminos, rs232cfspeed(baudrate));
81 cfsetispeed(&terminos, rs232cfspeed(baudrate));
82 cfsetospeed(&terminos, rs232cfspeed(baudrate));
82 cfsetospeed(&terminos, rs232cfspeed(baudrate));
83 tcsetattr(fd, TCSANOW, &terminos);
83 tcsetattr(fd, TCSANOW, &terminos);
84 return 0;
84 return 0;
85 }
85 }
86 }
86 }
87
87
88 int rs232setparity(rs232port_t fd, rs232parity Parity)
88 int rs232setparity(rs232port_t fd, rs232parity Parity)
89 {
89 {
90 if ((int)fd == -1)
90 if ((int)fd == -1)
91 {
91 {
92 return fd;
92 return fd;
93 }
93 }
94 else
94 else
95 {
95 {
96 struct termios terminos;
96 struct termios terminos;
97 tcgetattr(fd, &terminos);
97 tcgetattr(fd, &terminos);
98 switch(Parity)
98 switch(Parity)
99 {
99 {
100 case rs232parityNo:
100 case rs232parityNo:
101 terminos.c_cflag &= Parity;
101 terminos.c_cflag &= Parity;
102 break;
102 break;
103 case rs232parityOdd:
103 case rs232parityOdd:
104 terminos.c_cflag &= ~PARENB;
104 terminos.c_cflag &= ~PARENB;
105 terminos.c_cflag |= Parity;
105 terminos.c_cflag |= Parity;
106 break;
106 break;
107 case rs232parityEven:
107 case rs232parityEven:
108 terminos.c_cflag &= ~PARENB;
108 terminos.c_cflag &= ~PARENB;
109 terminos.c_cflag |= Parity;
109 terminos.c_cflag |= Parity;
110 break;
110 break;
111 default:
111 default:
112 terminos.c_cflag &= ~PARENB;
112 terminos.c_cflag &= ~PARENB;
113 break;
113 break;
114 }
114 }
115 tcsetattr(fd, TCSANOW, &terminos);
115 tcsetattr(fd, TCSANOW, &terminos);
116 return 0;
116 return 0;
117 }
117 }
118 }
118 }
119
119
120 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
120 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
121 {
121 {
122 if ((int)fd == -1)
122 if ((int)fd == -1)
123 {
123 {
124 return fd;
124 return fd;
125 }
125 }
126 else
126 else
127 {
127 {
128 struct termios terminos;
128 struct termios terminos;
129 tcgetattr(fd, &terminos);
129 tcgetattr(fd, &terminos);
130 switch(NbStop)
130 switch(NbStop)
131 {
131 {
132 case 2:
132 case 2:
133 terminos.c_cflag |= CSTOPB;
133 terminos.c_cflag |= CSTOPB;
134 break;
134 break;
135 default:
135 default:
136 terminos.c_cflag &= ~CSTOPB;
136 terminos.c_cflag &= ~CSTOPB;
137 break;
137 break;
138 }
138 }
139 tcsetattr(fd, TCSANOW, &terminos);
139 tcsetattr(fd, TCSANOW, &terminos);
140 return 0;
140 return 0;
141 }
141 }
142 }
142 }
143
143
144
144
145 int rs232setcsize(rs232port_t fd, int ChSize)
145 int rs232setcsize(rs232port_t fd, int ChSize)
146 {
146 {
147 if ((int)fd == -1)
147 if ((int)fd == -1)
148 {
148 {
149 return fd;
149 return fd;
150 }
150 }
151 else
151 else
152 {
152 {
153 struct termios terminos;
153 struct termios terminos;
154 tcgetattr(fd, &terminos);
154 tcgetattr(fd, &terminos);
155 switch(ChSize)
155 switch(ChSize)
156 {
156 {
157 case 5:
157 case 5:
158 terminos.c_cflag |= CS5;
158 terminos.c_cflag |= CS5;
159 break;
159 break;
160 case 6:
160 case 6:
161 terminos.c_cflag |= CS6;
161 terminos.c_cflag |= CS6;
162 break;
162 break;
163 case 7:
163 case 7:
164 terminos.c_cflag |= CS7;
164 terminos.c_cflag |= CS7;
165 break;
165 break;
166 default:
166 default:
167 terminos.c_cflag |= CS8;
167 terminos.c_cflag |= CS8;
168 break;
168 break;
169 }
169 }
170 tcsetattr(fd, TCSANOW, &terminos);
170 tcsetattr(fd, TCSANOW, &terminos);
171 return 0;
171 return 0;
172 }
172 }
173 }
173 }
174
174
175 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
175 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
176 {
176 {
177 if(BaudeRate<25)
177 if(BaudeRate<25)
178 return B0;
178 return B0;
179
179
180 if(BaudeRate<67)
180 if(BaudeRate<67)
181 return B50;
181 return B50;
182
182
183 if(BaudeRate<93)
183 if(BaudeRate<93)
184 return B75;
184 return B75;
185
185
186 if(BaudeRate<123)
186 if(BaudeRate<123)
187 return B110;
187 return B110;
188
188
189 if(BaudeRate<142)
189 if(BaudeRate<142)
190 return B134;
190 return B134;
191
191
192 if(BaudeRate<175)
192 if(BaudeRate<175)
193 return B150;
193 return B150;
194
194
195 if(BaudeRate<250)
195 if(BaudeRate<250)
196 return B200;
196 return B200;
197
197
198 if(BaudeRate<450)
198 if(BaudeRate<450)
199 return B300;
199 return B300;
200
200
201 if(BaudeRate<900)
201 if(BaudeRate<900)
202 return B600;
202 return B600;
203
203
204 if(BaudeRate<1500)
204 if(BaudeRate<1500)
205 return B1200;
205 return B1200;
206
206
207 if(BaudeRate<2100)
207 if(BaudeRate<2100)
208 return B1800;
208 return B1800;
209
209
210 if(BaudeRate<3600)
210 if(BaudeRate<3600)
211 return B2400;
211 return B2400;
212
212
213 if(BaudeRate<7200)
213 if(BaudeRate<7200)
214 return B4800;
214 return B4800;
215
215
216 if(BaudeRate<1400)
216 if(BaudeRate<1400)
217 return B9600;
217 return B9600;
218
218
219 if(BaudeRate<28800)
219 if(BaudeRate<28800)
220 return B19200;
220 return B19200;
221
221
222 if(BaudeRate<48000)
222 if(BaudeRate<48000)
223 return B38400;
223 return B38400;
224
224
225 if(BaudeRate<86400)
225 if(BaudeRate<86400)
226 return B57600;
226 return B57600;
227
227
228 if(BaudeRate<172800)
228 if(BaudeRate<172800)
229 return B115200;
229 return B115200;
230 else
230 else
231 return B230400;
231 return B230400;
232 }
232 }
233
233
234
234
235 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
235 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
236 {
236 {
237 if ((int)fd == -1)
237 if ((int)fd == -1)
238 {
238 {
239 return fd;
239 return fd;
240 }
240 }
241 else
241 else
242 {
242 {
243 switch(Parity)
243 switch(Parity)
244 {
244 {
245 case rs232parityNo:
245 case rs232parityNo:
246 terminos->c_cflag &= Parity;
246 terminos->c_cflag &= Parity;
247 break;
247 break;
248 case rs232parityOdd:
248 case rs232parityOdd:
249 terminos->c_cflag &= ~PARENB;
249 terminos->c_cflag &= ~PARENB;
250 terminos->c_cflag |= Parity;
250 terminos->c_cflag |= Parity;
251 break;
251 break;
252 case rs232parityEven:
252 case rs232parityEven:
253 terminos->c_cflag &= ~PARENB;
253 terminos->c_cflag &= ~PARENB;
254 terminos->c_cflag |= Parity;
254 terminos->c_cflag |= Parity;
255 break;
255 break;
256 default:
256 default:
257 terminos->c_cflag &= ~PARENB;
257 terminos->c_cflag &= ~PARENB;
258 break;
258 break;
259 }
259 }
260 return 0;
260 return 0;
261 }
261 }
262 }
262 }
263
263
264 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
264 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
265 {
265 {
266 if ((int)fd == -1)
266 if ((int)fd == -1)
267 {
267 {
268 return fd;
268 return fd;
269 }
269 }
270 else
270 else
271 {
271 {
272 switch(NbStop)
272 switch(NbStop)
273 {
273 {
274 case 2:
274 case 2:
275 terminos->c_cflag |= CSTOPB;
275 terminos->c_cflag |= CSTOPB;
276 break;
276 break;
277 default:
277 default:
278 terminos->c_cflag &= ~CSTOPB;
278 terminos->c_cflag &= ~CSTOPB;
279 break;
279 break;
280 }
280 }
281 return 0;
281 return 0;
282 }
282 }
283 }
283 }
284
284
285
285
286 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
286 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
287 {
287 {
288 if ((int)fd == -1)
288 if ((int)fd == -1)
289 {
289 {
290 return fd;
290 return fd;
291 }
291 }
292 else
292 else
293 {
293 {
294 switch(ChSize)
294 switch(ChSize)
295 {
295 {
296 case 5:
296 case 5:
297 terminos->c_cflag |= CS5;
297 terminos->c_cflag |= CS5;
298 break;
298 break;
299 case 6:
299 case 6:
300 terminos->c_cflag |= CS6;
300 terminos->c_cflag |= CS6;
301 break;
301 break;
302 case 7:
302 case 7:
303 terminos->c_cflag |= CS7;
303 terminos->c_cflag |= CS7;
304 break;
304 break;
305 default:
305 default:
306 terminos->c_cflag |= CS8;
306 terminos->c_cflag |= CS8;
307 break;
307 break;
308 }
308 }
309 return 0;
309 return 0;
310 }
310 }
311 }
311 }
312
312
313
313
314 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
314 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
315 {
315 {
316 if ((int)fd == -1)
316 if ((int)fd == -1)
317 {
317 {
318 return -1;
318 return -1;
319 }
319 }
320 else
320 else
321 {
321 {
322 return write(fd, psWrite, WriteBufferSize);
322 return write(fd, psWrite, WriteBufferSize);
323 }
323 }
324 }
324 }
325
325
326
326
327 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
327 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
328 {
328 {
329
329
330 if ((int)fd == -1)
330 if ((int)fd == -1)
331 {
331 {
332 return -1;
332 return -1;
333 }
333 }
334 else
334 else
335 {
335 {
336 return read(fd, psReadHex, ReadBufferSize);
336 return read(fd, psReadHex, ReadBufferSize);
337 }
337 }
338
338
339 }
339 }
340
340
341 #endif
341 #endif
342 #endif //#ifdef HAVE_TERMIOS_H
342 #endif //#ifdef HAVE_TERMIOS_H
343
343
General Comments 0
You need to be logged in to leave comments. Login now