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