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