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