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