@@ -1,50 +1,51 | |||
|
1 | 1 | #ifndef RS232_H |
|
2 | 2 | #define RS232_H |
|
3 | 3 | |
|
4 | 4 | #define badPortValue -1 |
|
5 | 5 | #define rs232noerr 0 |
|
6 | 6 | typedef int rs232port_t; |
|
7 | 7 | typedef int rs232speed_t; |
|
8 | 8 | typedef enum {rs232OneStop,rs232One5Stop,rs232TwoStop}rs232stop; |
|
9 | 9 | typedef enum {rs232parityNo,rs232parityOdd,rs232parityEven}rs232parity; |
|
10 | 10 | |
|
11 | 11 | #ifdef __cplusplus |
|
12 | 12 | extern "C" { |
|
13 | 13 | #endif |
|
14 | 14 | typedef struct rs232portslist_t |
|
15 | 15 | { |
|
16 | 16 | char* name; |
|
17 | 17 | struct rs232portslist_t* next; |
|
18 | 18 | }rs232portslist_t; |
|
19 | 19 | #ifdef __cplusplus |
|
20 | 20 | } |
|
21 | 21 | #endif |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | #ifdef __cplusplus |
|
25 | 25 | #define rs232extern extern "C" |
|
26 | 26 | #else |
|
27 | 27 | #define rs232extern extern |
|
28 | 28 | #endif |
|
29 | 29 | rs232extern rs232port_t rs232open(char* psPortName); |
|
30 | 30 | rs232extern rs232portslist_t* rs232getportlist(); |
|
31 | 31 | rs232extern void rs232deleteportlist(rs232portslist_t* list); |
|
32 | 32 | rs232extern int rs232close(rs232port_t fd); |
|
33 | 33 | rs232extern int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop); |
|
34 | 34 | rs232extern int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize); |
|
35 | 35 | rs232extern int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize); |
|
36 | rs232extern int rs232availablebytes(rs232port_t fd); | |
|
36 | 37 | rs232extern int rs232setparity(rs232port_t fd, rs232parity Parity); |
|
37 | 38 | rs232extern int rs232setnbstop(rs232port_t fd, rs232stop NbStop); |
|
38 | 39 | rs232extern int rs232setcsize(rs232port_t fd, int ChSize); |
|
39 | 40 | rs232extern int rs232setbaudrate(rs232port_t fd, int baudrate); |
|
40 | 41 | rs232extern int rs232setRTS(rs232port_t fd); |
|
41 | 42 | rs232extern int rs232clearRTS(rs232port_t fd); |
|
42 | 43 | rs232extern int rs232setDTR(rs232port_t fd); |
|
43 | 44 | rs232extern int rs232clearDTR(rs232port_t fd); |
|
44 | 45 | rs232extern int rs232saferead(rs232port_t fd,char* data,int count ); |
|
45 | 46 | rs232extern int rs232safewrite(rs232port_t fd,char* data,int count); |
|
46 | 47 | |
|
47 | 48 | #endif |
|
48 | 49 | |
|
49 | 50 | |
|
50 | 51 |
@@ -1,532 +1,538 | |||
|
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 <malloc.h> |
|
7 | 7 | #include <dirent.h> |
|
8 | 8 | #include <limits.h> |
|
9 | 9 | #include <stdlib.h> |
|
10 | 10 | #include <sys/stat.h> |
|
11 | 11 | |
|
12 | 12 | #include <termios.h> |
|
13 | 13 | #include <termio.h> |
|
14 | 14 | #include "RS232.h" |
|
15 | 15 | |
|
16 | 16 | rs232speed_t rs232cfspeed(unsigned int BaudeRate); |
|
17 | 17 | |
|
18 | 18 | rs232port_t rs232open(char* psPortName) |
|
19 | 19 | { |
|
20 | 20 | rs232port_t fd; |
|
21 | 21 | int flags; |
|
22 | 22 | fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY );//| O_NDELAY); |
|
23 | 23 | //fcntl((int)fd, F_SETFL, FNDELAY); |
|
24 | 24 | //fd = open(psPortName, O_RDWR | O_NOCTTY); |
|
25 | 25 | #ifdef RS232_debug |
|
26 | 26 | if(fd==-1)printf("can't open Port\n"); |
|
27 | 27 | #else |
|
28 | 28 | if(fd!=-1) |
|
29 | 29 | { |
|
30 | 30 | //flags = fcntl(fd, F_GETFL); |
|
31 | 31 | //flags |= O_NONBLOCK; |
|
32 | 32 | //flags|=FNDELAY; |
|
33 | 33 | //fcntl(fd, F_SETFL, flags); |
|
34 | 34 | //fcntl((int)fd, F_SETFL, FNDELAY); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | #endif |
|
38 | 38 | return fd; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | int rs232close(rs232port_t fd) |
|
42 | 42 | { |
|
43 | 43 | if ((int)fd == -1) |
|
44 | 44 | { |
|
45 | 45 | return -1; |
|
46 | 46 | } |
|
47 | 47 | else |
|
48 | 48 | { |
|
49 | 49 | close(fd); |
|
50 | 50 | return 0; |
|
51 | 51 | } |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | rs232portslist_t* rs232getportlist() |
|
55 | 55 | { |
|
56 | 56 | struct dirent *dp; |
|
57 | 57 | char* path="/dev/serial/by-id"; |
|
58 | 58 | DIR *dir = opendir(path); |
|
59 | 59 | rs232portslist_t* firstitem=NULL; |
|
60 | 60 | rs232portslist_t* previtem=NULL; |
|
61 | 61 | char* linkname; |
|
62 | 62 | char* devName; |
|
63 | 63 | int i=0; |
|
64 | 64 | if(dir!=NULL) |
|
65 | 65 | { |
|
66 | 66 | while ((dp=readdir(dir)) != NULL) |
|
67 | 67 | { |
|
68 | 68 | int len = strlen(path)+strlen(dp->d_name)+8;//+8 => let's put some margin |
|
69 | 69 | linkname = (char*)malloc(len); |
|
70 | 70 | memset(linkname,'\0',len); |
|
71 | 71 | struct stat statbuf; |
|
72 | 72 | strcpy(linkname,path); |
|
73 | 73 | strcat(linkname,"/"); |
|
74 | 74 | strcat(linkname,dp->d_name); |
|
75 | 75 | lstat(linkname, &statbuf); |
|
76 | 76 | if(S_ISLNK(statbuf.st_mode)) |
|
77 | 77 | { |
|
78 | 78 | devName=realpath(linkname, NULL); |
|
79 | 79 | if(devName!=NULL) |
|
80 | 80 | { |
|
81 | 81 | rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); |
|
82 | 82 | item->name = devName; |
|
83 | 83 | item->next = NULL; |
|
84 | 84 | if(NULL!=previtem)previtem->next = item; |
|
85 | 85 | previtem = item; |
|
86 | 86 | if(NULL==firstitem)firstitem = item; |
|
87 | 87 | } |
|
88 | 88 | } |
|
89 | 89 | free(linkname); |
|
90 | 90 | } |
|
91 | 91 | } |
|
92 | 92 | return firstitem; |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void rs232deleteportlist(rs232portslist_t* list) |
|
96 | 96 | { |
|
97 | 97 | if(list!=NULL) |
|
98 | 98 | { |
|
99 | 99 | if(list->next != NULL) |
|
100 | 100 | rs232deleteportlist(list->next); |
|
101 | 101 | free(list); |
|
102 | 102 | } |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) |
|
106 | 106 | { |
|
107 | 107 | if ((int)fd == -1) |
|
108 | 108 | { |
|
109 | 109 | return -1; |
|
110 | 110 | } |
|
111 | 111 | else |
|
112 | 112 | { |
|
113 | 113 | struct termios terminos; |
|
114 | 114 | tcgetattr(fd, &terminos); |
|
115 | 115 | terminos.c_iflag=0; |
|
116 | 116 | terminos.c_oflag=0; |
|
117 | 117 | terminos.c_cflag=0; |
|
118 | 118 | terminos.c_lflag=0; |
|
119 | 119 | cfsetispeed(&terminos, rs232cfspeed(BaudeRate)); |
|
120 | 120 | cfsetospeed(&terminos, rs232cfspeed(BaudeRate)); |
|
121 | 121 | terminos.c_cflag |= (CLOCAL | CREAD); |
|
122 | 122 | rs232cfparity((int)fd, &terminos, Parity); |
|
123 | 123 | rs232cfnbstop((int)fd, &terminos, NbStop); |
|
124 | 124 | rs232cfcsize((int)fd, &terminos, ChSize); |
|
125 | 125 | terminos.c_cc[VMIN]=0; |
|
126 | 126 | terminos.c_cc[VTIME]=1; |
|
127 | 127 | tcflush(fd, TCIFLUSH); |
|
128 | 128 | #ifdef RS232_debug |
|
129 | 129 | if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n"); |
|
130 | 130 | #else |
|
131 | 131 | tcsetattr(fd, TCSANOW, &terminos); |
|
132 | 132 | #endif |
|
133 | 133 | return 0; |
|
134 | 134 | } |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | int rs232setbaudrate(rs232port_t fd, int baudrate) |
|
138 | 138 | { |
|
139 | 139 | if ((int)fd == -1) |
|
140 | 140 | { |
|
141 | 141 | return fd; |
|
142 | 142 | } |
|
143 | 143 | else |
|
144 | 144 | { |
|
145 | 145 | struct termios terminos; |
|
146 | 146 | tcgetattr((int)fd, &terminos); |
|
147 | 147 | cfsetispeed(&terminos, rs232cfspeed(baudrate)); |
|
148 | 148 | cfsetospeed(&terminos, rs232cfspeed(baudrate)); |
|
149 | 149 | tcsetattr((int)fd, TCSANOW, &terminos); |
|
150 | 150 | return 0; |
|
151 | 151 | } |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | int rs232setparity(rs232port_t fd, rs232parity Parity) |
|
155 | 155 | { |
|
156 | 156 | if ((int)fd == -1) |
|
157 | 157 | { |
|
158 | 158 | return fd; |
|
159 | 159 | } |
|
160 | 160 | else |
|
161 | 161 | { |
|
162 | 162 | struct termios terminos; |
|
163 | 163 | tcgetattr((int)fd, &terminos); |
|
164 | 164 | terminos.c_cflag &= ~PARENB; |
|
165 | 165 | terminos.c_cflag &= ~PARODD; |
|
166 | 166 | switch(Parity) |
|
167 | 167 | { |
|
168 | 168 | case rs232parityNo: |
|
169 | 169 | terminos.c_cflag &= ~PARENB; |
|
170 | 170 | break; |
|
171 | 171 | case rs232parityOdd: |
|
172 | 172 | terminos.c_cflag |= PARENB; |
|
173 | 173 | terminos.c_cflag |= PARODD; |
|
174 | 174 | break; |
|
175 | 175 | case rs232parityEven: |
|
176 | 176 | terminos.c_cflag |= PARENB; |
|
177 | 177 | terminos.c_cflag &= ~PARODD; |
|
178 | 178 | break; |
|
179 | 179 | default: |
|
180 | 180 | terminos.c_cflag &= ~PARENB; |
|
181 | 181 | break; |
|
182 | 182 | } |
|
183 | 183 | tcsetattr((int)fd, TCSANOW, &terminos); |
|
184 | 184 | return 0; |
|
185 | 185 | } |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) |
|
189 | 189 | { |
|
190 | 190 | if ((int)fd == -1) |
|
191 | 191 | { |
|
192 | 192 | return fd; |
|
193 | 193 | } |
|
194 | 194 | else |
|
195 | 195 | { |
|
196 | 196 | struct termios terminos; |
|
197 | 197 | tcgetattr((int)fd, &terminos); |
|
198 | 198 | switch(NbStop) |
|
199 | 199 | { |
|
200 | 200 | case rs232OneStop: |
|
201 | 201 | terminos.c_cflag &= ~CSTOPB; |
|
202 | 202 | break; |
|
203 | 203 | case rs232One5Stop: |
|
204 | 204 | terminos.c_cflag |= CSTOPB; |
|
205 | 205 | break; |
|
206 | 206 | case rs232TwoStop: |
|
207 | 207 | terminos.c_cflag |= CSTOPB; |
|
208 | 208 | break; |
|
209 | 209 | default: |
|
210 | 210 | terminos.c_cflag &= ~CSTOPB; |
|
211 | 211 | break; |
|
212 | 212 | } |
|
213 | 213 | tcsetattr((int)fd, TCSANOW, &terminos); |
|
214 | 214 | return 0; |
|
215 | 215 | } |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | |
|
219 | 219 | int rs232setcsize(rs232port_t fd, int ChSize) |
|
220 | 220 | { |
|
221 | 221 | if ((int)fd == -1) |
|
222 | 222 | { |
|
223 | 223 | return fd; |
|
224 | 224 | } |
|
225 | 225 | else |
|
226 | 226 | { |
|
227 | 227 | struct termios terminos; |
|
228 | 228 | tcgetattr((int)fd, &terminos); |
|
229 | 229 | terminos.c_cflag &= ~CSIZE; |
|
230 | 230 | switch(ChSize) |
|
231 | 231 | { |
|
232 | 232 | case 5: |
|
233 | 233 | terminos.c_cflag |= CS5; |
|
234 | 234 | break; |
|
235 | 235 | case 6: |
|
236 | 236 | terminos.c_cflag |= CS6; |
|
237 | 237 | break; |
|
238 | 238 | case 7: |
|
239 | 239 | terminos.c_cflag |= CS7; |
|
240 | 240 | break; |
|
241 | 241 | default: |
|
242 | 242 | terminos.c_cflag |= CS8; |
|
243 | 243 | break; |
|
244 | 244 | } |
|
245 | 245 | tcsetattr((int)fd, TCSANOW, &terminos); |
|
246 | 246 | return 0; |
|
247 | 247 | } |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) |
|
251 | 251 | { |
|
252 | 252 | if(BaudeRate<25) |
|
253 | 253 | return B0; |
|
254 | 254 | |
|
255 | 255 | if(BaudeRate<67) |
|
256 | 256 | return B50; |
|
257 | 257 | |
|
258 | 258 | if(BaudeRate<93) |
|
259 | 259 | return B75; |
|
260 | 260 | |
|
261 | 261 | if(BaudeRate<123) |
|
262 | 262 | return B110; |
|
263 | 263 | |
|
264 | 264 | if(BaudeRate<142) |
|
265 | 265 | return B134; |
|
266 | 266 | |
|
267 | 267 | if(BaudeRate<175) |
|
268 | 268 | return B150; |
|
269 | 269 | |
|
270 | 270 | if(BaudeRate<250) |
|
271 | 271 | return B200; |
|
272 | 272 | |
|
273 | 273 | if(BaudeRate<450) |
|
274 | 274 | return B300; |
|
275 | 275 | |
|
276 | 276 | if(BaudeRate<900) |
|
277 | 277 | return B600; |
|
278 | 278 | |
|
279 | 279 | if(BaudeRate<1500) |
|
280 | 280 | return B1200; |
|
281 | 281 | |
|
282 | 282 | if(BaudeRate<2100) |
|
283 | 283 | return B1800; |
|
284 | 284 | |
|
285 | 285 | if(BaudeRate<3600) |
|
286 | 286 | return B2400; |
|
287 | 287 | |
|
288 | 288 | if(BaudeRate<7200) |
|
289 | 289 | return B4800; |
|
290 | 290 | |
|
291 | 291 | if(BaudeRate<1400) |
|
292 | 292 | return B9600; |
|
293 | 293 | |
|
294 | 294 | if(BaudeRate<28800) |
|
295 | 295 | return B19200; |
|
296 | 296 | |
|
297 | 297 | if(BaudeRate<48000) |
|
298 | 298 | return B38400; |
|
299 | 299 | |
|
300 | 300 | if(BaudeRate<86400) |
|
301 | 301 | return B57600; |
|
302 | 302 | |
|
303 | 303 | if(BaudeRate<172800) |
|
304 | 304 | return B115200; |
|
305 | 305 | |
|
306 | 306 | if(BaudeRate<345600) |
|
307 | 307 | return B230400; |
|
308 | 308 | |
|
309 | 309 | if(BaudeRate<345600) |
|
310 | 310 | return B460800; |
|
311 | 311 | |
|
312 | 312 | if(BaudeRate<748800) |
|
313 | 313 | return B576000; |
|
314 | 314 | |
|
315 | 315 | if(BaudeRate<1210800) |
|
316 | 316 | return B921600; |
|
317 | 317 | |
|
318 | 318 | if(BaudeRate<1750000) |
|
319 | 319 | return B1500000; |
|
320 | 320 | |
|
321 | 321 | if(BaudeRate<2250000) |
|
322 | 322 | return B2000000; |
|
323 | 323 | |
|
324 | 324 | if(BaudeRate<2750000) |
|
325 | 325 | return B2500000; |
|
326 | 326 | else |
|
327 | 327 | return B3000000; |
|
328 | 328 | |
|
329 | 329 | } |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity) |
|
333 | 333 | { |
|
334 | 334 | if ((int)fd == -1) |
|
335 | 335 | { |
|
336 | 336 | return fd; |
|
337 | 337 | } |
|
338 | 338 | else |
|
339 | 339 | { |
|
340 | 340 | terminos->c_cflag &= ~PARENB; |
|
341 | 341 | terminos->c_cflag &= ~PARODD; |
|
342 | 342 | switch(Parity) |
|
343 | 343 | { |
|
344 | 344 | case rs232parityNo: |
|
345 | 345 | terminos->c_cflag &= ~PARENB; |
|
346 | 346 | terminos->c_cflag &= ~PARODD; |
|
347 | 347 | break; |
|
348 | 348 | case rs232parityOdd: |
|
349 | 349 | terminos->c_cflag |= PARENB; |
|
350 | 350 | terminos->c_cflag |= PARODD; |
|
351 | 351 | break; |
|
352 | 352 | case rs232parityEven: |
|
353 | 353 | terminos->c_cflag |= PARENB; |
|
354 | 354 | terminos->c_cflag &= ~PARODD; |
|
355 | 355 | break; |
|
356 | 356 | default: |
|
357 | 357 | terminos->c_cflag &= ~PARENB; |
|
358 | 358 | terminos->c_cflag &= ~PARODD; |
|
359 | 359 | break; |
|
360 | 360 | } |
|
361 | 361 | return 0; |
|
362 | 362 | } |
|
363 | 363 | } |
|
364 | 364 | |
|
365 | 365 | int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop) |
|
366 | 366 | { |
|
367 | 367 | if ((int)fd == -1) |
|
368 | 368 | { |
|
369 | 369 | return fd; |
|
370 | 370 | } |
|
371 | 371 | else |
|
372 | 372 | { |
|
373 | 373 | switch(NbStop) |
|
374 | 374 | { |
|
375 | 375 | case 2: |
|
376 | 376 | terminos->c_cflag |= CSTOPB; |
|
377 | 377 | break; |
|
378 | 378 | default: |
|
379 | 379 | terminos->c_cflag &= ~CSTOPB; |
|
380 | 380 | break; |
|
381 | 381 | } |
|
382 | 382 | return 0; |
|
383 | 383 | } |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | |
|
387 | 387 | int rs232cfcsize(int fd, struct termios *terminos, int ChSize) |
|
388 | 388 | { |
|
389 | 389 | if ((int)fd == -1) |
|
390 | 390 | { |
|
391 | 391 | return fd; |
|
392 | 392 | } |
|
393 | 393 | else |
|
394 | 394 | { |
|
395 | 395 | terminos->c_cflag &= ~CSIZE; |
|
396 | 396 | switch(ChSize) |
|
397 | 397 | { |
|
398 | 398 | case 5: |
|
399 | 399 | terminos->c_cflag |= CS5; |
|
400 | 400 | break; |
|
401 | 401 | case 6: |
|
402 | 402 | terminos->c_cflag |= CS6; |
|
403 | 403 | break; |
|
404 | 404 | case 7: |
|
405 | 405 | terminos->c_cflag |= CS7; |
|
406 | 406 | break; |
|
407 | 407 | default: |
|
408 | 408 | terminos->c_cflag |= CS8; |
|
409 | 409 | break; |
|
410 | 410 | } |
|
411 | 411 | return 0; |
|
412 | 412 | } |
|
413 | 413 | } |
|
414 | 414 | |
|
415 | 415 | |
|
416 | 416 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) |
|
417 | 417 | { |
|
418 | 418 | if ((int)fd == -1) |
|
419 | 419 | { |
|
420 | 420 | return -1; |
|
421 | 421 | } |
|
422 | 422 | else |
|
423 | 423 | { |
|
424 | 424 | return write((int)fd, psWrite, WriteBufferSize); |
|
425 | 425 | } |
|
426 | 426 | } |
|
427 | 427 | |
|
428 | int rs232availablebytes(rs232port_t fd) | |
|
429 | { | |
|
430 | int bytes; | |
|
431 | ioctl(fd, FIONREAD, &bytes); | |
|
432 | return bytes; | |
|
433 | } | |
|
428 | 434 | |
|
429 | 435 | int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize) |
|
430 | 436 | { |
|
431 | 437 | |
|
432 | 438 | if ((int)fd == -1) |
|
433 | 439 | { |
|
434 | 440 | return -1; |
|
435 | 441 | } |
|
436 | 442 | else |
|
437 | 443 | { |
|
438 | 444 | return read((int)fd, psReadHex, ReadBufferSize); |
|
439 | 445 | } |
|
440 | 446 | |
|
441 | 447 | } |
|
442 | 448 | |
|
443 | 449 | |
|
444 | 450 | int rs232setRTS(rs232port_t fd) |
|
445 | 451 | { |
|
446 | 452 | int status; |
|
447 | 453 | ioctl((int)fd, TIOCMGET, &status); |
|
448 | 454 | status &= ~TIOCM_RTS; |
|
449 | 455 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
450 | 456 | { |
|
451 | 457 | return -1; |
|
452 | 458 | } |
|
453 | 459 | return 0; |
|
454 | 460 | } |
|
455 | 461 | |
|
456 | 462 | int rs232clearRTS(rs232port_t fd) |
|
457 | 463 | { |
|
458 | 464 | int status; |
|
459 | 465 | ioctl((int)fd, TIOCMGET, &status); |
|
460 | 466 | status |= TIOCM_RTS; |
|
461 | 467 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
462 | 468 | { |
|
463 | 469 | return -1; |
|
464 | 470 | } |
|
465 | 471 | return 0; |
|
466 | 472 | } |
|
467 | 473 | |
|
468 | 474 | int rs232setDTR(rs232port_t fd) |
|
469 | 475 | { |
|
470 | 476 | int status; |
|
471 | 477 | ioctl((int)fd, TIOCMGET, &status); |
|
472 | 478 | status &= ~TIOCM_DTR; |
|
473 | 479 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
474 | 480 | { |
|
475 | 481 | return -1; |
|
476 | 482 | } |
|
477 | 483 | return 0; |
|
478 | 484 | } |
|
479 | 485 | |
|
480 | 486 | |
|
481 | 487 | int rs232clearDTR(rs232port_t fd) |
|
482 | 488 | { |
|
483 | 489 | int status; |
|
484 | 490 | ioctl((int)fd, TIOCMGET, &status); |
|
485 | 491 | status |= TIOCM_DTR; |
|
486 | 492 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
487 | 493 | { |
|
488 | 494 | return -1; |
|
489 | 495 | } |
|
490 | 496 | return 0; |
|
491 | 497 | } |
|
492 | 498 | |
|
493 | 499 | |
|
494 | 500 | |
|
495 | 501 | int rs232saferead(rs232port_t fd,char* data,int count ) |
|
496 | 502 | { |
|
497 | 503 | int read=0; |
|
498 | 504 | int i=0; |
|
499 | 505 | for(i=0;i<1000;i++) |
|
500 | 506 | { |
|
501 | 507 | read = rs232read((int)fd,data,count); |
|
502 | 508 | //printf("read %d bytes\n",read); |
|
503 | 509 | if(read==-1)read=0; |
|
504 | 510 | count-=read; |
|
505 | 511 | data+=read; |
|
506 | 512 | if(count==0) |
|
507 | 513 | return 0; |
|
508 | 514 | usleep(10); |
|
509 | 515 | } |
|
510 | 516 | return -1; |
|
511 | 517 | } |
|
512 | 518 | |
|
513 | 519 | |
|
514 | 520 | |
|
515 | 521 | int rs232safewrite(rs232port_t fd,char* data,int count) |
|
516 | 522 | { |
|
517 | 523 | int written=0; |
|
518 | 524 | int i=0; |
|
519 | 525 | for(i=0;i<1000;i++) |
|
520 | 526 | { |
|
521 | 527 | written = rs232write((int)fd,data+written,count); |
|
522 | 528 | //printf("%d bytes written\n",written); |
|
523 | 529 | if(written==-1)written=0; |
|
524 | 530 | count-=written; |
|
525 | 531 | data+=written; |
|
526 | 532 | if(count==0) |
|
527 | 533 | return 0; |
|
528 | 534 | } |
|
529 | 535 | return -1; |
|
530 | 536 | } |
|
531 | 537 | |
|
532 | 538 |
@@ -1,388 +1,395 | |||
|
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 <malloc.h> |
|
7 | 7 | |
|
8 | 8 | #include <windows.h> |
|
9 | 9 | #include "RS232.h" |
|
10 | 10 | #include <locale.h> |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | int privatedecodeparity(rs232parity Parity ) |
|
15 | 15 | { |
|
16 | 16 | switch(Parity) |
|
17 | 17 | { |
|
18 | 18 | case rs232parityNo: |
|
19 | 19 | return NOPARITY; |
|
20 | 20 | break; |
|
21 | 21 | case rs232parityOdd: |
|
22 | 22 | return ODDPARITY; |
|
23 | 23 | break; |
|
24 | 24 | case rs232parityEven: |
|
25 | 25 | return EVENPARITY; |
|
26 | 26 | break; |
|
27 | 27 | default: |
|
28 | 28 | return NOPARITY; |
|
29 | 29 | break; |
|
30 | 30 | } |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | int privatedecodestop(rs232stop NbStop) |
|
34 | 34 | { |
|
35 | 35 | switch(NbStop) |
|
36 | 36 | { |
|
37 | 37 | case rs232OneStop: |
|
38 | 38 | return ONESTOPBIT; |
|
39 | 39 | break; |
|
40 | 40 | case rs232One5Stop: |
|
41 | 41 | return ONE5STOPBITS; |
|
42 | 42 | break; |
|
43 | 43 | case rs232TwoStop: |
|
44 | 44 | return TWOSTOPBITS; |
|
45 | 45 | break; |
|
46 | 46 | default: |
|
47 | 47 | return ONESTOPBIT; |
|
48 | 48 | break; |
|
49 | 49 | } |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) |
|
55 | 55 | { |
|
56 | 56 | |
|
57 | 57 | if(BaudeRate<123) |
|
58 | 58 | return (rs232speed_t)CBR_110; |
|
59 | 59 | |
|
60 | 60 | if(BaudeRate<450) |
|
61 | 61 | return (rs232speed_t)CBR_300; |
|
62 | 62 | |
|
63 | 63 | if(BaudeRate<900) |
|
64 | 64 | return (rs232speed_t)CBR_600; |
|
65 | 65 | |
|
66 | 66 | if(BaudeRate<1500) |
|
67 | 67 | return (rs232speed_t)CBR_1200; |
|
68 | 68 | |
|
69 | 69 | if(BaudeRate<3600) |
|
70 | 70 | return (rs232speed_t)CBR_2400; |
|
71 | 71 | |
|
72 | 72 | if(BaudeRate<7200) |
|
73 | 73 | return (rs232speed_t)CBR_4800; |
|
74 | 74 | |
|
75 | 75 | if(BaudeRate<14000) |
|
76 | 76 | return (rs232speed_t)CBR_9600; |
|
77 | 77 | |
|
78 | 78 | if(BaudeRate<16800) |
|
79 | 79 | return (rs232speed_t)CBR_14400; |
|
80 | 80 | |
|
81 | 81 | if(BaudeRate<28800) |
|
82 | 82 | return (rs232speed_t)CBR_19200; |
|
83 | 83 | |
|
84 | 84 | if(BaudeRate<48000) |
|
85 | 85 | return (rs232speed_t)CBR_38400; |
|
86 | 86 | |
|
87 | 87 | if(BaudeRate<86400) |
|
88 | 88 | return (rs232speed_t)CBR_57600; |
|
89 | 89 | |
|
90 | 90 | if(BaudeRate<172800) |
|
91 | 91 | return (rs232speed_t)CBR_115200; |
|
92 | 92 | |
|
93 | 93 | if(BaudeRate<345600) |
|
94 | 94 | return (rs232speed_t)230400; |
|
95 | 95 | |
|
96 | 96 | if(BaudeRate<345600) |
|
97 | 97 | return (rs232speed_t)460800; |
|
98 | 98 | |
|
99 | 99 | if(BaudeRate<748800) |
|
100 | 100 | return (rs232speed_t)576000; |
|
101 | 101 | |
|
102 | 102 | if(BaudeRate<1210800) |
|
103 | 103 | return (rs232speed_t)921600; |
|
104 | 104 | |
|
105 | 105 | if(BaudeRate<1750000) |
|
106 | 106 | return (rs232speed_t)1500000; |
|
107 | 107 | |
|
108 | 108 | if(BaudeRate<2250000) |
|
109 | 109 | return (rs232speed_t)2000000; |
|
110 | 110 | |
|
111 | 111 | if(BaudeRate<2750000) |
|
112 | 112 | return (rs232speed_t)2500000; |
|
113 | 113 | else |
|
114 | 114 | return (rs232speed_t)3000000; |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | rs232port_t rs232open(char* psPortName) |
|
119 | 119 | { |
|
120 | 120 | rs232port_t fd; |
|
121 | 121 | /* Char to Wchar conversion*/ |
|
122 | 122 | int neededSize = MultiByteToWideChar(CP_ACP, 0, psPortName, -1, 0, 0); |
|
123 | 123 | wchar_t* wcstr = (wchar_t*)malloc(sizeof(wchar_t)*neededSize); |
|
124 | 124 | MultiByteToWideChar(CP_ACP, 0, psPortName, -1, wcstr, neededSize); |
|
125 | 125 | fd = (rs232port_t)CreateFile(wcstr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); |
|
126 | 126 | free(wcstr); |
|
127 | 127 | #ifdef RS232_debug |
|
128 | 128 | if(fd==(rs232port_t)INVALID_HANDLE_VALUE) |
|
129 | 129 | { |
|
130 | 130 | printf("can't open Port\n"); |
|
131 | 131 | return (rs232port_t)badPortValue; |
|
132 | 132 | } |
|
133 | 133 | #endif |
|
134 | 134 | return fd; |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | int rs232close(rs232port_t fd) |
|
138 | 138 | { |
|
139 | 139 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
140 | 140 | { |
|
141 | 141 | return (rs232port_t)badPortValue; |
|
142 | 142 | } |
|
143 | 143 | else |
|
144 | 144 | { |
|
145 | 145 | CloseHandle((HANDLE)fd); |
|
146 | 146 | return rs232noerr; |
|
147 | 147 | } |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | rs232portslist_t* rs232getportlist() //here is the very dirty way! |
|
151 | 151 | { |
|
152 | 152 | int i=0; |
|
153 | 153 | char devName[]="COM111"; |
|
154 | 154 | rs232port_t testport; |
|
155 | 155 | rs232portslist_t* firstitem=NULL; |
|
156 | 156 | rs232portslist_t* previtem=NULL; |
|
157 | 157 | for(i=0;i<256;i++) |
|
158 | 158 | { |
|
159 | 159 | devName[3] = '\0'; |
|
160 | 160 | devName[4] = '\0'; |
|
161 | 161 | devName[5] = '\0'; |
|
162 | 162 | devName[6] = '\0'; |
|
163 | 163 | sprintf(devName+3,"%d",i); |
|
164 | 164 | testport= rs232open(devName); |
|
165 | 165 | if(testport != (rs232port_t)badPortValue) |
|
166 | 166 | { |
|
167 | 167 | rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); |
|
168 | 168 | char* name = (char*)malloc(7); |
|
169 | 169 | strcpy(name,devName); |
|
170 | 170 | item->name = name; |
|
171 | 171 | item->next = NULL; |
|
172 | 172 | if(NULL!=previtem)previtem->next = item; |
|
173 | 173 | previtem = item; |
|
174 | 174 | if(NULL==firstitem)firstitem = item; |
|
175 | 175 | rs232close(testport); |
|
176 | 176 | } |
|
177 | 177 | } |
|
178 | 178 | return firstitem; |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | void rs232deleteportlist(rs232portslist_t* list) |
|
182 | 182 | { |
|
183 | 183 | if(list!=NULL) |
|
184 | 184 | { |
|
185 | 185 | if(list->next != NULL) |
|
186 | 186 | rs232deleteportlist(list->next); |
|
187 | 187 | free(list); |
|
188 | 188 | } |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | |
|
192 | 192 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) |
|
193 | 193 | { |
|
194 | 194 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
195 | 195 | { |
|
196 | 196 | return (rs232port_t)badPortValue; |
|
197 | 197 | } |
|
198 | 198 | else |
|
199 | 199 | { |
|
200 | 200 | DCB dcbSerialParams = {0}; |
|
201 | 201 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
202 | 202 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
203 | 203 | dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); |
|
204 | 204 | dcbSerialParams.ByteSize=ChSize; |
|
205 | 205 | dcbSerialParams.StopBits=privatedecodestop(NbStop); |
|
206 | 206 | dcbSerialParams.Parity=privatedecodeparity(Parity); |
|
207 | 207 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
208 | 208 | COMMTIMEOUTS timeouts={0}; |
|
209 | 209 | timeouts.ReadIntervalTimeout=100; |
|
210 | 210 | timeouts.ReadTotalTimeoutConstant=100; |
|
211 | 211 | timeouts.ReadTotalTimeoutMultiplier=1; |
|
212 | 212 | timeouts.WriteTotalTimeoutConstant=100; |
|
213 | 213 | timeouts.WriteTotalTimeoutMultiplier=10; |
|
214 | 214 | SetCommTimeouts((HANDLE)fd, &timeouts); |
|
215 | 215 | return rs232noerr; |
|
216 | 216 | } |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | int rs232setbaudrate(rs232port_t fd, int baudrate) |
|
220 | 220 | { |
|
221 | 221 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
222 | 222 | { |
|
223 | 223 | return (rs232port_t)badPortValue; |
|
224 | 224 | } |
|
225 | 225 | else |
|
226 | 226 | { |
|
227 | 227 | DCB dcbSerialParams = {0}; |
|
228 | 228 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
229 | 229 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
230 | 230 | dcbSerialParams.BaudRate=rs232cfspeed(baudrate); |
|
231 | 231 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
232 | 232 | return rs232noerr; |
|
233 | 233 | } |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | int rs232setparity(rs232port_t fd, rs232parity Parity) |
|
237 | 237 | { |
|
238 | 238 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
239 | 239 | { |
|
240 | 240 | return (rs232port_t)badPortValue; |
|
241 | 241 | } |
|
242 | 242 | else |
|
243 | 243 | { |
|
244 | 244 | DCB dcbSerialParams = {0}; |
|
245 | 245 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
246 | 246 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
247 | 247 | dcbSerialParams.Parity = privatedecodeparity(Parity); |
|
248 | 248 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
249 | 249 | return rs232noerr; |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) |
|
254 | 254 | { |
|
255 | 255 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
256 | 256 | { |
|
257 | 257 | return (rs232port_t)badPortValue; |
|
258 | 258 | } |
|
259 | 259 | else |
|
260 | 260 | { |
|
261 | 261 | DCB dcbSerialParams = {0}; |
|
262 | 262 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
263 | 263 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
264 | 264 | dcbSerialParams.StopBits = privatedecodestop(NbStop); |
|
265 | 265 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
266 | 266 | return rs232noerr; |
|
267 | 267 | } |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | int rs232setcsize(rs232port_t fd, int ChSize) |
|
272 | 272 | { |
|
273 | 273 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
274 | 274 | { |
|
275 | 275 | return (rs232port_t)badPortValue; |
|
276 | 276 | } |
|
277 | 277 | else |
|
278 | 278 | { |
|
279 | 279 | DCB dcbSerialParams = {0}; |
|
280 | 280 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
281 | 281 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
282 | 282 | dcbSerialParams.ByteSize = ChSize; |
|
283 | 283 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
284 | 284 | return rs232noerr; |
|
285 | 285 | } |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | |
|
289 | 289 | |
|
290 | 290 | |
|
291 | 291 | |
|
292 | 292 | |
|
293 | 293 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) |
|
294 | 294 | { |
|
295 | 295 | |
|
296 | 296 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
297 | 297 | { |
|
298 | 298 | return (rs232port_t)badPortValue; |
|
299 | 299 | } |
|
300 | 300 | else |
|
301 | 301 | { |
|
302 | 302 | DWORD dwBytesWriten = 0; |
|
303 | 303 | WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); |
|
304 | 304 | return dwBytesWriten; |
|
305 | 305 | } |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) |
|
310 | 310 | { |
|
311 | 311 | |
|
312 | 312 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
313 | 313 | { |
|
314 | 314 | return (rs232port_t)badPortValue; |
|
315 | 315 | } |
|
316 | 316 | else |
|
317 | 317 | { |
|
318 | 318 | DWORD dwBytesRead = 0; |
|
319 | 319 | ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL); |
|
320 | 320 | return dwBytesRead; |
|
321 | 321 | } |
|
322 | 322 | |
|
323 | 323 | } |
|
324 | 324 | |
|
325 | int rs232availablebytes(rs232port_t fd) | |
|
326 | { | |
|
327 | DWORD dwErrorFlags; | |
|
328 | COMSTAT ComStat; | |
|
329 | ClearCommError(m_hIDComDev, &dwErrorFlags, &ComStat); | |
|
330 | return((int)ComStat.cbInQue); | |
|
331 | } | |
|
325 | 332 | |
|
326 | 333 | int rs232saferead(rs232port_t fd,char* data,int count ) |
|
327 | 334 | { |
|
328 | 335 | int read=0; |
|
329 | 336 | int i=0; |
|
330 | 337 | for(i=0;i<100;i++) |
|
331 | 338 | { |
|
332 | 339 | read = rs232read(fd,data,count); |
|
333 | 340 | count -=read; |
|
334 | 341 | data+=read; |
|
335 | 342 | if(count==0) |
|
336 | 343 | return rs232noerr; |
|
337 | 344 | } |
|
338 | 345 | return -1; |
|
339 | 346 | } |
|
340 | 347 | |
|
341 | 348 | |
|
342 | 349 | |
|
343 | 350 | int rs232safewrite(rs232port_t fd,char* data,int count) |
|
344 | 351 | { |
|
345 | 352 | int written=0; |
|
346 | 353 | int i=0; |
|
347 | 354 | for(i=0;i<1000;i++) |
|
348 | 355 | { |
|
349 | 356 | written = rs232write(fd,data+written,count); |
|
350 | 357 | count-=written; |
|
351 | 358 | data+=written; |
|
352 | 359 | if(count==0) |
|
353 | 360 | return rs232noerr; |
|
354 | 361 | } |
|
355 | 362 | return -1; |
|
356 | 363 | } |
|
357 | 364 | |
|
358 | 365 | |
|
359 | 366 | int rs232setRTS(rs232port_t fd) |
|
360 | 367 | { |
|
361 | 368 | if(EscapeCommFunction((HANDLE)fd, CLRRTS)) |
|
362 | 369 | return rs232noerr; |
|
363 | 370 | return -1; |
|
364 | 371 | } |
|
365 | 372 | |
|
366 | 373 | int rs232clearRTS(rs232port_t fd) |
|
367 | 374 | { |
|
368 | 375 | if(EscapeCommFunction((HANDLE)fd, SETRTS)) |
|
369 | 376 | return rs232noerr; |
|
370 | 377 | return -1; |
|
371 | 378 | } |
|
372 | 379 | |
|
373 | 380 | int rs232setDTR(rs232port_t fd) |
|
374 | 381 | { |
|
375 | 382 | if(EscapeCommFunction((HANDLE)fd, CLRDTR)) |
|
376 | 383 | return rs232noerr; |
|
377 | 384 | return -1; |
|
378 | 385 | } |
|
379 | 386 | |
|
380 | 387 | |
|
381 | 388 | int rs232clearDTR(rs232port_t fd) |
|
382 | 389 | { |
|
383 | 390 | if(EscapeCommFunction((HANDLE)fd, SETDTR)) |
|
384 | 391 | return rs232noerr; |
|
385 | 392 | return -1; |
|
386 | 393 | } |
|
387 | 394 | |
|
388 | 395 |
General Comments 0
You need to be logged in to leave comments.
Login now