author | fturovic <frank.turovich@nokia.com> |
Thu, 05 Aug 2010 09:52:02 -0500 | |
changeset 1766 | 040623849d55 |
parent 1481 | c7f22cc57d44 |
permissions | -rw-r--r-- |
60 | 1 |
/* |
2 |
* Copyright (c) 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 the License "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 |
* |
|
16 |
*/ |
|
17 |
// RealSerialComm.cpp: implementation of the CRealSerialComm class. |
|
18 |
// |
|
19 |
////////////////////////////////////////////////////////////////////// |
|
20 |
||
21 |
#include "stdafx.h" |
|
22 |
#include "RealSerialComm.h" |
|
23 |
//#include "pn_const.h" |
|
24 |
//#include "OSTConstants.h" |
|
25 |
#include "Connection.h" |
|
26 |
||
27 |
#ifdef _DEBUG |
|
366 | 28 |
//static char sLogMsg[3000]; |
60 | 29 |
#endif |
30 |
////////////////////////////////////////////////////////////////////// |
|
31 |
// Construction/Destruction |
|
32 |
////////////////////////////////////////////////////////////////////// |
|
33 |
#ifdef _DEBUG |
|
34 |
#define LogErrorText(err) { if (err > 0) GetErrorText(err); } |
|
35 |
#define LogErrorText2(err) { if (err > 0) GetErrorText2(err); } |
|
36 |
#else |
|
37 |
#define LogErrorText(err) {} |
|
38 |
#define LogErrorText2(err) {} |
|
39 |
#endif |
|
40 |
||
41 |
#ifdef _DEBUG |
|
42 |
#define DUMPCOMSTAT(x) DumpComStat(x) |
|
43 |
#define DUMPCOMSTATP(x) DumpComStatP(x) |
|
44 |
#else |
|
45 |
#define DUMPCOMSTAT(x) |
|
46 |
#define DUMPCOMSTATP(x) |
|
47 |
#endif |
|
48 |
||
49 |
CRealSerialComm::CRealSerialComm() |
|
50 |
{ |
|
51 |
#ifdef _DEBUG |
|
52 |
if (gDoLogging) |
|
53 |
{ |
|
54 |
FILE* f = fopen("c:\\tcf\\rscommlog.txt", "at"); |
|
55 |
fprintf(f, "CRealSerialComm::CRealSerialComm() (default constructor)\n"); |
|
56 |
fclose(f); |
|
57 |
} |
|
58 |
#endif |
|
59 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
60 |
m_serialPortName[0] = 0; |
|
61 |
m_pBuffer = NULL; |
|
62 |
m_ProcDebugLog = NULL; |
|
63 |
||
64 |
} |
|
65 |
CRealSerialComm::CRealSerialComm(ConnectData* connectSettings, DWORD connectionId, CBaseProtocol* protocol) |
|
66 |
{ |
|
67 |
#ifdef _DEBUG |
|
68 |
if (gDoLogging) |
|
69 |
{ |
|
70 |
FILE* f = fopen("c:\\tcf\\rscommlog.txt", "at"); |
|
71 |
fprintf(f, "connectSettings=%x connectionId=%d, protocol=%x\n", connectSettings, connectionId, protocol); |
|
72 |
fclose(f); |
|
73 |
} |
|
74 |
#endif |
|
75 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
76 |
m_serialPortName[0] = 0; |
|
77 |
m_pBuffer = NULL; |
|
78 |
||
79 |
m_connId = connectionId; |
|
80 |
m_Protocol = protocol; |
|
81 |
||
82 |
m_ConnectSettings = new ConnectData(); |
|
83 |
memcpy(m_ConnectSettings, connectSettings, sizeof(ConnectData)); |
|
84 |
||
85 |
#if (defined(LOG_COMM) || defined(LOG_PROCCOMM)) && defined(_DEBUG) |
|
86 |
if (gDoLogging) |
|
87 |
{ |
|
88 |
m_CommDebugLog = new TCDebugLog("TCF_Comm", connectionId, 2000L); |
|
89 |
m_ProcDebugLog = new TCDebugLog("TCF_CommP", connectionId, 2000L); |
|
90 |
} |
|
91 |
#endif |
|
92 |
} |
|
93 |
CRealSerialComm::~CRealSerialComm() |
|
94 |
{ |
|
95 |
#ifdef _DEBUG |
|
96 |
if (gDoLogging) |
|
97 |
{ |
|
98 |
FILE* f = fopen("c:\\tcf\\rscommlog.txt", "at"); |
|
99 |
fprintf(f, "CRealSerialComm::~CRealSerialComm()\n"); |
|
100 |
fclose(f); |
|
101 |
} |
|
102 |
#endif |
|
103 |
if (m_hSerial != INVALID_HANDLE_VALUE) |
|
104 |
::CloseHandle(m_hSerial); |
|
105 |
||
106 |
if (m_pBuffer) |
|
107 |
delete[] m_pBuffer; |
|
108 |
||
109 |
} |
|
110 |
||
111 |
long CRealSerialComm::OpenPort() |
|
112 |
{ |
|
113 |
COMMLOGOPEN(); |
|
114 |
COMMLOGS("CRealSerialComm::OpenPort\n"); |
|
115 |
||
116 |
long err = TCAPI_ERR_NONE; |
|
117 |
||
118 |
char* comPort = m_ConnectSettings->realSerialSettings.comPort; |
|
119 |
DWORD baudRate = m_ConnectSettings->realSerialSettings.baudRate; |
|
120 |
DWORD dataBits = m_ConnectSettings->realSerialSettings.dataBits; |
|
121 |
eParity parity = m_ConnectSettings->realSerialSettings.parity; |
|
122 |
eStopBits stopBits = m_ConnectSettings->realSerialSettings.stopBits; |
|
123 |
eFlowControl flow = m_ConnectSettings->realSerialSettings.flowControl; |
|
124 |
||
125 |
COMMLOGA2("CRealSerialComm::OpenPort comPort=%s baudRate=%d\n", comPort, baudRate); |
|
126 |
COMMLOGA2("CRealSerialComm::OpenPort dataBits=%d parity=%d\n", dataBits, parity); |
|
127 |
COMMLOGA2("CRealSerialComm::OpenPort stopBits=%d flow=%d\n", stopBits, flow); |
|
128 |
||
129 |
// fill in DCB |
|
130 |
m_dcb.DCBlength = sizeof(DCB); |
|
131 |
m_dcb.BaudRate = baudRate; |
|
132 |
m_dcb.ByteSize = dataBits; |
|
133 |
||
134 |
// parity |
|
135 |
switch(parity) |
|
136 |
{ |
|
137 |
default: |
|
138 |
case eParityNone: |
|
139 |
m_dcb.fParity = FALSE; |
|
140 |
m_dcb.Parity = NOPARITY; |
|
141 |
break; |
|
142 |
case eParityEven: |
|
143 |
m_dcb.fParity = TRUE; |
|
144 |
m_dcb.Parity = EVENPARITY; |
|
145 |
break; |
|
146 |
case eParityOdd: |
|
147 |
m_dcb.fParity = TRUE; |
|
148 |
m_dcb.Parity = ODDPARITY; |
|
149 |
break; |
|
150 |
} |
|
151 |
||
152 |
// stop bits |
|
153 |
switch(stopBits) |
|
154 |
{ |
|
155 |
default: |
|
156 |
case eStopBits1: |
|
157 |
m_dcb.StopBits = ONESTOPBIT; |
|
158 |
break; |
|
159 |
case eStopBits15: |
|
160 |
m_dcb.StopBits = ONE5STOPBITS; |
|
161 |
break; |
|
162 |
case eStopBits2: |
|
163 |
m_dcb.StopBits = TWOSTOPBITS; |
|
164 |
break; |
|
165 |
} |
|
166 |
||
167 |
// flow control |
|
168 |
switch(flow) |
|
169 |
{ |
|
170 |
default: |
|
171 |
case eFlowControlNone: |
|
172 |
m_dcb.fRtsControl = RTS_CONTROL_DISABLE; |
|
173 |
m_dcb.fOutxCtsFlow = FALSE; |
|
174 |
m_dcb.fInX = m_dcb.fOutX = FALSE; |
|
175 |
break; |
|
176 |
case eFlowControlHW: |
|
177 |
m_dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; |
|
178 |
m_dcb.fOutxCtsFlow = TRUE; |
|
179 |
m_dcb.fInX = m_dcb.fOutX = FALSE; |
|
180 |
break; |
|
181 |
case eFlowControlSW: |
|
182 |
m_dcb.fRtsControl = RTS_CONTROL_DISABLE; |
|
183 |
m_dcb.fOutxCtsFlow = FALSE; |
|
184 |
m_dcb.fInX = m_dcb.fOutX = TRUE; |
|
185 |
m_dcb.XonChar = '\021'; // Ctrl-Q; |
|
186 |
m_dcb.XoffChar = '\023'; // Ctrl-S; |
|
187 |
m_dcb.XonLim = 100; |
|
188 |
m_dcb.XoffLim = 100; |
|
189 |
break; |
|
190 |
} |
|
191 |
||
192 |
// other things in DCB |
|
193 |
m_dcb.fDtrControl = DTR_CONTROL_ENABLE; |
|
194 |
m_dcb.fDsrSensitivity = FALSE; |
|
195 |
m_dcb.fBinary = TRUE; |
|
196 |
m_dcb.fNull = FALSE; |
|
197 |
m_dcb.fAbortOnError = TRUE; // reads & writes will terminate with errors if one occurs |
|
198 |
||
199 |
// translate serial port |
|
200 |
char p[20]; char* pp = p; |
|
201 |
strncpy(p, comPort, 20); |
|
202 |
int len = (int)strlen(p); |
|
203 |
for (int i = 0; i < len; i++) |
|
204 |
{ |
|
205 |
p[i] = toupper(p[i]); |
|
206 |
} |
|
207 |
if (strncmp(p, "COM", 3) == 0) |
|
208 |
{ |
|
209 |
pp+=3; |
|
210 |
} |
|
211 |
int val = atoi((const char*)pp); |
|
212 |
if (val == INT_MIN || val == INT_MAX) |
|
213 |
{ |
|
214 |
err = TCAPI_ERR_INVALID_MEDIA_DATA; |
|
215 |
} |
|
216 |
else |
|
217 |
{ |
|
218 |
// must translate for CreatFile |
|
219 |
_snprintf(m_serialPortName, MAX_COMPORT_SIZE, "\\\\.\\COM%d", val); |
|
220 |
} |
|
221 |
||
222 |
||
223 |
if (err == TCAPI_ERR_NONE) |
|
224 |
{ |
|
225 |
m_hSerial = CreateFile(m_serialPortName, |
|
226 |
GENERIC_READ|GENERIC_WRITE, // dwDesiredAccess = read & write |
|
227 |
0, // dwSharedMode = 0 ==> device not shared |
|
228 |
NULL, // lpSecurityAttributes = NULL ==> not inheritable |
|
229 |
OPEN_EXISTING, // dwCreationDisposition ==> required for devices |
|
230 |
0, // dwFlagsAndAttributes ==> no special flags or attributes (not overlapped) |
|
231 |
NULL ); // hTemplateFile = NULL ==> required for devices |
|
232 |
||
233 |
if (m_hSerial != INVALID_HANDLE_VALUE) |
|
234 |
{ |
|
235 |
// TODO: this is really not needed as we're not doing overlapped IO |
|
236 |
// and we're not creating an event nor waiting on that event |
|
237 |
if (!SetCommMask(m_hSerial, EV_RXCHAR)) |
|
238 |
{ |
|
239 |
::CloseHandle(m_hSerial); |
|
240 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
241 |
m_lastCommError = GetLastError(); |
|
242 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
243 |
} |
|
244 |
else |
|
245 |
{ |
|
246 |
// no error from SetCommMask |
|
247 |
if (!SetupComm(m_hSerial,MAX_MESSAGE_LENGTH,MAX_SERIAL_MESSAGE_BUFFER_LENGTH)) |
|
248 |
{ |
|
249 |
CloseHandle(m_hSerial); |
|
250 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
251 |
m_lastCommError = GetLastError(); |
|
252 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
253 |
} |
|
254 |
else |
|
255 |
{ |
|
256 |
// no error from SetupComm |
|
257 |
// Get rid of any junk that might be sitting there. |
|
258 |
PurgeComm( m_hSerial, PURGE_TXABORT | PURGE_RXABORT | |
|
259 |
PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
260 |
||
261 |
// Using these settings, the ReadFile command will return immediately |
|
262 |
// rather than waiting for a timeout. |
|
263 |
COMMTIMEOUTS lclCommTimeOuts; |
|
264 |
||
265 |
lclCommTimeOuts.ReadIntervalTimeout = MAXDWORD; // we don't care about time between chars |
|
266 |
lclCommTimeOuts.ReadTotalTimeoutMultiplier = 100; |
|
267 |
lclCommTimeOuts.ReadTotalTimeoutConstant = 0; |
|
268 |
lclCommTimeOuts.WriteTotalTimeoutMultiplier = 100; |
|
269 |
lclCommTimeOuts.WriteTotalTimeoutConstant = 0; |
|
270 |
||
271 |
if (!SetCommTimeouts( m_hSerial, &lclCommTimeOuts )) |
|
272 |
{ |
|
273 |
CloseHandle(m_hSerial); |
|
274 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
275 |
m_lastCommError = GetLastError(); |
|
276 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
277 |
} |
|
278 |
else |
|
279 |
{ |
|
280 |
// no error from SetCommTimeouts |
|
281 |
err = SetDCB(); |
|
282 |
if (err != TCAPI_ERR_NONE) |
|
283 |
{ |
|
284 |
CloseHandle(m_hSerial); |
|
285 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
286 |
} |
|
287 |
else |
|
288 |
{ |
|
289 |
// no error from SetDCB |
|
290 |
err = TCAPI_ERR_NONE; |
|
291 |
m_numberBytes = 0; |
|
292 |
m_lastCommError = 0; |
|
293 |
m_isConnected = true; |
|
294 |
m_pBuffer = new BYTE[MAX_SERIAL_MESSAGE_BUFFER_LENGTH]; |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
else |
|
301 |
{ |
|
302 |
// error from CreateFile |
|
303 |
// couldn't open serial port |
|
304 |
m_lastCommError = GetLastError(); |
|
305 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
COMMLOGA2("CRealSerialComm::OpenPort err=%d osError=%d\n", err, m_lastCommError); |
|
310 |
LogErrorText(m_lastCommError); |
|
311 |
// if (m_lastCommError > 0) |
|
312 |
// LogErrorText(m_lastCommError); |
|
313 |
COMMLOGCLOSE(); |
|
314 |
return err; |
|
315 |
} |
|
316 |
||
317 |
long CRealSerialComm::SetDCB() |
|
318 |
{ |
|
319 |
// assumes serial port is open |
|
320 |
long err = TCAPI_ERR_NONE; |
|
321 |
if (m_hSerial == INVALID_HANDLE_VALUE) |
|
322 |
return err; |
|
323 |
||
324 |
// setup DCB |
|
325 |
DCB lcldcb; |
|
326 |
lcldcb.DCBlength = sizeof(DCB); |
|
327 |
||
328 |
if (!GetCommState( m_hSerial, &lcldcb )) |
|
329 |
{ |
|
330 |
m_lastCommError = GetLastError(); |
|
331 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
332 |
} |
|
333 |
||
334 |
// LogDCB(pInfo); |
|
335 |
// copy only the ones that Connect() set initially |
|
336 |
lcldcb.BaudRate = m_dcb.BaudRate; |
|
337 |
lcldcb.ByteSize = m_dcb.ByteSize; |
|
338 |
lcldcb.Parity = m_dcb.Parity; |
|
339 |
lcldcb.StopBits = m_dcb.StopBits; |
|
340 |
lcldcb.fRtsControl = m_dcb.fRtsControl; |
|
341 |
lcldcb.fOutxCtsFlow = m_dcb.fOutxCtsFlow; |
|
342 |
lcldcb.fDtrControl = m_dcb.fDtrControl; |
|
343 |
lcldcb.fDsrSensitivity = m_dcb.fDsrSensitivity; |
|
344 |
lcldcb.fInX = m_dcb.fInX; |
|
345 |
lcldcb.fOutX = m_dcb.fOutX; |
|
346 |
lcldcb.XonChar = m_dcb.XonChar; |
|
347 |
lcldcb.XoffChar = m_dcb.XoffChar; |
|
348 |
lcldcb.XonLim = m_dcb.XonLim; |
|
349 |
lcldcb.XoffLim = m_dcb.XoffLim; |
|
350 |
lcldcb.fBinary = m_dcb.fBinary; |
|
351 |
lcldcb.fParity = m_dcb.fParity; |
|
352 |
lcldcb.fNull = m_dcb.fNull; |
|
353 |
lcldcb.fAbortOnError = m_dcb.fAbortOnError; |
|
354 |
||
355 |
// DCB has been changed |
|
356 |
// If setting the port went well then we are connected properly! |
|
357 |
if (!SetCommState( m_hSerial, &lcldcb)) |
|
358 |
{ |
|
359 |
m_lastCommError = GetLastError(); |
|
360 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
361 |
} |
|
362 |
||
363 |
return err; |
|
364 |
} |
|
365 |
||
366 |
long CRealSerialComm::ClosePort() |
|
367 |
{ |
|
368 |
COMMLOGOPEN(); |
|
1473
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
369 |
COMMLOGA1("CRealSerialComm::ClosePort connected=%d\n", IsConnected()); |
60 | 370 |
|
371 |
long err = TCAPI_ERR_NONE; |
|
372 |
||
373 |
if (!IsConnected()) |
|
374 |
{ |
|
375 |
||
376 |
err = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
377 |
} |
|
378 |
else if (m_hSerial != INVALID_HANDLE_VALUE) |
|
379 |
{ |
|
380 |
// disable event notification |
|
381 |
SetCommMask( m_hSerial, 0 ); |
|
382 |
||
383 |
// drop DTR |
|
384 |
EscapeCommFunction( m_hSerial, CLRDTR ); |
|
385 |
||
386 |
// purge any outstanding reads/writes and close device handle |
|
387 |
PurgeComm( m_hSerial, |
|
388 |
PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); |
|
389 |
||
390 |
CloseHandle( m_hSerial ); |
|
391 |
m_hSerial = INVALID_HANDLE_VALUE; |
|
392 |
||
393 |
if (m_pBuffer) |
|
394 |
{ |
|
395 |
delete[] m_pBuffer; |
|
396 |
m_pBuffer = NULL; |
|
397 |
} |
|
398 |
m_isConnected = false; |
|
399 |
} |
|
400 |
||
401 |
COMMLOGCLOSE(); |
|
402 |
return err; |
|
403 |
} |
|
404 |
||
405 |
void CRealSerialComm::DeleteMsg(DWORD inMsgLength) |
|
406 |
{ |
|
407 |
if (!IsConnected()) |
|
408 |
return; |
|
409 |
// inMsgLength includes header |
|
410 |
// delete from beginning of buffer |
|
411 |
if (inMsgLength == 0) |
|
412 |
return; |
|
413 |
if (m_numberBytes > 0 && m_numberBytes >= inMsgLength) |
|
414 |
{ |
|
415 |
size_t moveLen = m_numberBytes - inMsgLength; |
|
416 |
if (moveLen > 0) |
|
417 |
memcpy(&m_pBuffer[0], &m_pBuffer[inMsgLength], moveLen); |
|
418 |
m_numberBytes -= inMsgLength; |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
long CRealSerialComm::SendDataToPort(DWORD inSize, const void *inData) |
|
423 |
{ |
|
424 |
||
425 |
long err = TCAPI_ERR_NONE; |
|
426 |
DWORD lclNumBytes=0; |
|
427 |
COMMLOGOPEN(); |
|
428 |
COMMLOGS("CRealSerialComm::SendDataToPort\n"); |
|
429 |
COMMLOGCLOSE(); |
|
430 |
if (!IsConnected()) |
|
431 |
{ |
|
432 |
||
433 |
COMMLOGOPEN(); |
|
434 |
COMMLOGS("CRealSerialComm::SendDataToPort notConnected\n"); |
|
435 |
COMMLOGCLOSE(); |
|
436 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
437 |
} |
|
438 |
||
439 |
if (WriteFile(m_hSerial, inData, inSize, &lclNumBytes, NULL)) |
|
440 |
{ |
|
441 |
// we were successful, but did we send all data? (i.e., a timeout occurred) |
|
442 |
// we are not doing overlapped I/O so if not all data went, then we timed out |
|
443 |
// and there was some kind of error |
|
444 |
if (lclNumBytes != inSize) |
|
445 |
{ |
|
1473
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
446 |
m_lastCommError = 0; |
60 | 447 |
COMMLOGOPEN(); |
1473
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
448 |
COMMLOGA3("CRealSerialComm::SendDataToPort WriteFile not all bytes sent: lclNumBytes=%d inSize=%d err=%d\n", lclNumBytes, inSize, m_lastCommError); |
60 | 449 |
COMMLOGCLOSE(); |
450 |
||
451 |
COMSTAT lclComStat; |
|
452 |
DWORD lclErrorFlags = 0; |
|
453 |
if (!ClearCommError(m_hSerial, &lclErrorFlags, &lclComStat)) |
|
454 |
{ |
|
455 |
// clear comm error returned error (this doesn't normally happen if the handle is valid and port is still open) |
|
456 |
m_lastCommError = GetLastError(); |
|
457 |
err = TCAPI_ERR_COMM_ERROR; |
|
458 |
COMMLOGOPEN(); |
|
459 |
COMMLOGA1("CRealSerialComm::SendDataToPort ClearCommError failed=%d\n", m_lastCommError); |
|
460 |
COMMLOGCLOSE(); |
|
461 |
} |
|
462 |
else |
|
463 |
{ |
|
464 |
// clear comm error returned OK |
|
465 |
// check error flags |
|
466 |
if (lclErrorFlags) |
|
467 |
{ |
|
468 |
// there really was an error |
|
469 |
err = TCAPI_ERR_COMM_ERROR; |
|
470 |
COMMLOGOPEN(); |
|
1473
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
471 |
COMMLOGA1("CRealSerialComm::SendDataToPort ClearCommError succeeded lclErrorFlags=%d\n", lclErrorFlags); |
60 | 472 |
COMMLOGCLOSE(); |
473 |
} |
|
474 |
else |
|
475 |
{ |
|
476 |
// No OS error returned, but WriteFile failed to write out all bytes |
|
477 |
// therefore, since we are not doing overlapped I/O, this is an error. |
|
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
478 |
// DUMPCOMSTAT(&lclComStat); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
479 |
BOOL flush = FlushFileBuffers(m_hSerial); // flush transmit buffer |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
480 |
// ClearCommError(m_hSerial, &lclErrorFlags, &lclComStat); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
481 |
// if (WriteFile(m_hSerial, inData, inSize, &lclNumBytes, NULL)) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
482 |
// { |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
483 |
// COMMLOGOPEN(); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
484 |
// COMMLOGA1("CRealSerialComm::SendDataToPort WriteFile#2 succeeded lclNumBytes=%d\n", lclNumBytes); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
485 |
// COMMLOGCLOSE(); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
486 |
// } |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
487 |
err = TCAPI_ERR_COMM_ERROR_DEVICE_NOT_READING; |
60 | 488 |
COMMLOGOPEN(); |
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
489 |
COMMLOGA2("CRealSerialComm::SendDataToPort ClearCommError succeeded lclErrorFlags=0 err=%d flush=%d\n", m_lastCommError, flush); |
60 | 490 |
COMMLOGCLOSE(); |
491 |
// DUMPCOMSTAT(&lclComStat); |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
else |
|
496 |
{ |
|
497 |
// we sent all the data we requested |
|
498 |
err = TCAPI_ERR_NONE; |
|
499 |
#ifdef _DEBUG |
|
500 |
COMMLOGOPEN(); |
|
501 |
COMMLOGS("CRealSerialComm::SendDataToPort WriteFile successful\n"); |
|
502 |
BYTE* ptr = (BYTE*)inData; |
|
424 | 503 |
long numBytes = (inSize > 80) ? 80 : inSize; |
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
504 |
char msg[300]; |
60 | 505 |
sprintf(msg, "CRealSerialComm::SendDataToPort = "); |
506 |
for (int i = 0; i < numBytes; i++) |
|
507 |
{ |
|
508 |
sprintf(msg, "%s %02.2x", msg, ptr[i]); |
|
509 |
} |
|
510 |
sprintf(msg, "%s\n", msg); |
|
511 |
COMMLOGS(msg); |
|
512 |
COMMLOGCLOSE(); |
|
513 |
#endif |
|
514 |
} |
|
515 |
} |
|
516 |
else |
|
517 |
{ |
|
518 |
// write failed |
|
519 |
m_lastCommError = GetLastError(); |
|
520 |
err = TCAPI_ERR_COMM_ERROR; |
|
521 |
COMMLOGOPEN(); |
|
522 |
COMMLOGA1("CRealSerialComm::SendDataToPort WriteFile failed = %d\n", m_lastCommError); |
|
523 |
COMMLOGCLOSE(); |
|
524 |
} |
|
525 |
||
526 |
return err; |
|
527 |
} |
|
528 |
long CRealSerialComm::PollPort(DWORD &outSize) |
|
529 |
{ |
|
530 |
long err = TCAPI_ERR_NONE; |
|
531 |
outSize = 0; |
|
532 |
||
533 |
COMSTAT lclComStat; |
|
534 |
DWORD lclErrorFlags=0; |
|
535 |
||
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
536 |
if (!IsConnected() || m_hSerial == INVALID_HANDLE_VALUE) |
60 | 537 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
538 |
||
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
539 |
|
60 | 540 |
// Sleep(1); |
541 |
if (!ClearCommError( m_hSerial, &lclErrorFlags, &lclComStat )) |
|
542 |
{ |
|
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
1473
diff
changeset
|
543 |
if (!IsConnected() || m_hSerial == INVALID_HANDLE_VALUE) |
1473
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
544 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
6c45b7c9cdac
Close ports when there is a fatal error allowing OS to free all handles to it. Do not report OSErrors on sending messages as these are confusing.
Chad Peckham <chad.peckham@nokia.com>
parents:
477
diff
changeset
|
545 |
|
60 | 546 |
m_lastCommError = GetLastError(); |
547 |
err = TCAPI_ERR_COMM_ERROR; |
|
548 |
||
549 |
PROCLOGOPEN(); |
|
550 |
PROCLOGA1("CRealSerialComm::PollPort ClearCommError failed=%d\n", m_lastCommError); |
|
551 |
// if (m_lastCommError > 0) |
|
552 |
LogErrorText2(m_lastCommError); |
|
553 |
PROCLOGCLOSE(); |
|
554 |
} |
|
555 |
else |
|
556 |
{ |
|
557 |
// ClearCommError succeeded |
|
558 |
if (lclErrorFlags) |
|
559 |
{ |
|
560 |
m_lastCommError = lclErrorFlags; |
|
561 |
err = TCAPI_ERR_COMM_ERROR; |
|
562 |
PROCLOGOPEN(); |
|
563 |
PROCLOGA1("CRealSerialComm::PollPort ClearCommError succeeded but lclErrorFlags=%d\n", m_lastCommError); |
|
564 |
PROCLOGCLOSE(); |
|
565 |
} |
|
566 |
else |
|
567 |
{ |
|
568 |
// DUMPCOMSTATP(&lclComStat); |
|
569 |
// PROCLOGOPEN(); |
|
570 |
// PROCLOGA1("CRealSerialComm::PollPort ClearCommError succeeded cbInQue=%d\n", lclComStat.cbInQue); |
|
571 |
// PROCLOGCLOSE(); |
|
572 |
m_lastCommError = 0; |
|
573 |
} |
|
574 |
outSize = lclComStat.cbInQue; |
|
575 |
} |
|
576 |
||
577 |
return err; |
|
578 |
} |
|
579 |
#ifdef _DEBUG |
|
580 |
void CRealSerialComm::DumpComStat(COMSTAT* stat) |
|
581 |
{ |
|
582 |
COMMLOGOPEN(); |
|
583 |
COMMLOGA3(" comstat fCtsHold =%d fDsrHold =%d fRlsdHold=%d\n", stat->fCtsHold, stat->fDsrHold, stat->fRlsdHold); |
|
584 |
COMMLOGA3(" comstat fXoffHold=%d fXoffSent=%d fEof =%d\n", stat->fXoffHold, stat->fXoffSent, stat->fEof); |
|
585 |
COMMLOGA3(" comstat fTxim =%d cbInQue =%d cbOutQue =%d\n", stat->fTxim, stat->cbInQue, stat->cbOutQue); |
|
586 |
COMMLOGCLOSE(); |
|
587 |
} |
|
588 |
void CRealSerialComm::DumpComStatP(COMSTAT* stat) |
|
589 |
{ |
|
590 |
PROCLOGOPEN(); |
|
591 |
PROCLOGA3(" comstat fCtsHold =%d fDsrHold =%d fRlsdHold=%d\n", stat->fCtsHold, stat->fDsrHold, stat->fRlsdHold); |
|
592 |
PROCLOGA3(" comstat fXoffHold=%d fXoffSent=%d fEof =%d\n", stat->fXoffHold, stat->fXoffSent, stat->fEof); |
|
593 |
PROCLOGA3(" comstat fTxim =%d cbInQue =%d cbOutQue =%d\n", stat->fTxim, stat->cbInQue, stat->cbOutQue); |
|
594 |
PROCLOGCLOSE(); |
|
595 |
} |
|
596 |
#endif |
|
597 |
long CRealSerialComm::ReadPort(DWORD inSize, void *outData, DWORD &outSize) |
|
598 |
{ |
|
599 |
long err = TCAPI_ERR_NONE; |
|
600 |
outSize = 0; |
|
601 |
||
602 |
COMSTAT lclComStat; |
|
603 |
DWORD lclErrorFlags=0; |
|
604 |
DWORD lclLength; |
|
605 |
if (!IsConnected()) |
|
606 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
607 |
||
608 |
// clear out any errors in the channel and get the length of the buffer |
|
609 |
if (!ClearCommError( m_hSerial, &lclErrorFlags, &lclComStat )) |
|
610 |
{ |
|
611 |
// ClearCommError failed |
|
612 |
m_lastCommError = GetLastError(); |
|
613 |
err = TCAPI_ERR_COMM_ERROR; |
|
614 |
PROCLOGOPEN(); |
|
615 |
PROCLOGA1("CRealSerialComm::ReadPort ClearCommError failed=%d\n", m_lastCommError); |
|
616 |
PROCLOGCLOSE(); |
|
617 |
} |
|
618 |
else |
|
619 |
{ |
|
620 |
if (lclErrorFlags) |
|
621 |
{ |
|
622 |
m_lastCommError = lclErrorFlags; |
|
623 |
err = TCAPI_ERR_COMM_ERROR; |
|
624 |
PROCLOGOPEN(); |
|
625 |
PROCLOGA1("CRealSerialComm::ReadPort ClearCommError succeeded but lclErrorFlags=%d\n", m_lastCommError); |
|
626 |
PROCLOGCLOSE(); |
|
627 |
} |
|
628 |
else |
|
629 |
{ |
|
630 |
m_lastCommError = 0; |
|
631 |
||
632 |
lclLength = min( inSize, lclComStat.cbInQue ); |
|
633 |
||
634 |
if (lclLength > 0) |
|
635 |
{ |
|
636 |
// Read lclLength number of bytes into outData. |
|
637 |
if (!ReadFile(m_hSerial,outData,lclLength,&outSize,NULL)) |
|
638 |
{ |
|
639 |
m_lastCommError = GetLastError(); |
|
640 |
err = TCAPI_ERR_COMM_ERROR; |
|
641 |
PROCLOGOPEN(); |
|
642 |
PROCLOGA1("CRealSerialComm::ReadPort ReadFile failed = %d\n", m_lastCommError); |
|
643 |
PROCLOGCLOSE(); |
|
644 |
} |
|
645 |
else |
|
646 |
{ |
|
647 |
// ReadFile returned successful, check to see all our bytes came in |
|
648 |
// If a timeout happened - we may not get all the data |
|
649 |
if (lclLength != outSize) |
|
650 |
{ |
|
651 |
lclErrorFlags = 0; |
|
652 |
if (!ClearCommError( m_hSerial, &lclErrorFlags, &lclComStat )) |
|
653 |
{ |
|
654 |
// ClearCommError failed |
|
655 |
m_lastCommError = GetLastError(); |
|
656 |
err = TCAPI_ERR_COMM_ERROR; |
|
657 |
PROCLOGOPEN(); |
|
658 |
PROCLOGA1("CRealSerialComm::ReadPort ClearCommError failed=%d\n", m_lastCommError); |
|
659 |
PROCLOGCLOSE(); |
|
660 |
} |
|
661 |
else |
|
662 |
{ |
|
663 |
// ClearCommError succeeded |
|
664 |
if (lclErrorFlags) |
|
665 |
{ |
|
666 |
// there really was an error |
|
667 |
m_lastCommError = lclErrorFlags; |
|
668 |
err = TCAPI_ERR_COMM_ERROR; |
|
669 |
PROCLOGOPEN(); |
|
670 |
PROCLOGA1("CRealSerialComm::ReadPort ReadFile succeeded-not all data read lclErrorFlags=%d\n", m_lastCommError); |
|
671 |
PROCLOGCLOSE(); |
|
672 |
} |
|
673 |
else |
|
674 |
{ |
|
675 |
// Since we are not doing overlapped I/O |
|
676 |
// and our timeout values say to timeout, we should read all the bytes |
|
677 |
// that the last Poll told us, if not this is an error |
|
678 |
err = TCAPI_ERR_COMM_ERROR; |
|
679 |
PROCLOGOPEN(); |
|
680 |
PROCLOGS("CRealSerialComm::ReadPort ReadFile succeeded-not all data read lclErrorFlags=0\n"); |
|
681 |
PROCLOGCLOSE(); |
|
682 |
} |
|
683 |
} |
|
684 |
} |
|
685 |
else |
|
686 |
{ |
|
687 |
// all data read |
|
688 |
m_lastCommError = 0; |
|
689 |
PROCLOGOPEN(); |
|
690 |
PROCLOGS("CRealSerialComm::ReadPort ReadFile successful\n"); |
|
691 |
PROCLOGCLOSE(); |
|
692 |
} |
|
693 |
} |
|
694 |
} |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
return err; |
|
699 |
} |
|
700 |
||
701 |
long CRealSerialComm::ProcessBuffer(CConnection* pConn, CRegistry* pRegistry, long& numberProcessed) |
|
702 |
{ |
|
703 |
PROCLOGOPEN(); |
|
704 |
PROCLOGS("CRealSerialComm::ProcessBuffer\n"); |
|
705 |
PROCLOGCLOSE(); |
|
706 |
||
707 |
long err = TCAPI_ERR_NONE; |
|
708 |
long routingErr = TCAPI_ERR_NONE; |
|
709 |
||
710 |
if (!IsConnected()) |
|
711 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
712 |
||
713 |
if (!m_Protocol) |
|
714 |
return TCAPI_ERR_UNKNOWN_MEDIA_TYPE; |
|
715 |
||
716 |
DWORD protocolHeaderLength = m_Protocol->GetHeaderLength(); |
|
717 |
||
718 |
// fill buffer |
|
719 |
if (m_numberBytes < MAX_SERIAL_MESSAGE_BUFFER_LENGTH) |
|
720 |
{ |
|
721 |
DWORD outLen = 0; |
|
722 |
err = PollPort(outLen); |
|
723 |
if (err == TCAPI_ERR_NONE && outLen > 0) |
|
724 |
{ |
|
725 |
if (outLen > (MAX_SERIAL_MESSAGE_BUFFER_LENGTH - m_numberBytes)) |
|
726 |
outLen = MAX_SERIAL_MESSAGE_BUFFER_LENGTH - m_numberBytes; |
|
727 |
BYTE *ptr = &m_pBuffer[m_numberBytes]; |
|
728 |
err = ReadPort(outLen, ptr, outLen); |
|
729 |
if (err == TCAPI_ERR_NONE && outLen > 0) |
|
730 |
{ |
|
731 |
m_numberBytes += outLen; |
|
732 |
} |
|
733 |
} |
|
734 |
} |
|
735 |
// now process buffer but only for complete messages |
|
736 |
if (err == TCAPI_ERR_NONE) |
|
737 |
{ |
|
738 |
if (m_numberBytes >= protocolHeaderLength) |
|
739 |
{ |
|
740 |
BYTE* ptr = m_pBuffer; |
|
741 |
long bytesRemaining = m_numberBytes; |
|
742 |
long usedLen = 0; |
|
743 |
bool done = false; |
|
458 | 744 |
long numberSkipped=0; |
60 | 745 |
|
746 |
while (!done) |
|
747 |
{ |
|
748 |
DWORD fullMessageLength = bytesRemaining; |
|
749 |
DWORD rawLength = 0; |
|
750 |
BYTE* fullMessage = ptr; |
|
751 |
BYTE* rawMessage = ptr; |
|
752 |
BYTE msgId = 0; |
|
458 | 753 |
int result = m_Protocol->DecodeMessage(fullMessage, fullMessageLength, msgId, rawMessage, rawLength); |
754 |
if (result == DECODE_NOT_ENOUGH_BYTES_TO_SEARCH) |
|
755 |
{ |
|
756 |
done = true; |
|
757 |
} |
|
758 |
else if (result == DECODE_MESSAGE_NOT_FOUND) |
|
759 |
{ |
|
760 |
numberSkipped++; |
|
761 |
usedLen += fullMessageLength; |
|
762 |
bytesRemaining -= fullMessageLength; |
|
763 |
ptr += fullMessageLength; |
|
764 |
if (bytesRemaining < protocolHeaderLength) |
|
765 |
done = true; |
|
766 |
} |
|
767 |
else if (result == DECODE_MESSAGE_FOUND) |
|
60 | 768 |
{ |
769 |
err = PreProcessMessage(msgId, fullMessageLength, fullMessage); |
|
770 |
if (err != TCAPI_ERR_NONE) |
|
771 |
{ |
|
458 | 772 |
PROCLOGOPEN(); |
773 |
PROCLOGA1("CTcpComm::ProcessBuffer Notify err = %x\n", err); |
|
774 |
PROCLOGCLOSE(); |
|
60 | 775 |
// notify all clients right now |
776 |
pConn->NotifyClientsCommError(err, false, 0); |
|
777 |
err = TCAPI_ERR_NONE; |
|
778 |
} |
|
779 |
#ifdef _DEBUG |
|
780 |
int reallen = fullMessageLength; |
|
458 | 781 |
if (reallen > 80) reallen = 80; |
60 | 782 |
char msg[6]; |
783 |
msg[0] = '\0'; |
|
784 |
||
477
cedd5978e2bc
Debug logging changes
Chad Peckham <chad.peckham@nokia.com>
parents:
458
diff
changeset
|
785 |
sLogMsg[0] = '\0'; |
60 | 786 |
if (reallen > 0) |
787 |
{ |
|
477
cedd5978e2bc
Debug logging changes
Chad Peckham <chad.peckham@nokia.com>
parents:
458
diff
changeset
|
788 |
sLogMsg[0] = '\0'; |
60 | 789 |
for (int i = 0; i < reallen; i++) |
790 |
{ |
|
458 | 791 |
if (isalnum(ptr[i])) |
792 |
{ |
|
793 |
sprintf(msg, "%c", ptr[i]); |
|
794 |
} |
|
795 |
else |
|
796 |
{ |
|
797 |
sprintf(msg, "%02.2x ", ptr[i]); |
|
798 |
} |
|
477
cedd5978e2bc
Debug logging changes
Chad Peckham <chad.peckham@nokia.com>
parents:
458
diff
changeset
|
799 |
strcat(sLogMsg, msg); |
60 | 800 |
} |
801 |
} |
|
802 |
#endif |
|
803 |
PROCLOGOPEN(); |
|
477
cedd5978e2bc
Debug logging changes
Chad Peckham <chad.peckham@nokia.com>
parents:
458
diff
changeset
|
804 |
PROCLOGA5("CRealSerialComm::ProcessBuffer - RouteMesssage pRegistry = %x id=%x len=%d len=%d msg=%s\n", pRegistry, msgId, fullMessageLength, rawLength, sLogMsg); |
60 | 805 |
PROCLOGCLOSE(); |
806 |
||
807 |
err = pRegistry->RouteMessage(msgId, fullMessage, fullMessageLength, rawMessage, rawLength); |
|
808 |
if (err != TCAPI_ERR_NONE) routingErr = err; // saved for future |
|
809 |
||
810 |
numberProcessed++; |
|
811 |
usedLen += fullMessageLength; |
|
812 |
bytesRemaining -= fullMessageLength; |
|
813 |
ptr += fullMessageLength; |
|
814 |
if (bytesRemaining < protocolHeaderLength) |
|
815 |
done = true; |
|
816 |
} |
|
817 |
} |
|
818 |
DeleteMsg(usedLen); |
|
819 |
} |
|
820 |
} |
|
821 |
// PROCLOGOPEN(); |
|
822 |
// PROCLOGA1("CRealSerialComm::ProcessBuffer err = %d\n", err); |
|
823 |
// PROCLOGCLOSE(); |
|
824 |
if (routingErr == TCAPI_ERR_NONE) |
|
825 |
return err; |
|
826 |
else |
|
827 |
return routingErr; |
|
828 |
} |
|
829 |
bool CRealSerialComm::IsConnectionEqual(ConnectData* pConn) |
|
830 |
{ |
|
831 |
bool equal = false; |
|
832 |
||
833 |
// forms accepted: |
|
834 |
// "comNN", "NN" |
|
835 |
char* ptr1 = m_ConnectSettings->realSerialSettings.comPort; |
|
836 |
char* ptr2 = pConn->realSerialSettings.comPort; |
|
837 |
bool digit1found = false; |
|
838 |
while(!digit1found && *ptr1 != NULL) |
|
839 |
{ |
|
840 |
if (*ptr1 >= '0' && *ptr1 <= '9') |
|
841 |
{ |
|
842 |
digit1found = true; |
|
843 |
break; |
|
844 |
} |
|
845 |
ptr1++; |
|
846 |
} |
|
847 |
bool digit2found = false; |
|
848 |
while(!digit2found && *ptr2 != NULL) |
|
849 |
{ |
|
850 |
if (*ptr2 >= '0' && *ptr2 <= '9') |
|
851 |
{ |
|
852 |
digit2found = true; |
|
853 |
break; |
|
854 |
} |
|
855 |
ptr2++; |
|
856 |
} |
|
857 |
if (digit1found && digit2found) |
|
858 |
{ |
|
859 |
if (strcmp(ptr1, ptr2) == 0) |
|
860 |
equal = true; |
|
861 |
} |
|
862 |
return equal; |
|
863 |
} |
|
864 |
||
865 |
#ifdef _DEBUG |
|
866 |
DWORD CRealSerialComm::GetErrorText(DWORD inError) |
|
867 |
{ |
|
868 |
LPVOID lpMsgBuf; |
|
869 |
||
870 |
if (inError == 0) return inError; |
|
871 |
||
872 |
FormatMessage( |
|
873 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
|
874 |
NULL, |
|
875 |
inError, |
|
876 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
877 |
(LPTSTR) &lpMsgBuf, |
|
878 |
0, |
|
879 |
NULL ); |
|
880 |
||
881 |
COMMLOGA1(" -- GetErrorText=%s", lpMsgBuf); |
|
882 |
// Free the buffer. |
|
883 |
LocalFree( lpMsgBuf ); |
|
884 |
||
885 |
return inError; |
|
886 |
} |
|
887 |
DWORD CRealSerialComm::GetErrorText2(DWORD inError) |
|
888 |
{ |
|
889 |
LPVOID lpMsgBuf; |
|
890 |
||
891 |
if (inError == 0) return inError; |
|
892 |
||
893 |
FormatMessage( |
|
894 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
|
895 |
NULL, |
|
896 |
inError, |
|
897 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
898 |
(LPTSTR) &lpMsgBuf, |
|
899 |
0, |
|
900 |
NULL ); |
|
901 |
||
902 |
PROCLOGA1(" -- GetErrorText=%s", lpMsgBuf); |
|
903 |
// Free the buffer. |
|
904 |
LocalFree( lpMsgBuf ); |
|
905 |
||
906 |
return inError; |
|
907 |
} |
|
908 |
||
909 |
void CRealSerialComm::DumpBuffer(BYTE* ptr, long length) |
|
910 |
{ |
|
911 |
char msg[256] = {0}; |
|
912 |
if (length > 50) length = 50; |
|
913 |
for (int i = 0; i < length; i++) |
|
914 |
{ |
|
915 |
sprintf(msg, "%s%02.2X ", msg, ptr[i]); |
|
916 |
} |
|
917 |
sprintf(msg, "%s\n", msg); |
|
918 |
PROCLOGS(msg); |
|
919 |
} |
|
920 |
#endif |