0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* CSerialPort -- encapsulates the details of using the serial port as
|
|
16 |
* a communications channel
|
|
17 |
* System Includes
|
|
18 |
*
|
|
19 |
*/
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
#include <stdio.h>
|
|
24 |
#include <assert.h>
|
|
25 |
|
|
26 |
/*********************************************************************
|
|
27 |
*
|
|
28 |
* Local Includes
|
|
29 |
*
|
|
30 |
********************************************************************/
|
|
31 |
#include "CSerialPort.h"
|
|
32 |
|
|
33 |
|
|
34 |
/*********************************************************************
|
|
35 |
*
|
|
36 |
* Definitions
|
|
37 |
*
|
|
38 |
********************************************************************/
|
|
39 |
#define READ_TIMEOUT (1000)
|
|
40 |
|
|
41 |
|
|
42 |
/*********************************************************************
|
|
43 |
*
|
|
44 |
* Construction
|
|
45 |
*
|
|
46 |
********************************************************************/
|
|
47 |
CSerialPort::CSerialPort()
|
|
48 |
{
|
|
49 |
iComPort = INVALID_HANDLE_VALUE;
|
|
50 |
iMutex = CreateMutex( NULL, false, NULL );
|
|
51 |
assert (iMutex != NULL);
|
|
52 |
}
|
|
53 |
|
|
54 |
|
|
55 |
CSerialPort::~CSerialPort()
|
|
56 |
{
|
|
57 |
assert( iComPort == INVALID_HANDLE_VALUE );
|
|
58 |
CloseHandle( iMutex );
|
|
59 |
iMutex = NULL;
|
|
60 |
}
|
|
61 |
|
|
62 |
|
|
63 |
/*********************************************************************
|
|
64 |
*
|
|
65 |
* OpenComPort() -- open a com port, set the timeouts, set the config
|
|
66 |
*
|
|
67 |
********************************************************************/
|
|
68 |
int CSerialPort::OpenPort( char *aComPort )
|
|
69 |
{
|
|
70 |
int err;
|
|
71 |
|
|
72 |
// Open the COM Port
|
|
73 |
iComPort = CreateFile( aComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL );
|
|
74 |
if( iComPort == INVALID_HANDLE_VALUE ) {
|
|
75 |
err = GetLastError();
|
|
76 |
return err;
|
|
77 |
}
|
|
78 |
|
|
79 |
// Set the timeouts (see msdn for the meaning of these carefully chosen values)
|
|
80 |
COMMTIMEOUTS CommTimeOuts;
|
|
81 |
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
|
|
82 |
CommTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
|
|
83 |
CommTimeOuts.ReadTotalTimeoutConstant = READ_TIMEOUT;
|
|
84 |
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
|
|
85 |
CommTimeOuts.WriteTotalTimeoutConstant = 0;
|
|
86 |
err = SetCommTimeouts( iComPort, &CommTimeOuts );
|
|
87 |
if( err == 0 ) {
|
|
88 |
err = GetLastError();
|
|
89 |
CloseHandle( iComPort );
|
|
90 |
iComPort = INVALID_HANDLE_VALUE;
|
|
91 |
return err;
|
|
92 |
}
|
|
93 |
|
|
94 |
// Configure the COM port - NO FLOW CONTROL
|
|
95 |
DCB dcb;
|
|
96 |
GetCommState(iComPort, &dcb);
|
|
97 |
dcb.DCBlength = sizeof(dcb);
|
|
98 |
dcb.BaudRate = CBR_115200;
|
|
99 |
// dcb.fBinary = TRUE;
|
|
100 |
dcb.fBinary = FALSE;
|
|
101 |
dcb.fParity = NOPARITY;
|
|
102 |
dcb.fOutxCtsFlow = TRUE;
|
|
103 |
dcb.fOutxDsrFlow = FALSE;
|
|
104 |
dcb.fDtrControl = DTR_CONTROL_ENABLE;
|
|
105 |
dcb.fDsrSensitivity = FALSE;
|
|
106 |
dcb.fTXContinueOnXoff = TRUE;
|
|
107 |
dcb.fOutX = FALSE;
|
|
108 |
dcb.fInX = FALSE;
|
|
109 |
dcb.fErrorChar = FALSE;
|
|
110 |
dcb.fNull = FALSE;
|
|
111 |
// dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
|
|
112 |
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
|
113 |
dcb.fAbortOnError = TRUE;
|
|
114 |
dcb.XonLim = 100;
|
|
115 |
dcb.XoffLim = 100;
|
|
116 |
dcb.ByteSize = 8;
|
|
117 |
dcb.Parity = NOPARITY;
|
|
118 |
dcb.StopBits = ONESTOPBIT;
|
|
119 |
dcb.XonChar = 17;
|
|
120 |
dcb.XoffChar = 19;
|
|
121 |
err = SetCommState( iComPort, &dcb );
|
|
122 |
if( err == 0 ) {
|
|
123 |
err = GetLastError();
|
|
124 |
CloseHandle( iComPort );
|
|
125 |
iComPort = INVALID_HANDLE_VALUE;
|
|
126 |
return err;
|
|
127 |
}
|
|
128 |
|
|
129 |
// raise RTS and DTR so the other end won't block (it can use whatever hw flow control it wants)
|
|
130 |
err = EscapeCommFunction( iComPort, SETDTR );
|
|
131 |
assert( err != 0 );
|
|
132 |
err = EscapeCommFunction( iComPort, SETRTS );
|
|
133 |
assert( err != 0 );
|
|
134 |
|
|
135 |
// Success
|
|
136 |
return 0;
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
/*********************************************************************
|
|
141 |
*
|
|
142 |
* CloseComPort() -- close an open com port.
|
|
143 |
*
|
|
144 |
********************************************************************/
|
|
145 |
void CSerialPort::ClosePort()
|
|
146 |
{
|
|
147 |
// if the port isn't open then just return
|
|
148 |
if( iComPort == INVALID_HANDLE_VALUE )
|
|
149 |
return;
|
|
150 |
|
|
151 |
// close the port
|
|
152 |
CloseHandle( iComPort );
|
|
153 |
iComPort = INVALID_HANDLE_VALUE;
|
|
154 |
}
|
|
155 |
|
|
156 |
|
|
157 |
/*********************************************************************
|
|
158 |
*
|
|
159 |
* ReceiveBytes() -- read specified number of bytes from the comport
|
|
160 |
*
|
|
161 |
********************************************************************/
|
|
162 |
int CSerialPort::ReceiveBytes( char* aBuff, int *aSize )
|
|
163 |
{
|
|
164 |
int err, mutex_err;
|
|
165 |
int original_size;
|
|
166 |
unsigned long iBytesRead;
|
|
167 |
|
|
168 |
// check the state and the params
|
|
169 |
assert( iComPort != INVALID_HANDLE_VALUE );
|
|
170 |
assert( aBuff != NULL );
|
|
171 |
assert( aSize != NULL );
|
|
172 |
|
|
173 |
// acquire the port mutex
|
|
174 |
mutex_err = WaitForSingleObject( iMutex, INFINITE );
|
|
175 |
assert( mutex_err == WAIT_OBJECT_0 );
|
|
176 |
|
|
177 |
// receive bytes
|
|
178 |
original_size = *aSize;
|
|
179 |
err = ReadFile( iComPort, aBuff, *aSize, &iBytesRead, NULL );
|
|
180 |
|
|
181 |
// give the mutex back
|
|
182 |
mutex_err = ReleaseMutex( iMutex );
|
|
183 |
assert( mutex_err != 0 );
|
|
184 |
|
|
185 |
// set aSize to the number actually read
|
|
186 |
*aSize = iBytesRead;
|
|
187 |
|
|
188 |
// check for errors
|
|
189 |
if( err == 0 ) {
|
|
190 |
err = GetLastError();
|
|
191 |
return err;
|
|
192 |
}
|
|
193 |
|
|
194 |
// done
|
|
195 |
return 0;
|
|
196 |
}
|
|
197 |
|
|
198 |
|
|
199 |
/*********************************************************************
|
|
200 |
*
|
|
201 |
* SendBytes() -- write the specified number of bytes to the COMport
|
|
202 |
*
|
|
203 |
********************************************************************/
|
|
204 |
int CSerialPort::SendBytes( char *aBuff, int *aSize )
|
|
205 |
{
|
|
206 |
int err, mutex_err;
|
|
207 |
DWORD dwModemStatus, dwBytes = 0;
|
|
208 |
|
|
209 |
// check state and params
|
|
210 |
assert( iComPort != INVALID_HANDLE_VALUE );
|
|
211 |
assert( aBuff != NULL );
|
|
212 |
assert( aSize != NULL );
|
|
213 |
|
|
214 |
// acquire the port mutex
|
|
215 |
mutex_err = WaitForSingleObject(iMutex, INFINITE);
|
|
216 |
assert( mutex_err == WAIT_OBJECT_0 );
|
|
217 |
|
|
218 |
// write bytes to the port
|
|
219 |
err = GetCommModemStatus( iComPort, &dwModemStatus );
|
|
220 |
err = WriteFile( iComPort, (LPVOID)aBuff, *aSize, &dwBytes, NULL );
|
|
221 |
|
|
222 |
// give the mutex back
|
|
223 |
mutex_err = ReleaseMutex( iMutex );
|
|
224 |
assert( mutex_err != 0 );
|
|
225 |
|
|
226 |
// set aSize to the number actually written
|
|
227 |
*aSize = dwBytes;
|
|
228 |
|
|
229 |
// check for errors
|
|
230 |
if( err == 0 ) {
|
|
231 |
err = GetLastError();
|
|
232 |
return err;
|
|
233 |
}
|
|
234 |
|
|
235 |
// done
|
|
236 |
return 0;
|
|
237 |
}
|