#include "stdafx.h"
#include "Verbpo.h"
#include "Uart.h"


Uart::Uart()
{
	m_hCom = INVALID_HANDLE_VALUE;
}


Uart::~Uart()
{
	if ((m_hCom != INVALID_HANDLE_VALUE))
	{
		CloseHandle(m_hCom);
	}
}



int Uart::Open_Uart()
{
	
	if (theApp.m_i32UartChannel >= 0)
	{
		CString a_str;
		a_str.Format(_T("%d"), theApp.m_i32UartChannel);
		CString str1 = _T("COM") + a_str;

		m_hCom = CreateFile(str1, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

		if (INVALID_HANDLE_VALUE == m_hCom)
		{
//			MessageBox(0, _T("串口打开失败，请重新设置串口"), _T("错误"), 0);
			return 1;
		}
		else
		{
			DCB dcb;
			COMMTIMEOUTS commtimeouts;

			GetCommTimeouts(m_hCom, &commtimeouts);
			//两字符之间最大的延时，当读取串口数据时，
			//一旦两个字符传输的时间差超过该时间，读取函数将返回现有的数据。设置为0表示该参数不起作用。
			commtimeouts.ReadIntervalTimeout = MAXDWORD; //10
			//读取每字符间的超时
			commtimeouts.ReadTotalTimeoutMultiplier = MAXDWORD; //0
			//一次读取串口数据的固定超时
			commtimeouts.ReadTotalTimeoutConstant = 10;//0; 
			//写入每字符间的超时。
			commtimeouts.WriteTotalTimeoutMultiplier = MAXDWORD;//1; 
			//一次写入串口数据的固定超时。
			commtimeouts.WriteTotalTimeoutConstant = 500;//1000 

			SetCommTimeouts(m_hCom, &commtimeouts);

			GetCommState(m_hCom, &dcb);

			dcb.ByteSize = 8;
			dcb.Parity = NOPARITY;
			dcb.StopBits = ONESTOPBIT;
			dcb.BaudRate = 115200;   //4232通讯波特率
			SetCommState(m_hCom, &dcb);

			unsigned char mode;   //设置4232的灵敏度，脉宽类型
			//缺省为高

			dcb.fRtsControl = RTS_CONTROL_ENABLE;
			SetCommState(m_hCom, &dcb);
			Sleep(500);//100

			dcb.fDtrControl = DTR_CONTROL_DISABLE;//复位信号高电平(控制口线低电平)
			//DTR_CONTROL_ENABLE;
			//dcb.fRtsControl=RTS_CONTROL_DISABLE;
			SetCommState(m_hCom, &dcb);

			Sleep(50);//100

			dcb.fDtrControl = DTR_CONTROL_ENABLE; //复位信号低电平(控制口线高电平--供电)
			SetCommState(m_hCom, &dcb);

			Sleep(500);//100

			dcb.fRtsControl = RTS_CONTROL_DISABLE;      //模式信号高电平(控制口线低电平)
			SetCommState(m_hCom, &dcb);

			Sleep(50);//100

			mode = 0x40;//高灵敏度 否则0
			mode |= 0x10;//设置脉宽类型 1.627uS 否则0 

			//设置波特率
			//9600 -0x46;
			//19200-0x03+mode;
			//38400-0x02+mode;
			//57600-0x01+mode;
			//115200-0x00+mode;
			//14400-0x04+mode;
			//12800-0x05+mode;
			//7200 -0x07+mode;
			//4800 -0x08+mode;
			//3600 -0x09+mode;
			//2400 -0x0A+mode;
			//1800 -0x0B+mode;
			//1200 -0x0C+mode;
			//9600 -0x06+mode;     //默认9600

			BYTE IrControl = 0x00 + mode;

			Sleep(50);//100

			dcb.fRtsControl = RTS_CONTROL_ENABLE;    //模式端口低电平（口线高电平）
			dcb.BaudRate = 2400;
			SetCommState(m_hCom, &dcb);

			//设置结束。
			PurgeComm(m_hCom, PURGE_RXCLEAR | PURGE_TXCLEAR);
			
		}
	}
	return 0;
}



int Uart::Close_Uart()
{
	return 0;
}

int Uart::Rev(unsigned char *buff, int length)
{
	DWORD ReadBytes = 0;
	if ((m_hCom != INVALID_HANDLE_VALUE))
	{
		if (ReadFile(m_hCom, buff, length, &ReadBytes, NULL))
		{
			return ReadBytes;
		}
	}
	return 0;
}

int Uart::Send(unsigned char *buff, int length)
{
	DWORD SendBytes = 0;
	if ((m_hCom != INVALID_HANDLE_VALUE))
	{
		if (!WriteFile(m_hCom, buff, length, &SendBytes, NULL) || SendBytes != length)
		{
			MessageBox(NULL, _T("Send error"), NULL, 0);
		}
		return SendBytes;

	}

	return 0;
}