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