Rev Author Line No. Line
2066 kakl 1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 * File Name : rs232.cpp
3 * Author : MCD Application Team
4 * Version : v2.2.0
5 * Date : 05/03/2010
6 * Description : Implements the RS232 class for COM communication
7 ********************************************************************************
8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/
15  
16 #include <windows.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include "rs232.h"
20  
21  
22  
23  
24 /// set serial communication over COM1 with 115200 Bauds, 8 bitand no parity.
25 CRS232::CRS232()
26 {
27 hcom = NULL;
28 bufferSize = 2048;
29  
30 numPort = 1;
31 speedInBaud = 115200;
32 nbBit = 8;
33 parity = 2;
34 nbStopBit = 1;
35  
36 isConnected = FALSE;
37 bEcho =0;
38 FlowControl = FALSE;
39 }
40 CRS232::~CRS232()
41 {
42 if(hcom != NULL)
43 closeCom();
44 }
45  
46  
47 void CRS232::SetComSettings(int _numPort, long _speedInBaud, int _nbBit, int _parity, float _nbStopBit)
48 {
49 numPort = _numPort;
50 speedInBaud = _speedInBaud;
51 nbBit = _nbBit;
52 parity = _parity;
53 nbStopBit = _nbStopBit;
54 }
55  
56  
57  
58 bool CRS232::open()
59 {
60  
61 char buf[] = "\\\\.\\COM1";
62  
63  
64 if(numPort<1 || numPort>999)
65 return false;
66  
67 if(speedInBaud<1)
68 return false;
69  
70 if(nbBit<5 || nbBit > 9)
71 return false;
72  
73 if(parity<0 || parity > 2)
74 return false;
75  
76 if(nbStopBit<1 || nbStopBit > 2)
77 return false;
78  
79  
80  
81 itoa(numPort, &buf[7], 10);
82  
83  
84 hcom=CreateFile(buf, GENERIC_READ | GENERIC_WRITE , 0, NULL, OPEN_EXISTING , 0, NULL);
85  
86  
87 if (hcom==0 || hcom==INVALID_HANDLE_VALUE)
88 return false;
89  
90 isConnected = TRUE;
91  
92  
93 setTimeOut(5000);
94  
95  
96 if ( !SetupComm(hcom, bufferSize, bufferSize) )
97 return false;
98  
99  
100 if ( !GetCommState(hcom, &dcb))
101 return false;
102  
103  
104 dcb.BaudRate = speedInBaud;
105  
106  
107 dcb.ByteSize = nbBit;
108  
109  
110 if(nbStopBit == 1)
111 dcb.StopBits = ONESTOPBIT;
112 if(nbStopBit == 1.5)
113 dcb.StopBits = ONE5STOPBITS;
114 if(nbStopBit == 2)
115 dcb.StopBits = TWOSTOPBITS;
116  
117  
118 if(parity == 0)
119 dcb.Parity = NOPARITY;
120 if(parity == 1)
121 dcb.Parity = ODDPARITY;
122 if(parity == 2)
123 dcb.Parity = EVENPARITY;
124  
125  
126  
127 if ( FlowControl == true)
128 {
129 dcb.fDtrControl = DTR_CONTROL_ENABLE;
130 dcb.fRtsControl = RTS_CONTROL_ENABLE;
131 }
132 else
133 {
134  
135 dcb.fDtrControl = DTR_CONTROL_DISABLE;
136 dcb.fRtsControl = RTS_CONTROL_DISABLE;
137  
138 }
139  
140  
141  
142 if (!SetCommState(hcom, &dcb))
143 return false;
144 else
145 return true;
146  
147  
148  
149 }
150  
151 void CRS232::closeCom()
152 {
153 CloseHandle(hcom);
154 hcom = NULL;
155 isConnected = FALSE;
156 }
157  
158 bool CRS232::setTimeOut(DWORD ms)
159 {
160  
161 if( ms<0)
162 return false;
163  
164 ct.ReadIntervalTimeout = ms;
165 ct.ReadTotalTimeoutMultiplier = ms;
166 ct.ReadTotalTimeoutConstant = ms;
167 ct.WriteTotalTimeoutMultiplier = ms;
168 ct.WriteTotalTimeoutConstant = ms;
169 if ( !SetCommTimeouts(hcom, &ct) )
170 return false;
171  
172 return false;
173 //MSDN: The SetCommTimeouts function sets the time-out parameters for all read and write operations on a specified communications device.
174 }
175  
176 bool CRS232::setSpeed(DWORD baudrate)
177 {
178  
179 if( baudrate<1)
180 return false;
181  
182  
183 if (!GetCommState(hcom, &dcb))
184 return FALSE;
185  
186 dcb.BaudRate = baudrate;
187  
188 if (!SetCommState(hcom, &dcb))
189 return FALSE;
190 else
191 return TRUE;
192  
193  
194  
195  
196 //MSDN: The SetCommState function configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues.
197 }
198  
199 int CRS232::sendData(string* data)
200 {
201  
202 if( data == NULL )
203 return false;
204  
205 return sendData((DWORD)data->size(), (LPBYTE)data->data());
206 }
207  
208 int CRS232::sendData(DWORD lg, LPBYTE data)
209 {
210 DWORD result=0;
211 DWORD result1=0;
212 DWORD counter =0;
213  
214  
215 if( lg<0 || data==NULL)
216 return false;
217  
218  
219  
220 if ( bEcho == 2)
221 {
222  
223  
224 for ( counter =0 ; counter < lg ; counter ++)
225  
226 {
227  
228  
229 if ( !WriteFile(hcom, data+counter, 1, &result1, 0) )
230 return -1;
231  
232 if( lg<0 || data==NULL)
233 return false;
234  
235  
236 if (!ReadFile(hcom, data+counter, 1, &result, 0))
237 return -1;
238  
239 }
240  
241 return (counter);
242  
243  
244  
245 }
246  
247 else
248 {
249  
250 if ( !WriteFile(hcom, data, lg, &result, 0) )
251 return -1;
252 else
253 return (int)result;
254 }
255  
256  
257  
258  
259 //MSDN: The WriteFile function writes data to a file and is designed for both synchronous
260 // and asynchronous operation. The function starts writing data to the file at the
261 // position indicated by the file pointer. After the write operation has been completed
262 // , the file pointer is adjusted by the number of bytes actually written, except when
263 // the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for
264 // overlapped input and output (I/O), the application must adjust the position of the
265 // file pointer after the write operation is finished.
266 // This function is designed for both synchronous and asynchronous operation.
267 // The WriteFileEx function is designed solely for asynchronous operation.
268 // It lets an application perform other processing during a file write operation.
269  
270 }
271  
272 int CRS232::receiveData(string* data)
273 { char buffer[1025];
274 int nbChar=0;
275  
276  
277 if( data==NULL)
278 return false;
279  
280 nbChar = receiveData(1024, (LPBYTE)buffer);
281 buffer[nbChar] = 0;
282 data->assign(buffer);
283 return nbChar;
284 }
285  
286 int CRS232::receiveData(DWORD lg, LPBYTE data)
287 {
288 DWORD result=0;
289 DWORD result1=0;
290 DWORD counter =0;
291  
292  
293 if( lg<0 || data==NULL)
294 return false;
295  
296  
297 if ( bEcho == 0)
298 {
299 if (!ReadFile(hcom, data, lg, &result, 0))
300 return -1;
301 else
302 return (int)result;
303 }
304  
305  
306 else if ( bEcho == 1)
307 {
308  
309 for ( counter =0 ; counter < lg ; counter ++)
310  
311 {
312  
313  
314 if (!ReadFile(hcom, data+counter, 1, &result, 0))
315 return -1;
316  
317 if( lg<0 || data==NULL)
318 return false;
319  
320 if ( !WriteFile(hcom, data+counter, 1, &result1, 0) )
321 return -1;
322  
323 }
324  
325 return (counter);
326  
327  
328 }
329  
330 else if ( bEcho == 2)
331 {
332 if (!ReadFile(hcom, data, lg, &result, 0))
333 return -1;
334 else
335 return (int)result;
336 }
337  
338  
339 else
340 {
341  
342 /* TODO */
343  
344 return -1;
345  
346 }
347  
348  
349  
350 //MSDN: The ReadFile function reads data from a file, starting at the position indicated
351 // by the file pointer. After the read operation has been completed, the file pointer
352 // is adjusted by the number of bytes actually read, unless the file handle is
353 // created with the overlapped attribute. If the file handle is created for
354 // overlapped input and output (I/O), the application must adjust the position of
355 // the file pointer after the read operation.
356 // This function is designed for both synchronous and asynchronous operation.
357 // The ReadFileEx function is designed solely for asynchronous operation. It lets
358 // an application perform other processing during a file read operation.
359  
360 }
361  
362 /**************************** SetRts(val) **************************************************/
363  
364 bool CRS232::setRts(bool val)
365 {
366 if(val)
367 {
368 if(EscapeCommFunction(hcom, SETRTS) == TRUE )
369 return true;
370 }
371 else
372 {
373 if(EscapeCommFunction(hcom, CLRRTS) == TRUE )
374 return true;
375 }
376  
377 return false;
378 }
379  
380 /**************************** SetTxd(val) ***************************************************/
381 bool CRS232::setTxd(bool val)
382 {
383 if(val)
384 {
385 if( EscapeCommFunction(hcom, SETBREAK) == TRUE )
386 return true;
387 }
388 else
389 {
390 if( EscapeCommFunction(hcom, CLRBREAK) == TRUE )
391 return true;
392 }
393 return false;
394 }
395  
396 /**************************** SetDtr(val) ************************************************** */
397 bool CRS232::setDtr(bool val)
398 {
399 if(val)
400 {
401 if( EscapeCommFunction(hcom, SETDTR) == TRUE )
402 return true;
403 }
404 else
405 {
406 if( EscapeCommFunction(hcom, CLRDTR) == TRUE )
407 return false;
408 }
409 return false;
410 }
411  
412 /********************** GetCts() ***********************/
413 bool CRS232::getCts()
414 {
415 DWORD result;
416 GetCommModemStatus(hcom, &result);
417 if(result & MS_CTS_ON)
418 return true;
419 else
420 return false;
421 }
422  
423 /********************** GetDtr() ***********************/
424 bool CRS232::getDtr()
425 {
426 DWORD result;
427 GetCommModemStatus(hcom, &result);
428 if(result & MS_DSR_ON)
429 return true;
430 else
431 return false;
432 }
433  
434 /********************** GetRi() ***********************/
435 bool CRS232::getRi()
436 {
437 DWORD result;
438 GetCommModemStatus(hcom, &result);
439 if(result & MS_RING_ON)
440 return true;
441 else
442 return false;
443 }
444  
445  
446 /********************** GetCd() ***********************/
447 bool CRS232::getCd()
448 { int err=0;
449 DWORD result;
450 err = GetCommModemStatus(hcom, &result);
451 if(result & MS_RLSD_ON)
452 return true;
453 else
454 return false;
455 }
456  
457  
458  
459  
460 string CRS232::getErrorMsg()
461 {
462 LPVOID lpMsgBuf;
463 string sErreur = "";
464  
465 if ( FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
466 NULL, GetLastError(),
467 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
468 (LPTSTR) &lpMsgBuf, 0, NULL ))
469 {
470 sErreur.assign((LPCTSTR)lpMsgBuf);
471 }
472  
473 return sErreur;
474 }
475  
476  
477  
478  
479 void CRS232::SetParity(int _parity)
480 {
481  
482 if(_parity == 0)
483 dcb.Parity = NOPARITY;
484 if(_parity == 1)
485 dcb.Parity = ODDPARITY;
486 if(_parity == 2)
487 dcb.Parity = EVENPARITY;
488  
489  
490  
491 SetCommState(hcom, &dcb);
492 }
493  
494 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/