/**************************************************************************
<MODULE NAME>
	<FILENAME></FILENAME>
	<HISTORY>
		<MODIFICATION>
			<DATE>09/06/2006</DATE>
			<AUTHOR>DRO</AUTHOR>
			<DESCRIPTION>
				Create one interface for
			</DESCRIPTION>
		</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/
/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <bios.h>
#include <CONIO.H>
#include <stdlib.h>
#include "..\comp\cardread\CdRdIDig.h"
#include "..\comp\TransCod\TrsCdDig.h"
#include "..\comp\CardRead\StMchDig\StCmdDig.h"


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME>CCardReaderDig</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderDig::CCardReaderDig(void)
{
	m_pcStateMachine = CStCommandDig::Instance();
	m_sParametersInit.iPort = 0;
	m_sParametersInit.iSetting = 0;
	m_sParametersInit.iIoAddr = 0;
	m_sParametersInit.iIrq = 0;
	m_sParametersInit.iBaudrate = 0;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME>~CCardReaderDig</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderDig::~CCardReaderDig(void)
{
	//CStateMachineDigCard::Destroy();
	//if(m_pcStateMachine != NULL)
	//	delete m_pcStateMachine;

	//m_pcStateMachine = NULL;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
CCardReaderManager* CCardReaderDig::Instance(void)
{
	if(m_pcInst == NULL)
	{
		m_pcInst = new CCardReaderDig();
	}
	return m_pcInst;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::LecteurInit(s_Parameters* shParm)
{
	m_sParametersInit.iPort = shParm->iPort;
	m_sParametersInit.iSetting = shParm->iSetting;
	return LecteurInit();
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::LecteurInit(void)
{
	int iResult;
	ResetRetries();
	strcpy(m_szInitString,"C0200402001210"); 
	iResult = m_chSerial.Open(m_sParametersInit.iPort, m_sParametersInit.iSetting);

#ifdef STESUD_TRACE
	CTrace::Instance()->Show("LecteurInit");
#endif

	if(iResult >= 0)
	{
		iResult = m_pcStateMachine->SendCommand(this, m_szInitString);
		if((iResult == 0) && (m_Result.sResponse.cType == 'P'))
		{
			m_eTypeReader = (e_TypeReader)(m_Result.sResponse.szData[2] - '0');
		}
		else if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
		{
			iResult = TestRetour(m_Result.sResponse.cStatusCode);
			if(iResult == A_NORMAL_CARD_DETECTED)
				iResult = 0;
		}

	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ReadCommande(char* pzLectBuffer)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("ReadCommande");
#endif

	ResetRetries();

	iResult = m_pcStateMachine->SendCommand(this, "C63");

	if(iResult == 0 )
	{
		if(m_Result.sResponse.cType == 'P' )
		{
			char szTp[128];
			memset(szTp, 0, strlen(szTp));
			if((m_iNbrCharValid-5) <= 128)
			{
				strcpy(szTp, m_Result.sResponse.szData);
				szTp[strlen(szTp)-2]=0x00;
				strcpy(pzLectBuffer, CTransCodeDigivote::Instance()->Decoding(szTp));
			}
			else
			{
				iResult = ERROR_LENGTH_READ_COMMAND;
			}
		}
		else if(m_Result.sResponse.cType == 'N' )
		{
			iResult = TestRetour(m_Result.sResponse.cStatusCode);
		}
		else
		{
			iResult = ERROR_INVALID_RESPONSE;
		}
	}
	else /*PMD*/
	{
		iResult = ERROR_READER_CARD;
	}

	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::WriteCommande(const char* pzString)
{
	int iResult = 0;
	char szTmp[120];
	char szTmp2[120];


	ResetRetries();

	if(m_eTypeReader == eReadWrite)
	{
		sprintf(szTmp2,"%s",CTransCodeDigivote::Instance()->Encoding(pzString));

		if(strlen(szTmp2) > 104)
			szTmp2[104] = '\0';

		sprintf(szTmp,"C73%s",szTmp2);

	#ifdef STESUD_TRACE
		CTrace::Instance()->Show("WriteCommande %s", szTmp);
	#endif

		iResult = m_pcStateMachine->SendCommand(this, szTmp);

		if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
		{
			iResult = TestRetour(m_Result.sResponse.cStatusCode);
		}
	}
	else
	{
		iResult = ERROR_READ_ONLY;
	}

	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::EjectCommande(unsigned char ucDirection)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("EjectCommande");
#endif
	/* Clear retries coumter*/

	ResetRetries();

	if(ucDirection == EJECT_FRONT)
	{
		/* Eject card to front*/
		iResult = m_pcStateMachine->SendCommand(this, "C30");
		
		if(iResult == 0)
		{
			if(m_Result.sResponse.cType == 'P')
			{
				iResult = ReturnPosition();
			}// end of if(iResult == 0 && m_Result.sResponse.cType == 'P')
			else if(m_Result.sResponse.cType == 'N' )
			{
				iResult = TestRetour(m_Result.sResponse.cStatusCode);
			}
			else
			{
				iResult = ERROR_READER_CARD;/*PMD*/
			}
		}
	}// end of if(ucDirection == EJECT_FRONT)
	else
	{
		/* Eject card to rear*/
		iResult = CaptureCard();/*PMD*/
	}

	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::StatusCommande(void)
{
	int iReturn;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("StatusCommande");
#endif
	ResetRetries();

	iReturn = m_pcStateMachine->SendCommand(this, "C10");
	if(iReturn == 0)
	{
		if(m_Result.sResponse.cType == 'P')
		{
			/*
			if(m_Result.sResponse.cStatusCode[1] >= '0' && m_Result.sResponse.cStatusCode[1] <= '9')
				iReturn = m_Result.sResponse.cStatusCode[1] - '0';

			if(m_Result.sResponse.cStatusCode[0] >= '0' && m_Result.sResponse.cStatusCode[0] <= '9')
				iReturn += ((m_Result.sResponse.cStatusCode[0] - '0') * 10);
			*/
			iReturn = ReturnPosition();

		}
		if(m_Result.sResponse.cType == 'N' )
		{
			iReturn = TestRetour(m_Result.sResponse.cStatusCode) * -1;
		}
	}
	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::Tempo(void)
{
	int iResult = 0;
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::WaitCard(void)
{
	int iReturn;

#ifdef STESUD_TRACE
	CTrace::Instance()->Show("WaitCard");
#endif
	ResetRetries();

	iReturn = m_pcStateMachine->SendCommand(this, "C:0");
	if((iReturn == 0) && (m_Result.sResponse.cType == 'P'))
	{
		iReturn = ReturnPosition();
	}
	else if((iReturn == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iReturn = TestRetour(m_Result.sResponse.cStatusCode);
	}
	else
	{
		iReturn = ERROR_READER_CARD;/*PMD*/
	}


	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
void CCardReaderDig::CheckQueue(void)
{
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::EnableWrite(void)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("EnableWrite");
#endif

	 iResult = m_pcStateMachine->SendCommand(this, "C:4");

	if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iResult = TestRetour(m_Result.sResponse.cStatusCode);
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::Enable(void)
{
	int iReturn;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("Enable");
#endif
	ResetRetries();

	iReturn = m_pcStateMachine->SendCommand(this, "C:0");
	if((iReturn == 0) && (m_Result.sResponse.cType == 'P'))
	{
		iReturn = ReturnPosition();
	}
	else if((iReturn == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iReturn = TestRetour(m_Result.sResponse.cStatusCode);
	}

	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::Disable(void)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("Disable");
#endif

	iResult = m_pcStateMachine->SendCommand(this, "C:1");
	if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iResult = TestRetour(m_Result.sResponse.cStatusCode);
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::WaitTillCardRemoved(void)
{
	int iReturn;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("WaitTillCardRemoved");
#endif
	iReturn =  m_pcStateMachine->SendCommand(this, "C11");
	if(iReturn == 0)
	{
		if(m_Result.sResponse.cType == 'P' )
		{
			iReturn = ReturnPosition();
		}
		else if(m_Result.sResponse.cType == 'N' )
		{
			iReturn = TestRetour(m_Result.sResponse.cStatusCode);
		}
	}
	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::CaptureCard(void)
{
	int iResult;

	iResult = m_pcStateMachine->SendCommand(this, "C31");
	if(iResult == 0)
	{
		if(m_Result.sResponse.cType == 'P' )
		{
			iResult = ReturnPosition();
		}
		else if(m_Result.sResponse.cType == 'N' )
		{
			iResult = TestRetour(m_Result.sResponse.cStatusCode);
		}
		else
		{
			iResult = ERROR_READER_CARD;/*PMD*/
		}
	}
	return iResult;
}
/*************************************************************************/
int CCardReaderDig::CardPosition(void)/*PMD*/
{
	int iResult;

	iResult = m_pcStateMachine->SendCommand(this, "C3032");
	if(iResult == 0)
	{
		if(m_Result.sResponse.cType == 'P')
		{
			iResult = ReturnPosition();
		}
		else if(m_Result.sResponse.cType == 'N')
		{
			iResult = TestRetour(m_Result.sResponse.cStatusCode);
		}
		else
		{
			iResult = ERROR_READER_CARD;/*PMD*/
		}
	}
	return iResult;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ClearMemory(void)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("ClearMemory");
#endif
	iResult = m_pcStateMachine->SendCommand(this, "C66");
	if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iResult = TestRetour(m_Result.sResponse.cStatusCode);
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME>ReSendCommand</NAME>
	<TYPE>LOCAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ReSendCommand(void)
{
	#ifdef STESUD_TRACE
		CTrace::Instance()->Show("ReSendCommand");
	#endif
	return m_chSerial.SendData(m_szCommand, strlen(m_szCommand));
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::SendCommand(char* Command)
{
	int iResult;
	#ifdef STESUD_TRACE
		CTrace::Instance()->Show("CCardReaderDig::SendCommand");
	#endif
	memset(m_Result.szResult, 0,sizeof(m_Result.szResult));/*PMD*/
	iResult = BuildFrame(Command, 1);
	if(iResult >= 0)
	{
		m_iCounterTimeout = 0;
		m_iCounterRetries = 0;
		iResult = m_chSerial.SendData(m_szCommand, strlen(m_szCommand));
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ChangeState(CStateMachineDigCard* pcState)
{
	m_pcStateMachine = pcState;
	return 0;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::CancelCommand(void)
{
	return m_pcStateMachine->CancelCommand(this);
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::SendEnq(void)
{
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("SendEnq");
#endif
	return m_chSerial.WriteByte(eEnq);
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::SendDleEot(void)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("SendDleEot");
#endif
	iResult = m_chSerial.WriteByte(eDle);
	if(iResult == ACK)
		iResult = m_chSerial.WriteByte(eEot);
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::BuildFrame(char* pzData, uchar ucBcc, uchar ucCR)
{
	int iResult = 0;
	char cBcc = eEtx;
	int iCounter = 0;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CCardReaderDig::BuildFrame");
#endif

	/* Clear command buffer */
	memset(m_szCommand, 0, sizeof(m_szCommand));

	/* Start Frame */
	m_szCommand[iCounter++] = eStx; //STX

	/* Insert command and data*/
	strcat(m_szCommand, pzData);
	iCounter += strlen(pzData);
	m_szCommand[iCounter++] = eEtx; //ETX

	if(ucBcc)
	{
		/* compute Bcc without ETX */
		cBcc ^= BuildBcc(pzData);
		m_szCommand[iCounter++] = cBcc;
	}

	if(ucCR)
	{
		m_szCommand[iCounter++] = eCr;
	}

	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::WaitAck(void)
{
	int iResult = ERROR_WAIT_ACK;
	unsigned long  ulClock, ulComputeClock;
	char cQuit = 0;

#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CCardReaderDig::WaitAck ");
#endif

	/* store the value of tick */
	ulClock = biostime(0,0l);
	do
	{
		char cTmp = 0;
		int iReadData =0;
		char cReceiveDle = 0;
		iReadData = m_chSerial.ReadByte();
/* << 9.06 */
/*		if((iReadData & 0x8E00) != 0) */
		if((iReadData & 0x9E00) != 0) // error
/* 9.06 >> */
		{
			iResult = ERROR_TIMEOUT_SERIAL;
/* << 9.06 */
/*			break;*/
			return iResult;
/* 9.06 >> */

		}

		cTmp = (iReadData & 0x00ff);

		/*Get tick pc. Warning at midnight the value is given 0*/
		if((ulComputeClock = biostime(0,0l)) < ulClock)
			ulComputeClock += UTT_TICK_IN_A_DAY;
		ulComputeClock -= ulClock;
		switch(cTmp)
		{
		case eAck:
			{
				iResult = m_pcStateMachine->ReceiveAck(this);
				cQuit = 1;
				break;
			}
		case eNack:
			{
				iResult = m_pcStateMachine->ReceiveNack(this);
				cQuit = 1;
				break;
			}
		case eDle:
			{
				//iResult = m_pcStateMachine->ReceiveDleEot(this);
				cReceiveDle = 1;
				break;
			}
		case eEot:
			{
				if(cReceiveDle == 1)
				{
					iResult = m_pcStateMachine->ReceiveDleEot(this);
					cQuit = 1;
				}
				break;
			}
		}
	}
	while (ulComputeClock < TIME_OUT_CARD && cQuit == 0);

	if(ulComputeClock >= TIME_OUT_CARD && cQuit == 0)
	{
		iResult = m_pcStateMachine->ReceiveTimeOut(this);
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::WaitResponce(void)
{
	int iResult = ERROR_WAIT_RESPONCE;
	unsigned long  ulRepSerial;
	#ifdef STESUD_TRACE
		CTrace::Instance()->Show("CCardReaderDig::WaitResponce ");
	#endif

	//Make sure that the result contains no old data
	memset(m_Result.szResult, 0,sizeof(m_Result.szResult));

	/* Read data on serial port*/
	ulRepSerial = m_chSerial.ReadDataWaitingETX(m_Result.szResult, (sizeof(m_Result.szResult)-1));
	if((ulRepSerial & 0xFFFF0000) == 0)
	{/* ok*/

		iResult = ValidFrame(1, 0);
		if(iResult == 0)
		{
			#ifdef STESUD_TRACE
				ShowResult();
			#endif
				;
		}
		else
		{
			#ifdef STESUD_TRACE
				int ii = 0;
				CTrace::Instance()->SetTrace(1);
				CTrace::Instance()->Show("The frame is not valid");

				for(; ii < strlen(m_Result.szResult);ii++)
					CTrace::Instance()->ShowNoLineFeed("0X%X ", m_Result.szResult[ii]);
				CTrace::Instance()->Show("");
				CTrace::Instance()->SetTrace(0);
			#endif
				;
		}
	}/* Serial port error */
	else if((ulRepSerial & 0xFFFF0000) == TIME_OUT_ERROR_SERIAL)
	{
			#ifdef STESUD_TRACE
				CTrace::Instance()->Show("timeout");
			#endif

			iResult = m_pcStateMachine->ReceiveTimeOut(this);
	}
/* << 9.06 */
	else
	{
		iResult = ERROR_TIMEOUT_SERIAL;
	}
/* 9.06 >> */
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ValidFrame(uchar ucBcc, uchar ucCR)
{
	int iResult = ERROR_INVALID_FRAME;
	char* pzStx = NULL;
	char* pzStx1 = NULL;
	char* pzTp = NULL;
	m_iNbrCharValid = 0;
	/* The responce */
	char szData[128];
	char* pzData;

#ifdef STESUD_TRACE
	CTrace::Instance()->Show("ValidFrame");
#endif
	memset(szData, 0, sizeof(szData));
	strcpy(szData, m_Result.szResult);
	pzData = szData;

	/*Search stx*/
	pzTp = pzData;
	while(*pzTp)
	{
		if(*pzTp == eStx)
		{
			char* pzEtx = NULL;
			pzStx = pzTp;
			pzStx1 = pzStx;

			/* pass Stx*/
			pzStx++;

			while(*pzTp)
			{
				if(*pzTp == eEtx)
				{
					char cReceiveBcc, cComputeBcc;
					pzEtx = pzTp;
					iResult = ACK;
					/* numbres of char valid*/
					m_iNbrCharValid = pzEtx - pzStx;
					if(ucBcc == 1)
					{
						if(*(++pzTp))
						{
							cReceiveBcc = *pzTp;
							char szTempo[128];
							/*Clear buffer*/
							memset(szTempo, 0, sizeof(szTempo));
							/*for compute Bcc witch Etx*/
							memcpy(szTempo, pzStx, m_iNbrCharValid + 1);
							cComputeBcc = BuildBcc(szTempo);
							if(cReceiveBcc != cComputeBcc)
							{
								iResult = ERROR_BCC;
							}
						}
						else
						{
							iResult = ERROR_LENGTH_READ_COMMAND;
						}
					}
					if(iResult == ACK)
					{
						if(ucCR == 1)
						{
							if(*(++pzTp))
							{
								if(*pzTp != eCr)
									iResult = ERROR_CR;
							}
							else
							{
								iResult = ERROR_LENGTH_READ_COMMAND;
							}
						}
					}
					if(pzData != pzStx1)
					{
						/**/
						memset(m_Result.szResult, 0, sizeof(m_Result.szResult));
						strcpy(m_Result.szResult, pzStx1);
					}
					return iResult;
				}// if(*pzTp == eEtx)
				pzTp++;
			}// while(*pzTp)
		} // if(*pzTp == eStx)
		else
		{
			pzTp++;
		}
	}	// while(*pzTp)
	return iResult;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
char CCardReaderDig::BuildBcc(char* pzData)
{
	char cBcc = 0;
	int iCounter2;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CCardReaderDig::BuildBcc");
#endif

	for(iCounter2 = 0; iCounter2 < strlen(pzData); iCounter2++)
	{
		cBcc ^= pzData[iCounter2];
	}
	return cBcc;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME>TimeOut</NAME>
	<TYPE>LOCAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::TimeOut(void)
{
	if(m_iCounterTimeout++ < MAX_TIMEOUT)
		return 0;
	else
		return ERROR_TIMEOUT_CARD;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME>TestRetour</NAME>
	<TYPE>LOCAL</TYPE>
	<DESCRIPTION>Transform a char into interger</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>etat</NAME>
			<TYPE>char</TYPE>
			<IO>IN</IO>
			<DESCRIPTION>
				Character transformed into integer
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
			0	ACK
			1	Unknown Commande
			2	Reading Error
			3	Writing Error
			5	Str too long
			6	Card Rejected
			7	Card Rejected
			8	No Card
			9	Transmission Error
			a	Abnormal Card Detected
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::TestRetour(char* pzEtat)
{
	int iStatus = 0;

	if(*pzEtat == 'x')
	{
		iStatus = ACK;  //     ACK
	}
	else if(*pzEtat == '0')
	{
		if(*(pzEtat+1) == '4')
			iStatus = STR_TOO_LONG;  //    STR_TOO_LONG
		else
			iStatus = UNKNOWN_COMMAND;  //     Unknown Commande
	}
	else if(*pzEtat == '1')
	{
		iStatus = A_NORMAL_CARD_DETECTED; //     Abnormal Card Detected
	}
	else if(*pzEtat == '2')
	{
		if(*(pzEtat+1) == '0')
		{
			iStatus = READING_ERROR;  //     Reading Error
		}
		else if(*(pzEtat+1) == '1')
		{
			iStatus = READING_ERROR;  //     Reading Error
		}
		else if(*(pzEtat+1) == '2')
		{
			iStatus = WRITING_ERROR;  //     Writing Error
		}
		else
		{
			iStatus = TIMEOUT_ERROR;
		}
	}
	else
	{
		iStatus = ERROR_READER_CARD; /*PMD*/
	}

	/*
	else if(strcmp(pzEtat,"") == 0)
	{
		iStatus = 9;  //     Transmission Error
	}
	else if(strcmp(pzEtat,"04") == 0)
	{
		iStatus = 5;  //     Str too long
	}
	else if(strcmp(pzEtat,"") == 0)
	{
		iStatus = 6;  //     Card Rejected
	}
	else if(strcmp(pzEtat,"") == 0)
	{
		iStatus = 8;  //     No Card      ????
	} */
	return(iStatus);
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
char* CCardReaderDig::CommandDirect(char cCommande, char cParam, char* pzData=NULL)
{
	#ifdef STESUD_TRACE
		if(pzData == NULL)
			CTrace::Instance()->Show("CommandDirect(%x, %x)", cCommande, cParam);
		else
			CTrace::Instance()->Show("CommandDirect(%x, %x, %s)", cCommande, cParam, pzData);
	#endif

	int iResult = 0;
	char szTmp[128];
	char szTmp2[120];
	ResetRetries();


	//Make sure that the result contains no old data
	memset(m_Result.szResult, 0,sizeof(m_Result.szResult));

	if(pzData != NULL)
	{
		//sprintf(szTmp2,"%s",CTransCodeDigivote::Instance()->Encoding(pzData));
		sprintf(szTmp2, "%s", pzData);
		if(strlen(szTmp2) > 104)
			szTmp2[104] = '\0';

		sprintf(szTmp,"C%c%c%s",cCommande, cParam, szTmp2);
	}
	else
	{
		sprintf(szTmp,"C%c%c",cCommande, cParam);
	}

	iResult = m_pcStateMachine->SendCommand(this, szTmp);
    
	if((iResult == 0) && (m_Result.sResponse.cType == 'N'))
	{
		iResult = TestRetour(m_Result.sResponse.cStatusCode);
		strcpy(szTmp, m_Result.szResult);
	}
	else
	{
		strcpy(szTmp, m_Result.szResult);
	}

	return szTmp;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::GetVersionCommande(char* pzLectBuffer)
{
	int iResult = 0;

	CommandDirect(0x3C, 0x30); 

	if(m_Result.sResponse.cType == 'P' )
	{
		if((m_iNbrCharValid-5) <= 128)
		{
			strcpy(pzLectBuffer, m_Result.sResponse.szData);
			pzLectBuffer[strlen(pzLectBuffer)-2]=0x00;
		}
		else
		{
			iResult = ERROR_LENGTH_READ_COMMAND;
		}
	}
	else if(m_Result.sResponse.cType == 'N')/*PMD*/
	{
		strcpy(pzLectBuffer,"UNKNOWN VERSION");/*PMD*/
	}

	return iResult;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::GetProtocol(char* pzLectBuffer)
{
	int iResult = 0;
	CommandDirect(0x3f, 0x30);
	if(m_Result.sResponse.cType == 'P' )
	{
		if((m_iNbrCharValid-5) <= 128)
		{
			strcpy(pzLectBuffer, m_Result.sResponse.szData);
			pzLectBuffer[strlen(pzLectBuffer)-2]=0x00;
		}
		else
		{
			iResult = ERROR_LENGTH_READ_COMMAND;
		}
	}

	return iResult;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::CloseConnection(void)
{
	return m_chSerial.Close();
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::SetRetries(void)
{
	int i;
/* << 9.06 */
/*	if(m_iCounterRetries++ < MAX_RETIES + 2)*/
	if(m_iCounterRetries++ < MAX_RETIES )
/* 9.06 >> */
		i = 0;
	else
		i = 1;
	return i;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderDig::ReturnPosition(void)
{
	int iResult;
	switch(m_Result.sResponse.cStatusCode[1])
	{
	case '0':
		iResult = CARD_NO;
		break;
	case '1':
		iResult = CARD_GATE;
		break;
	case '2':
		iResult = CARD_HOME;
		break;
	default:
		{
			iResult = ERROR_READER_CARD;/*PMD*/
		}
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderDig</CLASS>
	<NAME></NAME>
	<TYPE>LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
#ifdef STESUD_TRACE
void CCardReaderDig::ShowResult(void)
{
	uchar i = 0;
	/*if(m_Result.szResult[1] == 'P')
	{
		CTrace::Instance()->Show("WaitResponce::Reponce P");
	}
	else*/
	if(m_Result.szResult[1] == 'N')
	{
		CTrace::Instance()->SetTrace(1);
		CTrace::Instance()->ShowNoLineFeed("CardReader: N ");
		CTrace::Instance()->Show("_@%s@_ ",m_Result.szResult);

		switch(m_Result.sResponse.cCommandCode)
		{
		case 0x30:
			CTrace::Instance()->ShowNoLineFeed("Initialyse ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x30:
					CTrace::Instance()->ShowNoLineFeed("Move card to gate ");
					break;
				case 0x31:
					CTrace::Instance()->ShowNoLineFeed("Capture card backward ");
					break;
				case 0x32:
					CTrace::Instance()->ShowNoLineFeed("Detian the card ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		case 0x31:
			CTrace::Instance()->ShowNoLineFeed("Sattus Request ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x30:
				case 0x31:
					CTrace::Instance()->ShowNoLineFeed("Position card ");
					break;
				case 0x32:
					CTrace::Instance()->ShowNoLineFeed("Write Result ");
					break;
				case 0x33:
					CTrace::Instance()->ShowNoLineFeed("Read Result ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		case 0x33:
			CTrace::Instance()->ShowNoLineFeed("Eject ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x30:
					CTrace::Instance()->ShowNoLineFeed("Front ");
				case 0x31:
					CTrace::Instance()->ShowNoLineFeed("back ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		case 0x36:
			CTrace::Instance()->ShowNoLineFeed("Read ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x30:
					CTrace::Instance()->ShowNoLineFeed(", Move card (cleaning) ");
					break;
				case 0x33:
					CTrace::Instance()->ShowNoLineFeed("Track3 and transmit data ");
					break;
				case 0x36:
					CTrace::Instance()->ShowNoLineFeed(", clear buffer ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		case 0x37:
			CTrace::Instance()->ShowNoLineFeed("Write ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x33:
					CTrace::Instance()->ShowNoLineFeed("Track  3 ");
					break;
				case 0x38:
					CTrace::Instance()->ShowNoLineFeed("data transmit ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		case 0x3a:
			CTrace::Instance()->ShowNoLineFeed("Enable ");
				switch(m_Result.sResponse.cParameterCode)
				{
				case 0x30:
					CTrace::Instance()->ShowNoLineFeed("enable card accept ");
					break;
				case 0x31:
					CTrace::Instance()->ShowNoLineFeed("disable card accept ");
					break;
				case 0x34:
					CTrace::Instance()->ShowNoLineFeed("Enable card accept with write function ");
					break;
				default:
					CTrace::Instance()->ShowNoLineFeed("Param ??? ");
					break;
				}
			break;
		default:
			CTrace::Instance()->ShowNoLineFeed("Command ?? ");
			break;
		}


		switch(m_Result.sResponse.cStatusCode[0])
		{
		case 0x30:
			switch(m_Result.sResponse.cStatusCode[1])
			{
			case 0x30:
				CTrace::Instance()->ShowNoLineFeed("Command unidentified ");
				break;
			case 0x31:
				CTrace::Instance()->ShowNoLineFeed("Parameter incorrect ");
				break;
			case 0x32:
				CTrace::Instance()->ShowNoLineFeed("Command cannot be executed ");
				break;
			case 0x34:
				CTrace::Instance()->ShowNoLineFeed("Command data error ");
				break;
			}
			break;
		case 0x31:
			switch(m_Result.sResponse.cStatusCode[1])
			{
			case 0x30:
				CTrace::Instance()->ShowNoLineFeed("Card jammed");
				break;
			case 0x33:
				CTrace::Instance()->ShowNoLineFeed("Irregular card length (long)");
				break;
			case 0x34:
				CTrace::Instance()->ShowNoLineFeed("Irregular card length (short)");
				break;
			case 0x36:
				CTrace::Instance()->ShowNoLineFeed("Card position move ");
				break;
			case 0x37:
				CTrace::Instance()->ShowNoLineFeed("Jam error all retrieve ");
				break;
			}
			break;
		case 0x32:
			switch(m_Result.sResponse.cStatusCode[1])
			{
			case 0x30:
				CTrace::Instance()->ShowNoLineFeed("Read error-Parity error");
				break;
			case 0x31:
				CTrace::Instance()->ShowNoLineFeed("Read error-other error");
				break;
			case 0x32:
				CTrace::Instance()->ShowNoLineFeed("Write error");
				break;
			}
			break;
		}
		CTrace::Instance()->Show("");
		CTrace::Instance()->ShowNoLineFeed(" szData ");
		for(i = 0; i< strlen(m_Result.sResponse.szData);i++)
			CTrace::Instance()->ShowNoLineFeed("%c", m_Result.sResponse.szData[i]);

		CTrace::Instance()->Show("");
		CTrace::Instance()->Show("*****************************");
		CTrace::Instance()->SetTrace(0);
	}
	else if(m_Result.szResult[1] != 'P')
	{
		CTrace::Instance()->SetTrace(1);
		CTrace::Instance()->Show("");
		CTrace::Instance()->Show("WaitResponce::Error");
		CTrace::Instance()->Show("%s",m_Result.szResult);
		CTrace::Instance()->Show("");
		CTrace::Instance()->Show("*****************************");
		CTrace::Instance()->Show("");
		CTrace::Instance()->Show("");
		CTrace::Instance()->SetTrace(0);
		return;
	}
		//if(getch() =='s')
		//	exit(-1);
}
#endif