##// END OF EJS Templates
Win32 read timeout between bytes set to a more realistic value
jeandet -
r43:a38235df29ea alexis
parent child
Show More
@@ -1,386 +1,386
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
95
96 if(BaudeRate<345600)
96 if(BaudeRate<345600)
97 return (rs232speed_t)230400;
97 return (rs232speed_t)230400;
98
98
99 if(BaudeRate<345600)
99 if(BaudeRate<345600)
100 return (rs232speed_t)460800;
100 return (rs232speed_t)460800;
101
101
102 if(BaudeRate<748800)
102 if(BaudeRate<748800)
103 return (rs232speed_t)576000;
103 return (rs232speed_t)576000;
104
104
105 if(BaudeRate<1210800)
105 if(BaudeRate<1210800)
106 return (rs232speed_t)921600;
106 return (rs232speed_t)921600;
107
107
108 if(BaudeRate<1750000)
108 if(BaudeRate<1750000)
109 return (rs232speed_t)1500000;
109 return (rs232speed_t)1500000;
110
110
111 if(BaudeRate<2250000)
111 if(BaudeRate<2250000)
112 return (rs232speed_t)2000000;
112 return (rs232speed_t)2000000;
113
113
114 if(BaudeRate<2750000)
114 if(BaudeRate<2750000)
115 return (rs232speed_t)2500000;
115 return (rs232speed_t)2500000;
116 else
116 else
117 return (rs232speed_t)3000000;
117 return (rs232speed_t)3000000;
118 }
118 }
119
119
120
120
121 rs232port_t rs232open(char* psPortName)
121 rs232port_t rs232open(char* psPortName)
122 {
122 {
123 rs232port_t fd;
123 rs232port_t fd;
124 fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
124 fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
125 #ifdef RS232_debug
125 #ifdef RS232_debug
126 if(fd==(rs232port_t)INVALID_HANDLE_VALUE)
126 if(fd==(rs232port_t)INVALID_HANDLE_VALUE)
127 {
127 {
128 printf("can't open Port\n");
128 printf("can't open Port\n");
129 return (rs232port_t)badPortValue;
129 return (rs232port_t)badPortValue;
130 }
130 }
131 #endif
131 #endif
132 return fd;
132 return fd;
133 }
133 }
134
134
135 int rs232close(rs232port_t fd)
135 int rs232close(rs232port_t fd)
136 {
136 {
137 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
137 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
138 {
138 {
139 return (rs232port_t)badPortValue;
139 return (rs232port_t)badPortValue;
140 }
140 }
141 else
141 else
142 {
142 {
143 CloseHandle((HANDLE)fd);
143 CloseHandle((HANDLE)fd);
144 return rs232noerr;
144 return rs232noerr;
145 }
145 }
146 }
146 }
147
147
148 rs232portslist_t* rs232getportlist() //here is the very dirty way!
148 rs232portslist_t* rs232getportlist() //here is the very dirty way!
149 {
149 {
150 int i=0;
150 int i=0;
151 char devName[]="COM111";
151 char devName[]="COM111";
152 rs232port_t testport;
152 rs232port_t testport;
153 rs232portslist_t* firstitem=NULL;
153 rs232portslist_t* firstitem=NULL;
154 rs232portslist_t* previtem=NULL;
154 rs232portslist_t* previtem=NULL;
155 for(i=0;i<256;i++)
155 for(i=0;i<256;i++)
156 {
156 {
157 devName[3] = '\0';
157 devName[3] = '\0';
158 devName[4] = '\0';
158 devName[4] = '\0';
159 devName[5] = '\0';
159 devName[5] = '\0';
160 devName[6] = '\0';
160 devName[6] = '\0';
161 sprintf(devName+3,"%d",i);
161 sprintf(devName+3,"%d",i);
162 testport= rs232open(devName);
162 testport= rs232open(devName);
163 if(testport != (rs232port_t)badPortValue)
163 if(testport != (rs232port_t)badPortValue)
164 {
164 {
165 rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t));
165 rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t));
166 char* name = (char*)malloc(7);
166 char* name = (char*)malloc(7);
167 strcpy(name,devName);
167 strcpy(name,devName);
168 item->name = name;
168 item->name = name;
169 item->next = NULL;
169 item->next = NULL;
170 if(NULL!=previtem)previtem->next = item;
170 if(NULL!=previtem)previtem->next = item;
171 previtem = item;
171 previtem = item;
172 if(NULL==firstitem)firstitem = item;
172 if(NULL==firstitem)firstitem = item;
173 rs232close(testport);
173 rs232close(testport);
174 }
174 }
175 }
175 }
176 return firstitem;
176 return firstitem;
177 }
177 }
178
178
179 void rs232deleteportlist(rs232portslist_t* list)
179 void rs232deleteportlist(rs232portslist_t* list)
180 {
180 {
181 if(list!=NULL)
181 if(list!=NULL)
182 {
182 {
183 if(list->next != NULL)
183 if(list->next != NULL)
184 rs232deleteportlist(list->next);
184 rs232deleteportlist(list->next);
185 free(list);
185 free(list);
186 }
186 }
187 }
187 }
188
188
189
189
190 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
190 int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
191 {
191 {
192 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
192 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
193 {
193 {
194 return (rs232port_t)badPortValue;
194 return (rs232port_t)badPortValue;
195 }
195 }
196 else
196 else
197 {
197 {
198 DCB dcbSerialParams = {0};
198 DCB dcbSerialParams = {0};
199 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
199 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
200 GetCommState((HANDLE)fd, &dcbSerialParams);
200 GetCommState((HANDLE)fd, &dcbSerialParams);
201 dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
201 dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
202 dcbSerialParams.ByteSize=ChSize;
202 dcbSerialParams.ByteSize=ChSize;
203 dcbSerialParams.StopBits=privatedecodestop(NbStop);
203 dcbSerialParams.StopBits=privatedecodestop(NbStop);
204 dcbSerialParams.Parity=privatedecodeparity(Parity);
204 dcbSerialParams.Parity=privatedecodeparity(Parity);
205 SetCommState((HANDLE)fd, &dcbSerialParams);
205 SetCommState((HANDLE)fd, &dcbSerialParams);
206 COMMTIMEOUTS timeouts={0};
206 COMMTIMEOUTS timeouts={0};
207 timeouts.ReadIntervalTimeout=100;
207 timeouts.ReadIntervalTimeout=5;
208 timeouts.ReadTotalTimeoutConstant=100;
208 timeouts.ReadTotalTimeoutConstant=100;
209 timeouts.ReadTotalTimeoutMultiplier=1;
209 timeouts.ReadTotalTimeoutMultiplier=1;
210 timeouts.WriteTotalTimeoutConstant=100;
210 timeouts.WriteTotalTimeoutConstant=100;
211 timeouts.WriteTotalTimeoutMultiplier=10;
211 timeouts.WriteTotalTimeoutMultiplier=10;
212 SetCommTimeouts((HANDLE)fd, &timeouts);
212 SetCommTimeouts((HANDLE)fd, &timeouts);
213 return rs232noerr;
213 return rs232noerr;
214 }
214 }
215 }
215 }
216
216
217 int rs232setbaudrate(rs232port_t fd, int baudrate)
217 int rs232setbaudrate(rs232port_t fd, int baudrate)
218 {
218 {
219 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
219 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
220 {
220 {
221 return (rs232port_t)badPortValue;
221 return (rs232port_t)badPortValue;
222 }
222 }
223 else
223 else
224 {
224 {
225 DCB dcbSerialParams = {0};
225 DCB dcbSerialParams = {0};
226 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
226 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
227 GetCommState((HANDLE)fd, &dcbSerialParams);
227 GetCommState((HANDLE)fd, &dcbSerialParams);
228 dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
228 dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
229 SetCommState((HANDLE)fd, &dcbSerialParams);
229 SetCommState((HANDLE)fd, &dcbSerialParams);
230 return rs232noerr;
230 return rs232noerr;
231 }
231 }
232 }
232 }
233
233
234 int rs232setparity(rs232port_t fd, rs232parity Parity)
234 int rs232setparity(rs232port_t fd, rs232parity Parity)
235 {
235 {
236 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
236 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
237 {
237 {
238 return (rs232port_t)badPortValue;
238 return (rs232port_t)badPortValue;
239 }
239 }
240 else
240 else
241 {
241 {
242 DCB dcbSerialParams = {0};
242 DCB dcbSerialParams = {0};
243 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
243 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
244 GetCommState((HANDLE)fd, &dcbSerialParams);
244 GetCommState((HANDLE)fd, &dcbSerialParams);
245 dcbSerialParams.Parity = privatedecodeparity(Parity);
245 dcbSerialParams.Parity = privatedecodeparity(Parity);
246 SetCommState((HANDLE)fd, &dcbSerialParams);
246 SetCommState((HANDLE)fd, &dcbSerialParams);
247 return rs232noerr;
247 return rs232noerr;
248 }
248 }
249 }
249 }
250
250
251 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
251 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
252 {
252 {
253 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
253 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
254 {
254 {
255 return (rs232port_t)badPortValue;
255 return (rs232port_t)badPortValue;
256 }
256 }
257 else
257 else
258 {
258 {
259 DCB dcbSerialParams = {0};
259 DCB dcbSerialParams = {0};
260 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
260 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
261 GetCommState((HANDLE)fd, &dcbSerialParams);
261 GetCommState((HANDLE)fd, &dcbSerialParams);
262 dcbSerialParams.StopBits = privatedecodestop(NbStop);
262 dcbSerialParams.StopBits = privatedecodestop(NbStop);
263 SetCommState((HANDLE)fd, &dcbSerialParams);
263 SetCommState((HANDLE)fd, &dcbSerialParams);
264 return rs232noerr;
264 return rs232noerr;
265 }
265 }
266 }
266 }
267
267
268
268
269 int rs232setcsize(rs232port_t fd, int ChSize)
269 int rs232setcsize(rs232port_t fd, int ChSize)
270 {
270 {
271 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
271 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
272 {
272 {
273 return (rs232port_t)badPortValue;
273 return (rs232port_t)badPortValue;
274 }
274 }
275 else
275 else
276 {
276 {
277 DCB dcbSerialParams = {0};
277 DCB dcbSerialParams = {0};
278 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
278 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
279 GetCommState((HANDLE)fd, &dcbSerialParams);
279 GetCommState((HANDLE)fd, &dcbSerialParams);
280 dcbSerialParams.ByteSize = ChSize;
280 dcbSerialParams.ByteSize = ChSize;
281 SetCommState((HANDLE)fd, &dcbSerialParams);
281 SetCommState((HANDLE)fd, &dcbSerialParams);
282 return rs232noerr;
282 return rs232noerr;
283 }
283 }
284 }
284 }
285
285
286
286
287
287
288
288
289
289
290
290
291 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
291 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
292 {
292 {
293
293
294 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
294 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
295 {
295 {
296 return (rs232port_t)badPortValue;
296 return (rs232port_t)badPortValue;
297 }
297 }
298 else
298 else
299 {
299 {
300 DWORD dwBytesWriten = 0;
300 DWORD dwBytesWriten = 0;
301 WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
301 WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
302 return dwBytesWriten;
302 return dwBytesWriten;
303 }
303 }
304 }
304 }
305
305
306
306
307 int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
307 int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
308 {
308 {
309
309
310 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
310 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
311 {
311 {
312 return (rs232port_t)badPortValue;
312 return (rs232port_t)badPortValue;
313 }
313 }
314 else
314 else
315 {
315 {
316 DWORD dwBytesRead = 0;
316 DWORD dwBytesRead = 0;
317 ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
317 ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
318 return dwBytesRead;
318 return dwBytesRead;
319 }
319 }
320
320
321 }
321 }
322
322
323
323
324 int rs232saferead(rs232port_t fd,char* data,int count )
324 int rs232saferead(rs232port_t fd,char* data,int count )
325 {
325 {
326 int read=0;
326 int read=0;
327 int i=0;
327 int i=0;
328 for(i=0;i<100;i++)
328 for(i=0;i<100;i++)
329 {
329 {
330 read = rs232read(fd,data,count);
330 read = rs232read(fd,data,count);
331 count -=read;
331 count -=read;
332 data+=read;
332 data+=read;
333 if(count==0)
333 if(count==0)
334 return rs232noerr;
334 return rs232noerr;
335 }
335 }
336 return -1;
336 return -1;
337 }
337 }
338
338
339
339
340
340
341 int rs232safewrite(rs232port_t fd,char* data,int count)
341 int rs232safewrite(rs232port_t fd,char* data,int count)
342 {
342 {
343 int written=0;
343 int written=0;
344 int i=0;
344 int i=0;
345 for(i=0;i<1000;i++)
345 for(i=0;i<1000;i++)
346 {
346 {
347 written = rs232write(fd,data+written,count);
347 written = rs232write(fd,data+written,count);
348 count-=written;
348 count-=written;
349 data+=written;
349 data+=written;
350 if(count==0)
350 if(count==0)
351 return rs232noerr;
351 return rs232noerr;
352 }
352 }
353 return -1;
353 return -1;
354 }
354 }
355
355
356
356
357 int rs232setRTS(rs232port_t fd)
357 int rs232setRTS(rs232port_t fd)
358 {
358 {
359 if(EscapeCommFunction((HANDLE)fd, CLRRTS))
359 if(EscapeCommFunction((HANDLE)fd, CLRRTS))
360 return rs232noerr;
360 return rs232noerr;
361 return -1;
361 return -1;
362 }
362 }
363
363
364 int rs232clearRTS(rs232port_t fd)
364 int rs232clearRTS(rs232port_t fd)
365 {
365 {
366 if(EscapeCommFunction((HANDLE)fd, SETRTS))
366 if(EscapeCommFunction((HANDLE)fd, SETRTS))
367 return rs232noerr;
367 return rs232noerr;
368 return -1;
368 return -1;
369 }
369 }
370
370
371 int rs232setDTR(rs232port_t fd)
371 int rs232setDTR(rs232port_t fd)
372 {
372 {
373 if(EscapeCommFunction((HANDLE)fd, CLRDTR))
373 if(EscapeCommFunction((HANDLE)fd, CLRDTR))
374 return rs232noerr;
374 return rs232noerr;
375 return -1;
375 return -1;
376 }
376 }
377
377
378
378
379 int rs232clearDTR(rs232port_t fd)
379 int rs232clearDTR(rs232port_t fd)
380 {
380 {
381 if(EscapeCommFunction((HANDLE)fd, SETDTR))
381 if(EscapeCommFunction((HANDLE)fd, SETDTR))
382 return rs232noerr;
382 return rs232noerr;
383 return -1;
383 return -1;
384 }
384 }
385
385
386 #endif //#ifdef HAVE_WINDOWS_H
386 #endif //#ifdef HAVE_WINDOWS_H
General Comments 0
You need to be logged in to leave comments. Login now