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