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