|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // SerialConnection.cpp: implementation of the CSerialConnection class. |
|
15 // This class opens a virtual comm port to perform read/write operation between PC and hardware |
|
16 // through USB cable. |
|
17 // This application is used to receive data from the H4 board.An USB driver should be installed on the PC |
|
18 // which maps the USB port to the virtual com port. |
|
19 // The data received from the hardware is displayed to debug output window. |
|
20 // |
|
21 // |
|
22 |
|
23 /** |
|
24 @file |
|
25 @internalComponent. |
|
26 */ |
|
27 |
|
28 #include "stdafx.h" |
|
29 #include "SerialPort.h" |
|
30 #include "SerialConnection.h" |
|
31 #include "SerialPortDlg.h" |
|
32 |
|
33 #ifdef _DEBUG |
|
34 #undef THIS_FILE |
|
35 static char THIS_FILE[]=__FILE__; |
|
36 #define new DEBUG_NEW |
|
37 #endif |
|
38 |
|
39 //set the baud rate |
|
40 #define BAUD_RATE 115200 |
|
41 //set the buffer size to receive data |
|
42 #define BUFFER_SIZE 64 |
|
43 |
|
44 // |
|
45 // Construction/Destruction |
|
46 // |
|
47 static HANDLE hPort; |
|
48 CSerialConnection::CSerialConnection() |
|
49 { |
|
50 } |
|
51 CSerialConnection::~CSerialConnection() //destructor |
|
52 { |
|
53 if( CloseHandle(hPort)>0 ) |
|
54 OutputDebugString ("Com port is closed successfully"); |
|
55 } |
|
56 BOOL CSerialConnection::PortInitialize(CString strPort) |
|
57 { |
|
58 |
|
59 DCB PortDCB; |
|
60 COMMTIMEOUTS CommTimeouts; |
|
61 |
|
62 //open the serial port. |
|
63 hPort = CreateFile ( strPort, |
|
64 GENERIC_READ | GENERIC_WRITE, |
|
65 0, |
|
66 NULL, |
|
67 OPEN_EXISTING, |
|
68 FILE_ATTRIBUTE_NORMAL, |
|
69 NULL); |
|
70 |
|
71 // If it fails to open the port, return FALSE. |
|
72 if ( hPort == INVALID_HANDLE_VALUE ) |
|
73 { |
|
74 AfxMessageBox("Unable to open port"); |
|
75 return FALSE; |
|
76 } |
|
77 |
|
78 PortDCB.DCBlength = sizeof (DCB); |
|
79 |
|
80 //Get the default port setting information. |
|
81 if (!GetCommState (hPort, &PortDCB)) |
|
82 { |
|
83 AfxMessageBox("Unable to retrieve default port setting information"); |
|
84 return FALSE; |
|
85 } |
|
86 |
|
87 //Change the DCB structure settings. |
|
88 |
|
89 PortDCB.BaudRate = BAUD_RATE; // Current baud |
|
90 PortDCB.fBinary = FALSE; // Binary mode; no EOF check |
|
91 PortDCB.fParity = FALSE; // Enable parity checking. |
|
92 PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control |
|
93 PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control |
|
94 PortDCB.fDtrControl = DTR_CONTROL_ENABLE; |
|
95 // DTR flow control type |
|
96 PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity |
|
97 PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx |
|
98 PortDCB.fOutX = FALSE; // No XON/XOFF out flow control |
|
99 PortDCB.fInX = FALSE; // No XON/XOFF in flow control |
|
100 PortDCB.fErrorChar = FALSE; // Disable error replacement. |
|
101 PortDCB.fNull = FALSE; // Disable null stripping. |
|
102 PortDCB.fRtsControl = RTS_CONTROL_ENABLE; |
|
103 // RTS flow control |
|
104 PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on |
|
105 // error. |
|
106 PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8 |
|
107 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space |
|
108 PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 |
|
109 |
|
110 // Configure the port according to the specifications of the DCB |
|
111 // structure. |
|
112 |
|
113 if (!SetCommState (hPort, &PortDCB)) |
|
114 { |
|
115 // could not create the read thread. |
|
116 AfxMessageBox("Unable to configure the serial port"); |
|
117 return FALSE; |
|
118 } |
|
119 |
|
120 // retrieve the time-out parameters for all read and write operations |
|
121 // on the port. |
|
122 if (!GetCommTimeouts (hPort, &CommTimeouts)) |
|
123 { |
|
124 AfxMessageBox("Unable to retrieve the time-out parameters"); |
|
125 return FALSE; |
|
126 } |
|
127 |
|
128 // Change the COMMTIMEOUTS structure settings. |
|
129 CommTimeouts.ReadIntervalTimeout = MAXDWORD; |
|
130 CommTimeouts.ReadTotalTimeoutMultiplier = 0; |
|
131 CommTimeouts.ReadTotalTimeoutConstant = 0; |
|
132 CommTimeouts.WriteTotalTimeoutMultiplier = 10; |
|
133 CommTimeouts.WriteTotalTimeoutConstant = 1000; |
|
134 |
|
135 // Set the time-out parameters for all read and write operations |
|
136 // on the port. |
|
137 if (!SetCommTimeouts (hPort, &CommTimeouts)) |
|
138 { |
|
139 AfxMessageBox("Unable to set the time-out parameters"); |
|
140 return FALSE; |
|
141 } |
|
142 |
|
143 LPVOID pPARAM=NULL; |
|
144 AfxBeginThread(PortReadThread,pPARAM); |
|
145 |
|
146 return TRUE; |
|
147 } |
|
148 |
|
149 UINT CSerialConnection::PortReadThread(LPVOID lpvoid) |
|
150 { |
|
151 int loopSize = 0; |
|
152 |
|
153 BYTE readByte[BUFFER_SIZE]; |
|
154 BYTE writeByte = 255; |
|
155 |
|
156 DWORD dwCommModemStatus; |
|
157 DWORD dwBytesTransferred; |
|
158 DWORD dwNumBytesWritten; |
|
159 |
|
160 CString str_LoopSize; |
|
161 |
|
162 if (hPort != INVALID_HANDLE_VALUE) |
|
163 { |
|
164 /* |
|
165 send some data to the harware.This is used for handshaking with the hardware. |
|
166 The USB Test code running on hardware will be on wait state until it receives any |
|
167 data from the PC |
|
168 */ |
|
169 |
|
170 WriteFile (hPort, &writeByte,1,&dwNumBytesWritten,NULL); |
|
171 //when a character appears it unblocks waitcommevent |
|
172 SetCommMask (hPort,EV_RXCHAR); |
|
173 //waits until a character appears at port |
|
174 WaitCommEvent (hPort, &dwCommModemStatus, 0); |
|
175 |
|
176 while(1) |
|
177 { |
|
178 if (EV_RXCHAR) |
|
179 { |
|
180 SetCommMask (hPort, EV_RXCHAR ); |
|
181 //read data send from the hardware |
|
182 ReadFile (hPort, &readByte, BUFFER_SIZE, &dwBytesTransferred, 0); |
|
183 |
|
184 if (dwBytesTransferred >= 1) |
|
185 { |
|
186 CString str_PortData((LPCSTR)&readByte,sizeof(readByte)); |
|
187 str_LoopSize.Format("%d",loopSize); |
|
188 loopSize++; |
|
189 //output the data to debug output window |
|
190 OutputDebugString (str_LoopSize + "Data send by USB = "+ str_PortData + "\n"); |
|
191 } |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 return 0; |
|
197 } |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |