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