//
//		TEXIO Serial Access Lib  Ver 1.00
//
//*     Copyright(c) 2012  TEXIO TECHNOLOGY CORPORATION

#include "stdafx.h"
#include "SerialPort.h"

#include <stdlib.h>
#include <winbase.h>
#include <string.h>
#include <stdio.h>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


SerialPort::SerialPort()
{

}

SerialPort::~SerialPort()
{

}

/****************************************
COM_OpenDevice
*****************************************/

HANDLE SerialPort::COM_OpenDevice( int iCommPort)
{
	HANDLE hComm;
	char buffer[10], CommName[7] = "COM";
	DCB	dcb;
	COMMPROP cp;

	if( (iCommPort < 1) || (iCommPort > 9) ){
		return (HANDLE)-1;
	}

	_itoa( iCommPort, buffer, 10 );
	strcat( CommName, buffer );

	if( (hComm = CreateFile(CommName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL)) == INVALID_HANDLE_VALUE ) {
		return (HANDLE)-1;
	}

	GetCommProperties( hComm, &cp );
	GetCommState( hComm, &dcb );

	dcb.BaudRate = 9600;
	dcb.ByteSize = 7;
	dcb.Parity = 2;
	dcb.StopBits = 0;
	dcb.EofChar = 0x0a;
	dcb.fBinary = 0;
	dcb.fParity = 1;
	dcb.fOutxCtsFlow = 0 ;
	dcb.fOutxDsrFlow = 0;
	dcb.fRtsControl = 0 ;

	SetCommState( hComm, &dcb );
	COM_Clear(hComm,0x0f);	// All Buffer Clear
	return hComm;
}

/****************************************
COM_CloseDevice
*****************************************/
int SerialPort::COM_CloseDevice( HANDLE hComm )
{
	if( !CloseHandle( hComm ) ){
		return -1;
	}
	return 0;
}

/****************************************
COM_SetTimeout
*****************************************/
int SerialPort::COM_SetTimeout( HANDLE hComm, int iTime)
{
	COMMTIMEOUTS timeout;

	if( !GetCommTimeouts( hComm, &timeout ) ){
		return -1;
	}

	timeout.ReadIntervalTimeout = iTime;
	timeout.ReadTotalTimeoutMultiplier = 5;	
	timeout.ReadTotalTimeoutConstant = iTime;
	if( !SetCommTimeouts( hComm, &timeout ) ){
		return -1;
	}
	return 0;
}

/****************************************
COM_SetParameter
*****************************************/
int SerialPort::COM_SetParameter( HANDLE hComm, DWORD dBaud, BYTE bByteSize, BYTE bParity, BYTE bStopBits)
{
	DCB	dcb;
	COMMPROP cp;

	GetCommProperties( hComm, &cp );
	GetCommState( hComm, &dcb );

	dcb.BaudRate = dBaud;
	dcb.ByteSize = bByteSize;
	dcb.Parity = bParity;
	dcb.StopBits = bStopBits;

	SetCommState( hComm, &dcb );

	return 0;
}

/****************************************
COM_Clear
*****************************************/
int SerialPort::COM_Clear( HANDLE hComm, int flag )
{

	if( !PurgeComm( hComm, flag) ){
		return -1;
	}
	return 0;

}
/****************************************
COM_Receive
*****************************************/
int SerialPort::COM_Receive( HANDLE hComm, char *String )
{
	DWORD result;
	char temp[2048] = "";
	unsigned int count;

	if( !ReadFile( hComm, temp, 2048, &result, NULL) ){	
		return -1;	
	}
	if( result == 0 ){
		return -1;	
	}
	for (count = 0 ;count < 2048 ; count ++){

		*String = temp[count] ;
		String ++ ;
		if (count >= result) break;
	}
	count = (int)result;
	return count;
}

/****************************************
COM_Send
*****************************************/
int SerialPort::COM_Send( HANDLE hComm, char *String, DWORD Len )
{
	DWORD result;

	COM_Clear(hComm,0x0f);
	if( !WriteFile( hComm, String, Len, &result, NULL) ){
		return -1;
	}

	return 0;
}

