@@ -1,365 +1,386 | |||
|
1 | 1 | #include "rs232config.h" |
|
2 | 2 | #include <stdio.h> |
|
3 | 3 | #include <unistd.h> |
|
4 | 4 | #include <fcntl.h> |
|
5 | 5 | #include <string.h> |
|
6 | 6 | #include <errno.h> |
|
7 | 7 | #include <malloc.h> |
|
8 | 8 | |
|
9 | 9 | #ifdef HAVE_WINDOWS_H |
|
10 | 10 | #include <windows.h> |
|
11 | 11 | #endif |
|
12 | 12 | #include "RS232.h" |
|
13 | 13 | |
|
14 | 14 | #ifdef HAVE_WINDOWS_H |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | int privatedecodeparity(rs232parity Parity ) |
|
18 | 18 | { |
|
19 | 19 | switch(Parity) |
|
20 | 20 | { |
|
21 | 21 | case rs232parityNo: |
|
22 | 22 | return NOPARITY; |
|
23 | 23 | break; |
|
24 | 24 | case rs232parityOdd: |
|
25 | 25 | return ODDPARITY; |
|
26 | 26 | break; |
|
27 | 27 | case rs232parityEven: |
|
28 | 28 | return EVENPARITY; |
|
29 | 29 | break; |
|
30 | 30 | default: |
|
31 | 31 | return NOPARITY; |
|
32 | 32 | break; |
|
33 | 33 | } |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | int privatedecodestop(rs232stop NbStop) |
|
37 | 37 | { |
|
38 | 38 | switch(NbStop) |
|
39 | 39 | { |
|
40 | 40 | case rs232OneStop: |
|
41 | 41 | return ONESTOPBIT; |
|
42 | 42 | break; |
|
43 | 43 | case rs232One5Stop: |
|
44 | 44 | return ONE5STOPBITS; |
|
45 | 45 | break; |
|
46 | 46 | case rs232TwoStop: |
|
47 | 47 | return TWOSTOPBITS; |
|
48 | 48 | break; |
|
49 | 49 | default: |
|
50 | 50 | return ONESTOPBIT; |
|
51 | 51 | break; |
|
52 | 52 | } |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) |
|
58 | 58 | { |
|
59 | 59 | |
|
60 | 60 | if(BaudeRate<123) |
|
61 | 61 | return (rs232speed_t)CBR_110; |
|
62 | 62 | |
|
63 | 63 | if(BaudeRate<450) |
|
64 | 64 | return (rs232speed_t)CBR_300; |
|
65 | 65 | |
|
66 | 66 | if(BaudeRate<900) |
|
67 | 67 | return (rs232speed_t)CBR_600; |
|
68 | 68 | |
|
69 | 69 | if(BaudeRate<1500) |
|
70 | 70 | return (rs232speed_t)CBR_1200; |
|
71 | 71 | |
|
72 | 72 | if(BaudeRate<3600) |
|
73 | 73 | return (rs232speed_t)CBR_2400; |
|
74 | 74 | |
|
75 | 75 | if(BaudeRate<7200) |
|
76 | 76 | return (rs232speed_t)CBR_4800; |
|
77 | 77 | |
|
78 | 78 | if(BaudeRate<14000) |
|
79 | 79 | return (rs232speed_t)CBR_9600; |
|
80 | 80 | |
|
81 | 81 | if(BaudeRate<16800) |
|
82 | 82 | return (rs232speed_t)CBR_14400; |
|
83 | 83 | |
|
84 | 84 | if(BaudeRate<28800) |
|
85 | 85 | return (rs232speed_t)CBR_19200; |
|
86 | 86 | |
|
87 | 87 | if(BaudeRate<48000) |
|
88 | 88 | return (rs232speed_t)CBR_38400; |
|
89 | 89 | |
|
90 | 90 | if(BaudeRate<86400) |
|
91 | 91 | return (rs232speed_t)CBR_57600; |
|
92 | 92 | |
|
93 | 93 | if(BaudeRate<172800) |
|
94 | 94 | return (rs232speed_t)CBR_115200; |
|
95 | ||
|
96 | if(BaudeRate<345600) | |
|
97 | return (rs232speed_t)230400; | |
|
98 | ||
|
99 | if(BaudeRate<345600) | |
|
100 | return (rs232speed_t)460800; | |
|
101 | ||
|
102 | if(BaudeRate<748800) | |
|
103 | return (rs232speed_t)576000; | |
|
104 | ||
|
105 | if(BaudeRate<1210800) | |
|
106 | return (rs232speed_t)921600; | |
|
107 | ||
|
108 | if(BaudeRate<1750000) | |
|
109 | return (rs232speed_t)1500000; | |
|
110 | ||
|
111 | if(BaudeRate<2250000) | |
|
112 | return (rs232speed_t)2000000; | |
|
113 | ||
|
114 | if(BaudeRate<2750000) | |
|
115 | return (rs232speed_t)2500000; | |
|
95 | 116 | else |
|
96 |
return (rs232speed_t) |
|
|
117 | return (rs232speed_t)3000000; | |
|
97 | 118 | } |
|
98 | 119 | |
|
99 | 120 | |
|
100 | 121 | rs232port_t rs232open(char* psPortName) |
|
101 | 122 | { |
|
102 | 123 | rs232port_t fd; |
|
103 | 124 | fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); |
|
104 | 125 | #ifdef RS232_debug |
|
105 | 126 | if(fd==(rs232port_t)INVALID_HANDLE_VALUE) |
|
106 | 127 | { |
|
107 | 128 | printf("can't open Port\n"); |
|
108 | 129 | return (rs232port_t)badPortValue; |
|
109 | 130 | } |
|
110 | 131 | #endif |
|
111 | 132 | return fd; |
|
112 | 133 | } |
|
113 | 134 | |
|
114 | 135 | int rs232close(rs232port_t fd) |
|
115 | 136 | { |
|
116 | 137 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
117 | 138 | { |
|
118 | 139 | return (rs232port_t)badPortValue; |
|
119 | 140 | } |
|
120 | 141 | else |
|
121 | 142 | { |
|
122 | 143 | CloseHandle((HANDLE)fd); |
|
123 | 144 | return rs232noerr; |
|
124 | 145 | } |
|
125 | 146 | } |
|
126 | 147 | |
|
127 | 148 | rs232portslist_t* rs232getportlist() //here is the very dirty way! |
|
128 | 149 | { |
|
129 | 150 | int i=0; |
|
130 | 151 | char devName[]="COM111"; |
|
131 | 152 | rs232port_t testport; |
|
132 | 153 | rs232portslist_t* firstitem=NULL; |
|
133 | 154 | rs232portslist_t* previtem=NULL; |
|
134 | 155 | for(i=0;i<256;i++) |
|
135 | 156 | { |
|
136 | 157 | devName[3] = '\0'; |
|
137 | 158 | devName[4] = '\0'; |
|
138 | 159 | devName[5] = '\0'; |
|
139 | 160 | devName[6] = '\0'; |
|
140 | 161 | sprintf(devName+3,"%d",i); |
|
141 | 162 | testport= rs232open(devName); |
|
142 | 163 | if(testport != (rs232port_t)badPortValue) |
|
143 | 164 | { |
|
144 | 165 | rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); |
|
145 | 166 | char* name = (char*)malloc(7); |
|
146 | 167 | strcpy(name,devName); |
|
147 | 168 | item->name = name; |
|
148 | 169 | item->next = NULL; |
|
149 | 170 | if(NULL!=previtem)previtem->next = item; |
|
150 | 171 | previtem = item; |
|
151 | 172 | if(NULL==firstitem)firstitem = item; |
|
152 | 173 | rs232close(testport); |
|
153 | 174 | } |
|
154 | 175 | } |
|
155 | 176 | return firstitem; |
|
156 | 177 | } |
|
157 | 178 | |
|
158 | 179 | void rs232deleteportlist(rs232portslist_t* list) |
|
159 | 180 | { |
|
160 | 181 | if(list!=NULL) |
|
161 | 182 | { |
|
162 | 183 | if(list->next != NULL) |
|
163 | 184 | rs232deleteportlist(list->next); |
|
164 | 185 | free(list); |
|
165 | 186 | } |
|
166 | 187 | } |
|
167 | 188 | |
|
168 | 189 | |
|
169 | 190 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) |
|
170 | 191 | { |
|
171 | 192 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
172 | 193 | { |
|
173 | 194 | return (rs232port_t)badPortValue; |
|
174 | 195 | } |
|
175 | 196 | else |
|
176 | 197 | { |
|
177 | 198 | DCB dcbSerialParams = {0}; |
|
178 | 199 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
179 | 200 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
180 | 201 | dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); |
|
181 | 202 | dcbSerialParams.ByteSize=ChSize; |
|
182 | 203 | dcbSerialParams.StopBits=privatedecodestop(NbStop); |
|
183 | 204 | dcbSerialParams.Parity=privatedecodeparity(Parity); |
|
184 | 205 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
185 | 206 | COMMTIMEOUTS timeouts={0}; |
|
186 | 207 | timeouts.ReadIntervalTimeout=100; |
|
187 | 208 | timeouts.ReadTotalTimeoutConstant=100; |
|
188 | 209 | timeouts.ReadTotalTimeoutMultiplier=1; |
|
189 | 210 | timeouts.WriteTotalTimeoutConstant=100; |
|
190 | 211 | timeouts.WriteTotalTimeoutMultiplier=10; |
|
191 | 212 | SetCommTimeouts((HANDLE)fd, &timeouts); |
|
192 | 213 | return rs232noerr; |
|
193 | 214 | } |
|
194 | 215 | } |
|
195 | 216 | |
|
196 | 217 | int rs232setbaudrate(rs232port_t fd, int baudrate) |
|
197 | 218 | { |
|
198 | 219 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
199 | 220 | { |
|
200 | 221 | return (rs232port_t)badPortValue; |
|
201 | 222 | } |
|
202 | 223 | else |
|
203 | 224 | { |
|
204 | 225 | DCB dcbSerialParams = {0}; |
|
205 | 226 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
206 | 227 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
207 | 228 | dcbSerialParams.BaudRate=rs232cfspeed(baudrate); |
|
208 | 229 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
209 | 230 | return rs232noerr; |
|
210 | 231 | } |
|
211 | 232 | } |
|
212 | 233 | |
|
213 | 234 | int rs232setparity(rs232port_t fd, rs232parity Parity) |
|
214 | 235 | { |
|
215 | 236 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
216 | 237 | { |
|
217 | 238 | return (rs232port_t)badPortValue; |
|
218 | 239 | } |
|
219 | 240 | else |
|
220 | 241 | { |
|
221 | 242 | DCB dcbSerialParams = {0}; |
|
222 | 243 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
223 | 244 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
224 | 245 | dcbSerialParams.Parity = privatedecodeparity(Parity); |
|
225 | 246 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
226 | 247 | return rs232noerr; |
|
227 | 248 | } |
|
228 | 249 | } |
|
229 | 250 | |
|
230 | 251 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) |
|
231 | 252 | { |
|
232 | 253 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
233 | 254 | { |
|
234 | 255 | return (rs232port_t)badPortValue; |
|
235 | 256 | } |
|
236 | 257 | else |
|
237 | 258 | { |
|
238 | 259 | DCB dcbSerialParams = {0}; |
|
239 | 260 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
240 | 261 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
241 | 262 | dcbSerialParams.StopBits = privatedecodestop(NbStop); |
|
242 | 263 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
243 | 264 | return rs232noerr; |
|
244 | 265 | } |
|
245 | 266 | } |
|
246 | 267 | |
|
247 | 268 | |
|
248 | 269 | int rs232setcsize(rs232port_t fd, int ChSize) |
|
249 | 270 | { |
|
250 | 271 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
251 | 272 | { |
|
252 | 273 | return (rs232port_t)badPortValue; |
|
253 | 274 | } |
|
254 | 275 | else |
|
255 | 276 | { |
|
256 | 277 | DCB dcbSerialParams = {0}; |
|
257 | 278 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
258 | 279 | GetCommState((HANDLE)fd, &dcbSerialParams); |
|
259 | 280 | dcbSerialParams.ByteSize = ChSize; |
|
260 | 281 | SetCommState((HANDLE)fd, &dcbSerialParams); |
|
261 | 282 | return rs232noerr; |
|
262 | 283 | } |
|
263 | 284 | } |
|
264 | 285 | |
|
265 | 286 | |
|
266 | 287 | |
|
267 | 288 | |
|
268 | 289 | |
|
269 | 290 | |
|
270 | 291 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) |
|
271 | 292 | { |
|
272 | 293 | |
|
273 | 294 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
274 | 295 | { |
|
275 | 296 | return (rs232port_t)badPortValue; |
|
276 | 297 | } |
|
277 | 298 | else |
|
278 | 299 | { |
|
279 | 300 | DWORD dwBytesWriten = 0; |
|
280 | 301 | WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); |
|
281 | 302 | return dwBytesWriten; |
|
282 | 303 | } |
|
283 | 304 | } |
|
284 | 305 | |
|
285 | 306 | |
|
286 | 307 | int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) |
|
287 | 308 | { |
|
288 | 309 | |
|
289 | 310 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) |
|
290 | 311 | { |
|
291 | 312 | return (rs232port_t)badPortValue; |
|
292 | 313 | } |
|
293 | 314 | else |
|
294 | 315 | { |
|
295 | 316 | DWORD dwBytesRead = 0; |
|
296 | 317 | ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL); |
|
297 | 318 | return dwBytesRead; |
|
298 | 319 | } |
|
299 | 320 | |
|
300 | 321 | } |
|
301 | 322 | |
|
302 | 323 | |
|
303 | 324 | int rs232saferead(rs232port_t fd,char* data,int count ) |
|
304 | 325 | { |
|
305 | 326 | int read=0; |
|
306 | 327 | int i=0; |
|
307 | 328 | for(i=0;i<100;i++) |
|
308 | 329 | { |
|
309 | 330 | read = rs232read(fd,data,count); |
|
310 | 331 | count -=read; |
|
311 | 332 | data+=read; |
|
312 | 333 | if(count==0) |
|
313 | 334 | return rs232noerr; |
|
314 | 335 | } |
|
315 | 336 | return -1; |
|
316 | 337 | } |
|
317 | 338 | |
|
318 | 339 | |
|
319 | 340 | |
|
320 | 341 | int rs232safewrite(rs232port_t fd,char* data,int count) |
|
321 | 342 | { |
|
322 | 343 | int written=0; |
|
323 | 344 | int i=0; |
|
324 | 345 | for(i=0;i<1000;i++) |
|
325 | 346 | { |
|
326 | 347 | written = rs232write(fd,data+written,count); |
|
327 | 348 | count-=written; |
|
328 | 349 | data+=written; |
|
329 | 350 | if(count==0) |
|
330 | 351 | return rs232noerr; |
|
331 | 352 | } |
|
332 | 353 | return -1; |
|
333 | 354 | } |
|
334 | 355 | |
|
335 | 356 | |
|
336 | 357 | int rs232setRTS(rs232port_t fd) |
|
337 | 358 | { |
|
338 | 359 | if(EscapeCommFunction((HANDLE)fd, CLRRTS)) |
|
339 | 360 | return rs232noerr; |
|
340 | 361 | return -1; |
|
341 | 362 | } |
|
342 | 363 | |
|
343 | 364 | int rs232clearRTS(rs232port_t fd) |
|
344 | 365 | { |
|
345 | 366 | if(EscapeCommFunction((HANDLE)fd, SETRTS)) |
|
346 | 367 | return rs232noerr; |
|
347 | 368 | return -1; |
|
348 | 369 | } |
|
349 | 370 | |
|
350 | 371 | int rs232setDTR(rs232port_t fd) |
|
351 | 372 | { |
|
352 | 373 | if(EscapeCommFunction((HANDLE)fd, CLRDTR)) |
|
353 | 374 | return rs232noerr; |
|
354 | 375 | return -1; |
|
355 | 376 | } |
|
356 | 377 | |
|
357 | 378 | |
|
358 | 379 | int rs232clearDTR(rs232port_t fd) |
|
359 | 380 | { |
|
360 | 381 | if(EscapeCommFunction((HANDLE)fd, SETDTR)) |
|
361 | 382 | return rs232noerr; |
|
362 | 383 | return -1; |
|
363 | 384 | } |
|
364 | 385 | |
|
365 | 386 | #endif //#ifdef HAVE_WINDOWS_H |
General Comments 0
You need to be logged in to leave comments.
Login now