##// END OF EJS Templates
RAS
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr -
r9:075f1b173d0a alexis
parent child
Show More
@@ -1,299 +1,299
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 return fd;
21 return fd;
22 }
22 }
23
23
24 int rs232close(rs232port_t fd)
24 int rs232close(rs232port_t fd)
25 {
25 {
26 if ((int)fd == -1)
26 if ((int)fd == -1)
27 {
27 {
28 return -1;
28 return -1;
29 }
29 }
30 else
30 else
31 {
31 {
32 close(fd);
32 close(fd);
33 return 0;
33 return 0;
34 }
34 }
35 }
35 }
36
36
37
37
38 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
38 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
39 {
39 {
40 if ((int)fd == -1)
40 if ((int)fd == -1)
41 {
41 {
42 return -1;
42 return -1;
43 }
43 }
44 else
44 else
45 {
45 {
46 struct termios terminos;
46 struct termios terminos;
47 tcgetattr(fd, &terminos);
47 tcgetattr(fd, &terminos);
48 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
48 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
49 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
49 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
50 terminos.c_cflag |= (CLOCAL | CREAD);
50 terminos.c_cflag |= (CLOCAL | CREAD);
51 rs232cfparity(fd, &terminos, Parity);
51 rs232cfparity(fd, &terminos, Parity);
52 rs232cfnbstop(fd, &terminos, NbStop);
52 rs232cfnbstop(fd, &terminos, NbStop);
53 rs232cfcsize(fd, &terminos, ChSize);
53 rs232cfcsize(fd, &terminos, ChSize);
54 tcsetattr(fd, TCSANOW, &terminos);
54 tcsetattr(fd, TCSANOW, &terminos);
55 return 0;
55 return 0;
56 }
56 }
57 }
57 }
58
58
59 int rs232setbaudrate(rs232port_t fd, int baudrate)
59 int rs232setbaudrate(rs232port_t fd, int baudrate)
60 {
60 {
61 if ((int)fd == -1)
61 if ((int)fd == -1)
62 {
62 {
63 return fd;
63 return fd;
64 }
64 }
65 else
65 else
66 {
66 {
67 struct termios terminos;
67 struct termios terminos;
68 tcgetattr(fd, &terminos);
68 tcgetattr(fd, &terminos);
69 cfsetispeed(&terminos, rs232cfspeed(baudrate));
69 cfsetispeed(&terminos, rs232cfspeed(baudrate));
70 cfsetospeed(&terminos, rs232cfspeed(baudrate));
70 cfsetospeed(&terminos, rs232cfspeed(baudrate));
71 tcsetattr(fd, TCSANOW, &terminos);
71 tcsetattr(fd, TCSANOW, &terminos);
72 return 0;
72 return 0;
73 }
73 }
74 }
74 }
75
75
76 int rs232setparity(rs232port_t fd, rs232parity Parity)
76 int rs232setparity(rs232port_t fd, rs232parity Parity)
77 {
77 {
78 if ((int)fd == -1)
78 if ((int)fd == -1)
79 {
79 {
80 return fd;
80 return fd;
81 }
81 }
82 else
82 else
83 {
83 {
84 struct termios terminos;
84 struct termios terminos;
85 tcgetattr(fd, &terminos);
85 tcgetattr(fd, &terminos);
86 terminos.c_cflag = Parity;
86 terminos.c_cflag = Parity;
87 tcsetattr(fd, TCSANOW, &terminos);
87 tcsetattr(fd, TCSANOW, &terminos);
88 return 0;
88 return 0;
89 }
89 }
90 }
90 }
91
91
92 int rs232setnbstop(rs232port_t fd, int NbStop)
92 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
93 {
93 {
94 if ((int)fd == -1)
94 if ((int)fd == -1)
95 {
95 {
96 return fd;
96 return fd;
97 }
97 }
98 else
98 else
99 {
99 {
100 struct termios terminos;
100 struct termios terminos;
101 tcgetattr(fd, &terminos);
101 tcgetattr(fd, &terminos);
102 switch(NbStop)
102 switch(NbStop)
103 {
103 {
104 case 2:
104 case 2:
105 terminos.c_cflag |= CSTOPB;
105 terminos.c_cflag |= CSTOPB;
106 break;
106 break;
107 default:
107 default:
108 terminos.c_cflag &= ~CSTOPB;
108 terminos.c_cflag &= ~CSTOPB;
109 break;
109 break;
110 }
110 }
111 tcsetattr(fd, TCSANOW, &terminos);
111 tcsetattr(fd, TCSANOW, &terminos);
112 return 0;
112 return 0;
113 }
113 }
114 }
114 }
115
115
116
116
117 int rs232setcsize(rs232port_t fd, int ChSize)
117 int rs232setcsize(rs232port_t fd, int ChSize)
118 {
118 {
119 if ((int)fd == -1)
119 if ((int)fd == -1)
120 {
120 {
121 return fd;
121 return fd;
122 }
122 }
123 else
123 else
124 {
124 {
125 struct termios terminos;
125 struct termios terminos;
126 tcgetattr(fd, &terminos);
126 tcgetattr(fd, &terminos);
127 switch(ChSize)
127 switch(ChSize)
128 {
128 {
129 case 5:
129 case 5:
130 terminos.c_cflag |= CS5;
130 terminos.c_cflag |= CS5;
131 break;
131 break;
132 case 6:
132 case 6:
133 terminos.c_cflag |= CS6;
133 terminos.c_cflag |= CS6;
134 break;
134 break;
135 case 7:
135 case 7:
136 terminos.c_cflag |= CS7;
136 terminos.c_cflag |= CS7;
137 break;
137 break;
138 default:
138 default:
139 terminos.c_cflag |= CS8;
139 terminos.c_cflag |= CS8;
140 break;
140 break;
141 }
141 }
142 tcsetattr(fd, TCSANOW, &terminos);
142 tcsetattr(fd, TCSANOW, &terminos);
143 return 0;
143 return 0;
144 }
144 }
145 }
145 }
146
146
147 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
147 rs232speed_t rs232cfspeed(unsigned int BaudeRate)
148 {
148 {
149 if(BaudeRate<25)
149 if(BaudeRate<25)
150 return B0;
150 return B0;
151
151
152 if(BaudeRate<67)
152 if(BaudeRate<67)
153 return B50;
153 return B50;
154
154
155 if(BaudeRate<93)
155 if(BaudeRate<93)
156 return B75;
156 return B75;
157
157
158 if(BaudeRate<123)
158 if(BaudeRate<123)
159 return B110;
159 return B110;
160
160
161 if(BaudeRate<142)
161 if(BaudeRate<142)
162 return B134;
162 return B134;
163
163
164 if(BaudeRate<175)
164 if(BaudeRate<175)
165 return B150;
165 return B150;
166
166
167 if(BaudeRate<250)
167 if(BaudeRate<250)
168 return B200;
168 return B200;
169
169
170 if(BaudeRate<450)
170 if(BaudeRate<450)
171 return B300;
171 return B300;
172
172
173 if(BaudeRate<900)
173 if(BaudeRate<900)
174 return B600;
174 return B600;
175
175
176 if(BaudeRate<1500)
176 if(BaudeRate<1500)
177 return B1200;
177 return B1200;
178
178
179 if(BaudeRate<2100)
179 if(BaudeRate<2100)
180 return B1800;
180 return B1800;
181
181
182 if(BaudeRate<3600)
182 if(BaudeRate<3600)
183 return B2400;
183 return B2400;
184
184
185 if(BaudeRate<7200)
185 if(BaudeRate<7200)
186 return B4800;
186 return B4800;
187
187
188 if(BaudeRate<1400)
188 if(BaudeRate<1400)
189 return B9600;
189 return B9600;
190
190
191 if(BaudeRate<28800)
191 if(BaudeRate<28800)
192 return B19200;
192 return B19200;
193
193
194 if(BaudeRate<48000)
194 if(BaudeRate<48000)
195 return B38400;
195 return B38400;
196
196
197 if(BaudeRate<86400)
197 if(BaudeRate<86400)
198 return B57600;
198 return B57600;
199
199
200 if(BaudeRate<172800)
200 if(BaudeRate<172800)
201 return B115200;
201 return B115200;
202 else
202 else
203 return B230400;
203 return B230400;
204 }
204 }
205
205
206
206
207 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
207 int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity)
208 {
208 {
209 if ((int)fd == -1)
209 if ((int)fd == -1)
210 {
210 {
211 return fd;
211 return fd;
212 }
212 }
213 else
213 else
214 {
214 {
215 terminos->c_cflag = Parity;
215 terminos->c_cflag = Parity;
216 return 0;
216 return 0;
217 }
217 }
218 }
218 }
219
219
220 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
220 int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop)
221 {
221 {
222 if ((int)fd == -1)
222 if ((int)fd == -1)
223 {
223 {
224 return fd;
224 return fd;
225 }
225 }
226 else
226 else
227 {
227 {
228 switch(NbStop)
228 switch(NbStop)
229 {
229 {
230 case 2:
230 case 2:
231 terminos->c_cflag |= CSTOPB;
231 terminos->c_cflag |= CSTOPB;
232 break;
232 break;
233 default:
233 default:
234 terminos->c_cflag &= ~CSTOPB;
234 terminos->c_cflag &= ~CSTOPB;
235 break;
235 break;
236 }
236 }
237 return 0;
237 return 0;
238 }
238 }
239 }
239 }
240
240
241
241
242 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
242 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
243 {
243 {
244 if ((int)fd == -1)
244 if ((int)fd == -1)
245 {
245 {
246 return fd;
246 return fd;
247 }
247 }
248 else
248 else
249 {
249 {
250 switch(ChSize)
250 switch(ChSize)
251 {
251 {
252 case 5:
252 case 5:
253 terminos->c_cflag |= CS5;
253 terminos->c_cflag |= CS5;
254 break;
254 break;
255 case 6:
255 case 6:
256 terminos->c_cflag |= CS6;
256 terminos->c_cflag |= CS6;
257 break;
257 break;
258 case 7:
258 case 7:
259 terminos->c_cflag |= CS7;
259 terminos->c_cflag |= CS7;
260 break;
260 break;
261 default:
261 default:
262 terminos->c_cflag |= CS8;
262 terminos->c_cflag |= CS8;
263 break;
263 break;
264 }
264 }
265 return 0;
265 return 0;
266 }
266 }
267 }
267 }
268
268
269
269
270 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
270 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
271 {
271 {
272 if ((int)fd == -1)
272 if ((int)fd == -1)
273 {
273 {
274 return -1;
274 return -1;
275 }
275 }
276 else
276 else
277 {
277 {
278 return write(fd, psWrite, WriteBufferSize);
278 return write(fd, psWrite, WriteBufferSize);
279 }
279 }
280 }
280 }
281
281
282
282
283 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
283 int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
284 {
284 {
285
285
286 if ((int)fd == -1)
286 if ((int)fd == -1)
287 {
287 {
288 return -1;
288 return -1;
289 }
289 }
290 else
290 else
291 {
291 {
292 return read(fd, psReadHex, ReadBufferSize);
292 return read(fd, psReadHex, ReadBufferSize);
293 }
293 }
294
294
295 }
295 }
296
296
297 #endif
297 #endif
298 #endif //#ifdef HAVE_TERMIOS_H
298 #endif //#ifdef HAVE_TERMIOS_H
299
299
General Comments 0
You need to be logged in to leave comments. Login now