1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
#include "stdafx.h"
#include "RS232Connection.tmh"
/*-----------------------------------------------------------------------------
FUNCTION: RS232Connection()
PURPOSE: Constructor. Initializes TTY structure
COMMENTS: This structure is a collection of TTY attributes
used by all parts of this program
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
2/14/96 AllenD Removed npTTYInfo
12/06/06 DonnMo Changed return value type
12/06/06 DonnMo Replaced macro calls with member-variable settings
12/06/06 DonnMo Removed initialization of font-related variables
-----------------------------------------------------------------------------*/
RS232Connection::RS232Connection()
{
//
// initialize generial TTY info
//
m_hCommPort = NULL;
m_fConnected = FALSE;
m_fLocalEcho = FALSE;
m_bPort = 1;
m_dwBaudRate = CBR_9600;
m_bByteSize = 8;
m_bParity = NOPARITY;
m_bStopBits = ONESTOPBIT;
m_fAutowrap = TRUE;
m_fNewLine = FALSE;
m_fDisplayErrors = TRUE;
//
// timeouts
//
//
// TimeoutsDefault
// We need ReadIntervalTimeout here to cause the read operations
// that we do to actually timeout and become overlapped.
// Specifying 1 here causes ReadFile to return very quickly
// so that our reader thread will continue execution.
//
m_TimeoutsDefault = { 25, 0, 0, 0, 0 };
m_timeoutsnew = m_TimeoutsDefault;
//
// read state and status events
//
m_dwReceiveState = RECEIVE_TTY;
m_dwEventFlags = EVENTFLAGS_DEFAULT;
m_chFlag = FLAGCHAR_DEFAULT;
//
// Flow Control Settings
//
m_fDtrControl = DTR_CONTROL_ENABLE;
m_fRtsControl = RTS_CONTROL_ENABLE;
m_chXON = ASCII_XON;
m_chXOFF = ASCII_XOFF;
m_wXONLimit = 0;
m_wXOFFLimit = 0;
m_fCTSOutFlow = FALSE;
m_fDSROutFlow = FALSE;
m_fDSRInFlow = FALSE;
m_fXonXoffOutFlow = FALSE;
m_fXonXoffInFlow = FALSE;
m_fTXafterXoffSent = FALSE;
m_fNoReading = FALSE;
m_fNoWriting = FALSE;
m_fNoEvents = FALSE;
m_fNoStatus = FALSE;
m_fDisplayTimeouts = FALSE;
}
/*-----------------------------------------------------------------------------
FUNCTION: SetPortState( void )
PURPOSE: Sets port state based on settings from the user
COMMENTS: Sets up DCB structure and calls SetCommState.
Sets up new timeouts by calling SetCommTimeouts.
HISTORY: Date: Author: Comment:
1/9/96 AllenD Wrote it
12/06/06 DonnMo Replaced calls to ErrorReporter() with CHECK_HR()
-----------------------------------------------------------------------------*/
HRESULT RS232Connection::SetPortState()
{
HRESULT hr = S_OK;
DCB dcb = {0};
DWORD dwLastError = 0;
dcb.DCBlength = sizeof(dcb);
//
// get current DCB settings
//
if (!GetCommState(m_hCommPort, &dcb))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "GetCommState() failed within SetPortState().");
return hr;
}
//
// update DCB rate, byte size, parity, and stop bits size
//
dcb.BaudRate = m_dwBaudRate;
dcb.ByteSize = m_bByteSize;
dcb.Parity = m_bParity;
dcb.StopBits = m_bStopBits;
//
// update event flags
//
if (m_dwEventFlags & EV_RXFLAG)
{
dcb.EvtChar = m_chFlag;
}
else
{
dcb.EvtChar = '\0';
}
dcb.EofChar = '\n';
//
// update flow control settings
//
dcb.fDtrControl = m_fDtrControl;
dcb.fRtsControl = m_fRtsControl;
dcb.fOutxCtsFlow = m_fCTSOutFlow;
dcb.fOutxDsrFlow = m_fDSROutFlow;
dcb.fDsrSensitivity = m_fDSRInFlow;
dcb.fOutX = m_fXonXoffOutFlow;
dcb.fInX = m_fXonXoffInFlow;
dcb.fTXContinueOnXoff = m_fTXafterXoffSent;
dcb.XonChar = m_chXON;
dcb.XoffChar = m_chXOFF;
dcb.XonLim = m_wXONLimit;
dcb.XoffLim = m_wXOFFLimit;
//
// DCB settings not in the user's control
//
dcb.fParity = TRUE;
//
// set new state
//
if (!SetCommState(m_hCommPort, &dcb))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "SetCommState() failed within SetPortState() when setting the new state.");
return hr;
}
//
// set new timeouts
//
if (!SetCommTimeouts(m_hCommPort, &m_timeoutsnew))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "SetCommTimeouts() failed within SetPortState() when setting the new timeouts.");
return hr;
}
return hr;
}
/*-----------------------------------------------------------------------------
FUNCTION: Connect( void )
PURPOSE: Setup Communication Port with our settings
RETURN:
S_OK and handle of com port if successful
-----------------------------------------------------------------------------*/
HRESULT RS232Connection::Connect(_In_ LPCWSTR wszPortName, _Out_ HANDLE *phCommPort)
{
HRESULT hr = S_OK;
DWORD dwLastError = 0;
if (phCommPort == NULL)
{
hr = E_POINTER;
CHECK_HR(hr, "A NULL phCommPort parameter was received");
return hr;
}
*phCommPort = NULL;
//
// retrieve a handle for the com port
// and configure the port for asynchronous
// communications.
//
m_hCommPort = CreateFileW(wszPortName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
0);
if (m_hCommPort == NULL)
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "CreateFileW() failed in RS232Connection::Connect");
return hr;
}
//
// Save original comm timeouts and set new ones
//
if (!GetCommTimeouts( m_hCommPort, &(m_timeoutsorig)))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "GetCommTimeouts() failed within RS232Connection::Connect");
return hr;
}
//
// Set port state
//
hr = SetPortState();
if (FAILED(hr))
{
CHECK_HR(hr, "SetPortState() failed within RS232Connection::Connect");
return hr;
}
//
// set comm buffer sizes
//
if (!SetupComm(m_hCommPort, MAX_READ_BUFFER, MAX_WRITE_BUFFER))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "SetupComm(SETDTR) failed within RS232Connection::Connect");
return hr;
}
//
// raise DTR
//
if (!EscapeCommFunction(m_hCommPort, SETDTR))
{
dwLastError = GetLastError();
hr = HRESULT_FROM_WIN32(dwLastError);
CHECK_HR(hr, "EscapeCommFunction() failed within RS232Connection::Connect");
return hr;
}
//
// set overall connect flag
//
m_fConnected = TRUE;
//
// set the return value
//
*phCommPort = m_hCommPort;
if (SUCCEEDED(hr))
{
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FLAG_DEVICE, "%!FUNC! handle: %p", m_hCommPort);
}
return hr;
}
/*-----------------------------------------------------------------------------
FUNCTION: Disconnect( void )
PURPOSE: Tears down the Communication Port
RETURN: nothing
-----------------------------------------------------------------------------*/
void RS232Connection::Disconnect()
{
if (m_hCommPort != NULL)
{
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FLAG_DEVICE, "%!FUNC! handle: %p", m_hCommPort);
CloseHandle(m_hCommPort);
m_hCommPort = NULL;
}
}
|