##// END OF EJS Templates
Removed error on port enumeration on win32, now the ports are correctly clsed.
jeandet -
r38:9392f8876142 alexis
parent child
Show More
@@ -1,364 +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 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(devName);
152 }
153 }
153 }
154 }
154 return firstitem;
155 return firstitem;
155 }
156 }
156
157
157 void rs232deleteportlist(rs232portslist_t* list)
158 void rs232deleteportlist(rs232portslist_t* list)
158 {
159 {
159 if(list!=NULL)
160 if(list!=NULL)
160 {
161 {
161 if(list->next != NULL)
162 if(list->next != NULL)
162 rs232deleteportlist(list->next);
163 rs232deleteportlist(list->next);
163 free(list);
164 free(list);
164 }
165 }
165 }
166 }
166
167
167
168
168 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)
169 {
170 {
170 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
171 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
171 {
172 {
172 return (rs232port_t)badPortValue;
173 return (rs232port_t)badPortValue;
173 }
174 }
174 else
175 else
175 {
176 {
176 DCB dcbSerialParams = {0};
177 DCB dcbSerialParams = {0};
177 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
178 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
178 GetCommState((HANDLE)fd, &dcbSerialParams);
179 GetCommState((HANDLE)fd, &dcbSerialParams);
179 dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
180 dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
180 dcbSerialParams.ByteSize=ChSize;
181 dcbSerialParams.ByteSize=ChSize;
181 dcbSerialParams.StopBits=privatedecodestop(NbStop);
182 dcbSerialParams.StopBits=privatedecodestop(NbStop);
182 dcbSerialParams.Parity=privatedecodeparity(Parity);
183 dcbSerialParams.Parity=privatedecodeparity(Parity);
183 SetCommState((HANDLE)fd, &dcbSerialParams);
184 SetCommState((HANDLE)fd, &dcbSerialParams);
184 COMMTIMEOUTS timeouts={0};
185 COMMTIMEOUTS timeouts={0};
185 timeouts.ReadIntervalTimeout=100;
186 timeouts.ReadIntervalTimeout=100;
186 timeouts.ReadTotalTimeoutConstant=100;
187 timeouts.ReadTotalTimeoutConstant=100;
187 timeouts.ReadTotalTimeoutMultiplier=1;
188 timeouts.ReadTotalTimeoutMultiplier=1;
188 timeouts.WriteTotalTimeoutConstant=100;
189 timeouts.WriteTotalTimeoutConstant=100;
189 timeouts.WriteTotalTimeoutMultiplier=10;
190 timeouts.WriteTotalTimeoutMultiplier=10;
190 SetCommTimeouts((HANDLE)fd, &timeouts);
191 SetCommTimeouts((HANDLE)fd, &timeouts);
191 return rs232noerr;
192 return rs232noerr;
192 }
193 }
193 }
194 }
194
195
195 int rs232setbaudrate(rs232port_t fd, int baudrate)
196 int rs232setbaudrate(rs232port_t fd, int baudrate)
196 {
197 {
197 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
198 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
198 {
199 {
199 return (rs232port_t)badPortValue;
200 return (rs232port_t)badPortValue;
200 }
201 }
201 else
202 else
202 {
203 {
203 DCB dcbSerialParams = {0};
204 DCB dcbSerialParams = {0};
204 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
205 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
205 GetCommState((HANDLE)fd, &dcbSerialParams);
206 GetCommState((HANDLE)fd, &dcbSerialParams);
206 dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
207 dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
207 SetCommState((HANDLE)fd, &dcbSerialParams);
208 SetCommState((HANDLE)fd, &dcbSerialParams);
208 return rs232noerr;
209 return rs232noerr;
209 }
210 }
210 }
211 }
211
212
212 int rs232setparity(rs232port_t fd, rs232parity Parity)
213 int rs232setparity(rs232port_t fd, rs232parity Parity)
213 {
214 {
214 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
215 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
215 {
216 {
216 return (rs232port_t)badPortValue;
217 return (rs232port_t)badPortValue;
217 }
218 }
218 else
219 else
219 {
220 {
220 DCB dcbSerialParams = {0};
221 DCB dcbSerialParams = {0};
221 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
222 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
222 GetCommState((HANDLE)fd, &dcbSerialParams);
223 GetCommState((HANDLE)fd, &dcbSerialParams);
223 dcbSerialParams.Parity = privatedecodeparity(Parity);
224 dcbSerialParams.Parity = privatedecodeparity(Parity);
224 SetCommState((HANDLE)fd, &dcbSerialParams);
225 SetCommState((HANDLE)fd, &dcbSerialParams);
225 return rs232noerr;
226 return rs232noerr;
226 }
227 }
227 }
228 }
228
229
229 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
230 int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
230 {
231 {
231 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
232 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
232 {
233 {
233 return (rs232port_t)badPortValue;
234 return (rs232port_t)badPortValue;
234 }
235 }
235 else
236 else
236 {
237 {
237 DCB dcbSerialParams = {0};
238 DCB dcbSerialParams = {0};
238 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
239 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
239 GetCommState((HANDLE)fd, &dcbSerialParams);
240 GetCommState((HANDLE)fd, &dcbSerialParams);
240 dcbSerialParams.StopBits = privatedecodestop(NbStop);
241 dcbSerialParams.StopBits = privatedecodestop(NbStop);
241 SetCommState((HANDLE)fd, &dcbSerialParams);
242 SetCommState((HANDLE)fd, &dcbSerialParams);
242 return rs232noerr;
243 return rs232noerr;
243 }
244 }
244 }
245 }
245
246
246
247
247 int rs232setcsize(rs232port_t fd, int ChSize)
248 int rs232setcsize(rs232port_t fd, int ChSize)
248 {
249 {
249 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
250 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
250 {
251 {
251 return (rs232port_t)badPortValue;
252 return (rs232port_t)badPortValue;
252 }
253 }
253 else
254 else
254 {
255 {
255 DCB dcbSerialParams = {0};
256 DCB dcbSerialParams = {0};
256 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
257 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
257 GetCommState((HANDLE)fd, &dcbSerialParams);
258 GetCommState((HANDLE)fd, &dcbSerialParams);
258 dcbSerialParams.ByteSize = ChSize;
259 dcbSerialParams.ByteSize = ChSize;
259 SetCommState((HANDLE)fd, &dcbSerialParams);
260 SetCommState((HANDLE)fd, &dcbSerialParams);
260 return rs232noerr;
261 return rs232noerr;
261 }
262 }
262 }
263 }
263
264
264
265
265
266
266
267
267
268
268
269
269 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
270 int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
270 {
271 {
271
272
272 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
273 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
273 {
274 {
274 return (rs232port_t)badPortValue;
275 return (rs232port_t)badPortValue;
275 }
276 }
276 else
277 else
277 {
278 {
278 DWORD dwBytesWriten = 0;
279 DWORD dwBytesWriten = 0;
279 WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
280 WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
280 return dwBytesWriten;
281 return dwBytesWriten;
281 }
282 }
282 }
283 }
283
284
284
285
285 int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
286 int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
286 {
287 {
287
288
288 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
289 if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
289 {
290 {
290 return (rs232port_t)badPortValue;
291 return (rs232port_t)badPortValue;
291 }
292 }
292 else
293 else
293 {
294 {
294 DWORD dwBytesRead = 0;
295 DWORD dwBytesRead = 0;
295 ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
296 ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
296 return dwBytesRead;
297 return dwBytesRead;
297 }
298 }
298
299
299 }
300 }
300
301
301
302
302 int rs232saferead(rs232port_t fd,char* data,int count )
303 int rs232saferead(rs232port_t fd,char* data,int count )
303 {
304 {
304 int read=0;
305 int read=0;
305 int i=0;
306 int i=0;
306 for(i=0;i<100;i++)
307 for(i=0;i<100;i++)
307 {
308 {
308 read = rs232read(fd,data,count);
309 read = rs232read(fd,data,count);
309 count -=read;
310 count -=read;
310 data+=read;
311 data+=read;
311 if(count==0)
312 if(count==0)
312 return rs232noerr;
313 return rs232noerr;
313 }
314 }
314 return -1;
315 return -1;
315 }
316 }
316
317
317
318
318
319
319 int rs232safewrite(rs232port_t fd,char* data,int count)
320 int rs232safewrite(rs232port_t fd,char* data,int count)
320 {
321 {
321 int written=0;
322 int written=0;
322 int i=0;
323 int i=0;
323 for(i=0;i<1000;i++)
324 for(i=0;i<1000;i++)
324 {
325 {
325 written = rs232write(fd,data+written,count);
326 written = rs232write(fd,data+written,count);
326 count-=written;
327 count-=written;
327 data+=written;
328 data+=written;
328 if(count==0)
329 if(count==0)
329 return rs232noerr;
330 return rs232noerr;
330 }
331 }
331 return -1;
332 return -1;
332 }
333 }
333
334
334
335
335 int rs232setRTS(rs232port_t fd)
336 int rs232setRTS(rs232port_t fd)
336 {
337 {
337 if(EscapeCommFunction((HANDLE)fd, CLRRTS))
338 if(EscapeCommFunction((HANDLE)fd, CLRRTS))
338 return rs232noerr;
339 return rs232noerr;
339 return -1;
340 return -1;
340 }
341 }
341
342
342 int rs232clearRTS(rs232port_t fd)
343 int rs232clearRTS(rs232port_t fd)
343 {
344 {
344 if(EscapeCommFunction((HANDLE)fd, SETRTS))
345 if(EscapeCommFunction((HANDLE)fd, SETRTS))
345 return rs232noerr;
346 return rs232noerr;
346 return -1;
347 return -1;
347 }
348 }
348
349
349 int rs232setDTR(rs232port_t fd)
350 int rs232setDTR(rs232port_t fd)
350 {
351 {
351 if(EscapeCommFunction((HANDLE)fd, CLRDTR))
352 if(EscapeCommFunction((HANDLE)fd, CLRDTR))
352 return rs232noerr;
353 return rs232noerr;
353 return -1;
354 return -1;
354 }
355 }
355
356
356
357
357 int rs232clearDTR(rs232port_t fd)
358 int rs232clearDTR(rs232port_t fd)
358 {
359 {
359 if(EscapeCommFunction((HANDLE)fd, SETDTR))
360 if(EscapeCommFunction((HANDLE)fd, SETDTR))
360 return rs232noerr;
361 return rs232noerr;
361 return -1;
362 return -1;
362 }
363 }
363
364
364 #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