#include "StdAfx.h"
#include "Serial.h"

CSerial::CSerial(void)
: m_hComm(NULL)
, m_bSerial(false)
, m_iBaud(0)
, m_iPort(0)
{
		m_hComm=NULL;

}

CSerial::~CSerial(void)
{
	if (m_hComm!=NULL) CloseHandle(m_hComm);
	m_hComm=NULL;
}

// 打开端口
bool CSerial::OpenPort(int iPort,int Baud)
{
	DCB dcb;
	COMMTIMEOUTS comtimeout;
	TCHAR szPort[100];
	m_bSerial=TRUE;
	if (m_hComm!=NULL)
	{
		CloseHandle(m_hComm);  //关闭通讯口
		m_hComm=NULL;
	}
	//
	wsprintf(szPort,L"COM%d:",iPort);
	m_hComm=CreateFile((LPCWSTR)szPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (m_hComm==INVALID_HANDLE_VALUE)
	{		
		m_hComm=NULL;
		return false;
	}
	m_iPort = iPort;
	m_iBaud = Baud;
	comtimeout.ReadIntervalTimeout =MAXDWORD;
	comtimeout.ReadTotalTimeoutConstant =1000;
	comtimeout.ReadTotalTimeoutMultiplier =0;
	comtimeout.WriteTotalTimeoutConstant =3000;
	comtimeout.WriteTotalTimeoutMultiplier =2000;
	SetCommTimeouts(m_hComm,&comtimeout);   //设定超时时间
	if (m_bSerial)
	{
		memset(&dcb,0,sizeof(DCB));
		dcb.DCBlength =sizeof(DCB);
		GetCommState(m_hComm,&dcb);  //取通讯口原参数
		dcb.BaudRate =m_iBaud;    //波特率
		dcb.ByteSize =8;         //数据位
		dcb.Parity =NOPARITY;    //奇偶校验位
		dcb.StopBits =ONESTOPBIT; //停止位
		dcb.fBinary =TRUE;
		dcb.fParity =TRUE;
		dcb.fOutxCtsFlow =0;
		dcb.fOutxDsrFlow =0;
		dcb.fRtsControl =1;
		dcb.fDtrControl =1;
		SetCommState(m_hComm,&dcb);  //设定通讯口参数
		EscapeCommFunction(m_hComm,SETDTR);
	}

	return true;
}

// 往端口送数
int CSerial::WriteData(unsigned char* pData, int len)
{
	DWORD dwWrited;
	if (m_hComm==NULL || !WriteFile(m_hComm,pData,len,&dwWrited,NULL)) 
	{
		return 0;
	}
	FlushFileBuffers(m_hComm);
	return (int)dwWrited;
}

void CSerial::SetBaud(int iBaud)
{
	m_iBaud=iBaud;
}

int CSerial::ReadData(unsigned char* pData, int len)
{
	DWORD dwWrited=0;
	if (m_hComm==NULL || !ReadFile(m_hComm,pData,len,&dwWrited,NULL)) 
	{
		return 0;
	}
	return (int)dwWrited;
}

bool CSerial::OpenPortNone(int iPort)
{
	DCB dcb;
	COMMTIMEOUTS comtimeout;
	TCHAR szPort[100];
	m_bSerial=TRUE;
	if (m_hComm!=NULL)
	{
		CloseHandle(m_hComm);  //关闭通讯口
		m_hComm=NULL;
	}
	//
	wsprintf(szPort,L"COM%d:",iPort);
	m_hComm=CreateFile((LPCWSTR)szPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (m_hComm==INVALID_HANDLE_VALUE)
	{		
		m_hComm=NULL;
		return false;
	}
	m_iPort = iPort;
	comtimeout.ReadIntervalTimeout =MAXDWORD;
	comtimeout.ReadTotalTimeoutConstant =1000;
	comtimeout.ReadTotalTimeoutMultiplier =0;
	comtimeout.WriteTotalTimeoutConstant =3000;
	comtimeout.WriteTotalTimeoutMultiplier =2000;
	SetCommTimeouts(m_hComm,&comtimeout);   //设定超时时间
	if (m_bSerial)
	{
		memset(&dcb,0,sizeof(DCB));
		dcb.DCBlength =sizeof(DCB);
		GetCommState(m_hComm,&dcb);  //取通讯口原参数
		dcb.BaudRate =m_iBaud;    //波特率
		dcb.ByteSize =8;         //数据位
		dcb.Parity =NOPARITY;    //奇偶校验位
		dcb.StopBits =ONESTOPBIT; //停止位
		dcb.fBinary =TRUE;
		dcb.fParity =TRUE;
		dcb.fOutxCtsFlow =0;
		dcb.fOutxDsrFlow =0;
		dcb.fRtsControl =0;
		dcb.fDtrControl =0;
		SetCommState(m_hComm,&dcb);  //设定通讯口参数
		EscapeCommFunction(m_hComm,SETDTR);
	}

	return true;
}

void CSerial::ClosePort(void)
{
	if (m_hComm!=NULL)
	{
		CloseHandle(m_hComm);  //关闭通讯口
		m_hComm=NULL;
	}
}

int CSerial::GetStatus(int iParam)
{
	unsigned char cmd[100];

	if (!OpenPortNone(m_iPort)) return -1;

	cmd[0]=0x10;
	cmd[1]=0x04;
	cmd[2]=(iParam&0XFF);
	if (WriteData(cmd,3)<1)
	{
//		MessageBox((LPCTSTR)"WriteData");
		return -1;
	}

	if (ReadData(cmd,1)<1)
	{
//		MessageBox((LPCTSTR)"WriteData");
		return -1;
	}
	if (!OpenPort(m_iPort,m_iBaud)) return -1;
	return cmd[0];
}

int CSerial::PrintString(int xpos, int ypos, char* str, int len)
{
	unsigned char cmd[100];

	if (m_hComm==NULL) return -1;

	while (ypos>0)
	{
		cmd[0]=0x1B;
		cmd[1]=0x4A;
		if (ypos >100)
		{
			cmd[2]=100;
			ypos-=100;
		}
		else
		{
			cmd[2]=ypos;
			ypos-=ypos;
		}
		if (WriteData(cmd,3)<1)
		{
	//		MessageBox((LPCTSTR)"WriteData");
			return -1;
		}
	}

	if (xpos>0)
	{
		cmd[0]=0x1B;
		cmd[1]=0x24;
		if (xpos >100)
		{
			cmd[2]=xpos%256;
			cmd[3]=xpos/256;
			xpos-=xpos;
		}
		else
		{
			cmd[2]=xpos%256;
			cmd[3]=xpos/256;
			xpos-=xpos;
		}
		if (WriteData(cmd,4)<1)
		{
	//		MessageBox((LPCTSTR)"WriteData");
			return -1;
		}
	}
	if (WriteData((unsigned char*)str,len)<1)
	{
//		MessageBox((LPCTSTR)"WriteData");
		return -1;
	}

	cmd[0]=0x0D;
	if (WriteData(cmd,1)<1)
	{
//		MessageBox((LPCTSTR)"WriteData");
		return -1;
	}

	return 1;
}

int CSerial::CutPaper(void)
{
	unsigned char cmd[100];

	if (m_hComm==NULL) return -1;

	cmd[0]=0x1D;
	cmd[1]=0x56;
	cmd[2]=0x42;
	cmd[3]=0x00;
	if (WriteData(cmd,4)<1)
	{
//		MessageBox((LPCTSTR)"WriteData");
		return -1;
	}
	return 1;
}

int CSerial::FeedPaper(int iMm)
{
	unsigned char cmd[100];

	if (m_hComm==NULL) return -1;

	iMm*=8;

	while (iMm>0)
	{
		cmd[0]=0x1B;
		cmd[1]=0x4A;
		if (iMm > 100)
		{
			cmd[2]=100;
			iMm-=100;
		}
		else
		{
			cmd[2]=iMm;
			iMm-=iMm;
		}
		if (WriteData(cmd,3)<1)
		{
	//		MessageBox((LPCTSTR)"WriteData");
			return -1;
		}
	}
	return 1;
}
