|
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 * Construction |
|
36 * |
|
37 ********************************************************************/ |
|
38 CSerialPort::CSerialPort() |
|
39 { |
|
40 iComPort = INVALID_HANDLE_VALUE; |
|
41 } |
|
42 |
|
43 |
|
44 CSerialPort::~CSerialPort() |
|
45 { |
|
46 assert( iComPort == INVALID_HANDLE_VALUE ); |
|
47 } |
|
48 |
|
49 |
|
50 /********************************************************************* |
|
51 * |
|
52 * OpenComPort() -- open a com port, set the timeouts, set the config |
|
53 * |
|
54 ********************************************************************/ |
|
55 int CSerialPort::OpenPort( char *aComPort ) |
|
56 { |
|
57 int err; |
|
58 |
|
59 // Open the COM Port |
|
60 iComPort = CreateFile( aComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL ); |
|
61 if( iComPort == INVALID_HANDLE_VALUE ) { |
|
62 err = GetLastError(); |
|
63 return err; |
|
64 } |
|
65 |
|
66 // Set the timeouts |
|
67 COMMTIMEOUTS CommTimeOuts; |
|
68 CommTimeOuts.ReadIntervalTimeout = 0; |
|
69 CommTimeOuts.ReadTotalTimeoutMultiplier = 0; |
|
70 CommTimeOuts.ReadTotalTimeoutConstant = 0; |
|
71 CommTimeOuts.WriteTotalTimeoutMultiplier = 0; |
|
72 CommTimeOuts.WriteTotalTimeoutConstant = 0; |
|
73 err = SetCommTimeouts( iComPort, &CommTimeOuts ); |
|
74 if( err == 0 ) { |
|
75 err = GetLastError(); |
|
76 CloseHandle( iComPort ); |
|
77 iComPort = INVALID_HANDLE_VALUE; |
|
78 return err; |
|
79 } |
|
80 |
|
81 // Configure the COM port |
|
82 DCB dcb; |
|
83 GetCommState(iComPort, &dcb); |
|
84 dcb.DCBlength = sizeof(dcb); |
|
85 dcb.BaudRate = CBR_115200; |
|
86 dcb.fBinary = TRUE; |
|
87 dcb.fParity = NOPARITY; |
|
88 dcb.fOutxCtsFlow = TRUE; |
|
89 dcb.fOutxDsrFlow = FALSE; |
|
90 dcb.fDtrControl = DTR_CONTROL_ENABLE; |
|
91 dcb.fDsrSensitivity = FALSE; |
|
92 dcb.fTXContinueOnXoff = TRUE; |
|
93 dcb.fOutX = FALSE; |
|
94 dcb.fInX = FALSE; |
|
95 dcb.fErrorChar = FALSE; |
|
96 dcb.fNull = FALSE; |
|
97 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; |
|
98 dcb.fAbortOnError = TRUE; |
|
99 dcb.XonLim = 100; |
|
100 dcb.XoffLim = 100; |
|
101 dcb.ByteSize = 8; |
|
102 dcb.Parity = NOPARITY; |
|
103 dcb.StopBits = ONESTOPBIT; |
|
104 dcb.XonChar = 17; |
|
105 dcb.XoffChar = 19; |
|
106 err = SetCommState( iComPort, &dcb ); |
|
107 if( err == 0 ) { |
|
108 err = GetLastError(); |
|
109 CloseHandle( iComPort ); |
|
110 iComPort = INVALID_HANDLE_VALUE; |
|
111 return err; |
|
112 } |
|
113 |
|
114 // Success |
|
115 return 0; |
|
116 } |
|
117 |
|
118 |
|
119 /********************************************************************* |
|
120 * |
|
121 * CloseComPort() -- close an open com port. |
|
122 * |
|
123 ********************************************************************/ |
|
124 void CSerialPort::ClosePort() |
|
125 { |
|
126 // if the port isn't open then just return |
|
127 if( iComPort == INVALID_HANDLE_VALUE ) |
|
128 return; |
|
129 |
|
130 // close the port |
|
131 CloseHandle( iComPort ); |
|
132 iComPort = INVALID_HANDLE_VALUE; |
|
133 } |
|
134 |
|
135 |
|
136 /********************************************************************* |
|
137 * |
|
138 * ReceiveBytes() -- read specified number of bytes from the comport |
|
139 * |
|
140 ********************************************************************/ |
|
141 int CSerialPort::ReceiveBytes( char* aBuff, int *aSize) |
|
142 { |
|
143 int err; |
|
144 int original_size; |
|
145 unsigned long iBytesRead; |
|
146 |
|
147 // check the state and the params |
|
148 assert( iComPort != INVALID_HANDLE_VALUE ); |
|
149 assert( aBuff != NULL ); |
|
150 assert( aSize != NULL ); |
|
151 |
|
152 // receive bytes |
|
153 original_size = *aSize; |
|
154 err = ReadFile( iComPort, aBuff, *aSize, &iBytesRead, NULL ); |
|
155 |
|
156 // set aSize to the number actually read |
|
157 *aSize = iBytesRead; |
|
158 |
|
159 // check for errors |
|
160 if( err == 0 ) { |
|
161 err = GetLastError(); |
|
162 return err; |
|
163 } |
|
164 |
|
165 // this condition should have generated an error -- lets make sure |
|
166 assert( *aSize == original_size ); |
|
167 return 0; |
|
168 } |
|
169 |
|
170 |
|
171 /********************************************************************* |
|
172 * |
|
173 * SendBytes() -- write the specified number of bytes to the COMport |
|
174 * |
|
175 ********************************************************************/ |
|
176 int CSerialPort::SendBytes( char *aBuff, int *aSize ) |
|
177 { |
|
178 int ret; |
|
179 DWORD dwModemStatus, dwBytes = 0; |
|
180 |
|
181 // check state and params |
|
182 assert( iComPort != INVALID_HANDLE_VALUE ); |
|
183 assert( aBuff != NULL ); |
|
184 assert( aSize != NULL ); |
|
185 |
|
186 // write bytes to the port |
|
187 ret = GetCommModemStatus( iComPort, &dwModemStatus ); |
|
188 ret = WriteFile( iComPort, (LPVOID)aBuff, *aSize, &dwBytes, NULL ); |
|
189 |
|
190 // set aSize to the number actually written |
|
191 *aSize = dwBytes; |
|
192 |
|
193 // check for errors |
|
194 if( ret == 0 ) { |
|
195 ret = GetLastError(); |
|
196 return ret; |
|
197 } |
|
198 |
|
199 // done |
|
200 return 0; |
|
201 } |