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