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