BlueFlashTool.cpp 10.9 KB
// BlueFlashTool.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "BlueFlashTool.h"
#include "BlueFlashToolDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBlueFlashToolApp

BEGIN_MESSAGE_MAP(CBlueFlashToolApp, CWinApp)
	//{{AFX_MSG_MAP(CBlueFlashToolApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlueFlashToolApp construction

CBlueFlashToolApp::CBlueFlashToolApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CBlueFlashToolApp object
//---------------全局变量定义区
	CBlueFlashToolApp theApp;
	CBlueFlashToolApp *g_pmainapp;
	app_main_data_struct g_AppMainData;
	//CByteArray g_pBufferBinFile[20];
	CIniFile g_globalini;
//---------------全局变量定义区


CString GetCurrPath();




/////////////////////////////////////////////////////////////////////////////
// CBlueFlashToolApp initialization

BOOL CBlueFlashToolApp::InitInstance()
{
	CString strglobalpath;
	if(MAX_TOPWELL_DEVICE_NUM > 25)
	{
		::AfxMessageBox("算了吧",MB_ICONSTOP);
		return FALSE;
	}
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	//Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
	g_pmainapp = this;

	strglobalpath = GetCurrPath() + "Global";
	g_globalini.SetIniFileName(strglobalpath);
	LoadConfigs();

	//--------------------------------------------------------数据初始化
	


	CBlueFlashToolDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


// 加载程序默认配置
bool CBlueFlashToolApp::LoadConfigs(void)
{
	CString strkeyname;
	int loop1,loop2;
#ifdef __TOPWELL_EDIT_SN_ENABLE__
	g_AppMainData.if_same_sn = g_globalini.ReadBool("SN_Config","SN_same");
	g_AppMainData.SN_head = g_globalini.ReadString("SN_Config","SN_head");
	g_AppMainData.SN_start = g_globalini.ReadInt("SN_Config","SN_start");
	g_AppMainData.SN_end = g_globalini.ReadInt("SN_Config","SN_end");
	g_AppMainData.SN_cur = g_globalini.ReadInt("SN_Config","SN_cur");
	g_AppMainData.if_same_btname = g_globalini.ReadBool("BT_name_Config","Btname_same");
	g_AppMainData.btname_head = g_globalini.ReadString("BT_name_Config","Btname_head");
	g_AppMainData.btname_start = g_globalini.ReadInt("BT_name_Config","Btname_start");
	g_AppMainData.btname_end = g_globalini.ReadInt("BT_name_Config","Btname_end");
	g_AppMainData.btname_cur = g_globalini.ReadInt("BT_name_Config","Btname_cur");
#endif
	g_AppMainData.com_config.aBandrate = g_globalini.ReadInt("UART_Config","波特率");
	g_AppMainData.com_config.bcheck = g_globalini.ReadInt("UART_Config","校验位");
	g_AppMainData.com_config.cdatabit = g_globalini.ReadInt("UART_Config","数据位");
	g_AppMainData.com_config.dstopbit = g_globalini.ReadInt("UART_Config","停止??");
	g_AppMainData.com_config.estreambit = g_globalini.ReadInt("UART_Config","流控制");
	g_AppMainData.LastEarBin = g_globalini.ReadInt("Config","LastEarFile");
	g_AppMainData.LastUsbBin = g_globalini.ReadInt("Config","LastUsbFile");
	g_AppMainData.firstID_binfile = g_globalini.ReadInt("Config","FirstID_binFile");
	g_AppMainData.lastID_binfile = g_globalini.ReadInt("Config","LastID_binFile");
	if (g_AppMainData.firstID_binfile <0 || g_AppMainData.firstID_binfile > 65535)
		g_AppMainData.firstID_binfile = 0;
	if (g_AppMainData.lastID_binfile <0 || g_AppMainData.lastID_binfile > 65535)
		g_AppMainData.lastID_binfile = 0;


	
	if (g_AppMainData.LastEarBin <1) g_AppMainData.LastEarBin = 1;
	if (g_AppMainData.LastUsbBin <1) g_AppMainData.LastUsbBin = 1;
	for (loop1 = 0;loop1<BLUE_TYPE_MAX;loop1++)
	{
		for (loop2 = 0;loop2<MAX_TOPWELL_DEVICE_NUM;loop2++)
		{
			if (loop1 == BLUE_TYPE_EAR)
				strkeyname.Format("ear_port%d",loop2);
			else
				strkeyname.Format("usb_port%d",loop2);
			g_AppMainData.ary_myComPort[loop1][loop2] = g_globalini.ReadInt("UART_Config",strkeyname);
			if (g_AppMainData.ary_myComPort[loop1][loop2] > 255)
			{
				g_AppMainData.ary_myComPort[loop1][loop2] = 0;
			}
			
			#ifdef __TOPWELL_EDIT_SN_ENABLE__
			if (loop1 == BLUE_TYPE_EAR)
				strkeyname.Format("earBin%d_path",loop2);
			else
				strkeyname.Format("usbBin%d_path",loop2);
			g_AppMainData.FilePathEar= g_globalini.ReadString("BinFilePath",strkeyname);
			#endif
		}
	}
	return true;
}

bool CBlueFlashToolApp::SaveConfigs(void)
{
	CString strkeyname;
	int loop1,loop2;

#ifdef __TOPWELL_EDIT_SN_ENABLE__
	g_globalini.WriteBool("SN_Config","SN_same",g_AppMainData.if_same_sn);
	g_globalini.WriteString("SN_Config","SN_head",g_AppMainData.SN_head);
	g_globalini.WriteInt("SN_Config","SN_start",g_AppMainData.SN_start);
	g_globalini.WriteInt("SN_Config","SN_end",g_AppMainData.SN_end);
	g_globalini.WriteInt("SN_Config","SN_cur",g_AppMainData.SN_cur);
	g_globalini.WriteBool("BT_name_Config","Btname_same",g_AppMainData.if_same_btname);
	g_globalini.WriteString("BT_name_Config","Btname_head",g_AppMainData.btname_head);
	g_globalini.WriteInt("BT_name_Config","Btname_start",g_AppMainData.btname_start);
	g_globalini.WriteInt("BT_name_Config","Btname_end",g_AppMainData.btname_end);
	g_globalini.WriteInt("BT_name_Config","Btname_cur",g_AppMainData.btname_cur);
#endif
	g_globalini.WriteInt("UART_Config","波特率",g_AppMainData.com_config.aBandrate);
	g_globalini.WriteInt("UART_Config","校验位",g_AppMainData.com_config.bcheck);
	g_globalini.WriteInt("UART_Config","数据位",g_AppMainData.com_config.cdatabit);
	g_globalini.WriteInt("UART_Config","停止位",g_AppMainData.com_config.dstopbit);
	g_globalini.WriteInt("UART_Config","流控制",g_AppMainData.com_config.estreambit);
	g_globalini.WriteInt("Config","LastEarFile",g_AppMainData.LastEarBin);
	g_globalini.WriteInt("Config","LastUsbFile",g_AppMainData.LastUsbBin);
	g_globalini.WriteInt("Config","FirstID_binFile",g_AppMainData.firstID_binfile);
	g_globalini.WriteInt("Config","LastID_binFile",g_AppMainData.lastID_binfile);


	for (loop1 = 0;loop1<BLUE_TYPE_MAX;loop1++)
	{
		for (loop2 = 0;loop2<MAX_TOPWELL_DEVICE_NUM;loop2++)
		{
			if (loop1 == BLUE_TYPE_EAR)
				strkeyname.Format("ear_port%d",loop2);
			else
				strkeyname.Format("usb_port%d",loop2);
			g_globalini.WriteInt("UART_Config",strkeyname,g_AppMainData.ary_myComPort[loop1][loop2]);
		}
	}
#ifdef __TOPWELL_EDIT_SN_ENABLE__
	g_globalini.WriteString("BinFilePath","EarBin",g_AppMainData.FilePathEar);
	g_globalini.WriteString("BinFilePath","UsbBin",g_AppMainData.FilePathUsb);
#endif
	return true;
}



//获取应用程序的根目录
//return value:应用程序的根目录
CString GetCurrPath()
{
	CString strPath ="";
	char cCurDirectory[_MAX_PATH];
	memset(cCurDirectory,'\0',_MAX_PATH);
	::GetModuleFileName(NULL,  cCurDirectory,   _MAX_PATH); 
	//GetCurrentDirectory(256, cCurDirectory); //不能使用这个函数,如果中间你调用文件对话框,对改变你的当前路径
	strPath.Format(cCurDirectory);
	CString strAppName =AfxGetApp()->m_pszAppName; 
	strAppName+=".exe";
	//把最后的exe文件名去掉 
	int length1,length2; 
	length1=strPath.GetLength(); 
	length2=strAppName.GetLength(); 
	strPath.Delete(length1-length2,length2); 
	//strPath.Replace("\\","\\\\");
	return strPath;
}
//判断某文件是否存在
//return value: ture:存在,false:不存在!
//Parameters:
//strAbsolutefilePath:文件的绝对路径
bool IsExistFile(CString strAbsolutefilePath)
{
	CFileFind findFile;
	//BOOL iFlag = false;
	BOOL iFlag = findFile.FindFile(strAbsolutefilePath);
	return (bool)iFlag;
}


BOOL QuerySingleSerialPortEx(int nPort)
{
	CString strComm;
	strComm.Format("COM%d",nPort);
	if (nPort >= 10)
	{//微软的技术手册规定打开端口号大于9的串口时应该使用"\\.\COM10"格式的文件名,更多的信息请参考msdn。
		strComm.Format("\\\\.\\COM%d",nPort);	
	}
	BOOL bValid = TRUE;
	HANDLE hComm;

	hComm = CreateFile(strComm,GENERIC_READ | GENERIC_WRITE,0,NULL,\
	OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL| FILE_FLAG_OVERLAPPED, NULL); 
	int nError = GetLastError();

	if(INVALID_HANDLE_VALUE == hComm)     
	{//***********如果没有该设备,或者被其他应用程序占用********
		if(2 == nError)
			bValid = -1;
		else if(5 == nError)
			bValid = FALSE;
		else
			bValid = FALSE;
	}
	CloseHandle(hComm); //重要 关闭文件句柄 = 关闭串口
	return bValid;
}

//利用API查询指定范围内的串口状态信息,并分别返回指定范围内系统存在的串口,
//当前未被占用的可用串口,已被占用的串口和系统不存在的串口
void QuerySerialPortStatusEx(CWordArray& cAryExistPort,
	CWordArray& cAryNoneOpenedPort,
	CWordArray& cAryAlreadyOpenedPort,
	CWordArray& cAryNotExistPort,int nStartPort,int nLastPort)
{
	CString strTmp,strComm,strFree;
	//对输入的参数进行检查
	if(nStartPort < 1) nStartPort = 1;
	if(nStartPort > 65535) nStartPort = 65535;
	if(nLastPort < 1) nLastPort = 1;
	if(nLastPort > 65535) nLastPort = 65535;
	int n1 = nStartPort,n2=nLastPort;
	nStartPort = min(n1,n2);
	nLastPort  = max(n1,n2);

	cAryExistPort.RemoveAll();
	cAryNoneOpenedPort.RemoveAll();
	cAryAlreadyOpenedPort.RemoveAll();
	cAryNotExistPort.RemoveAll();

 	HANDLE hComm;
	int nAccum = 0;
	for (int i = nStartPort;i<=nLastPort;i++)
	{
		strComm.Format("COM%d",i);
		if (i>=10)
		{//微软的技术手册规定打开端口号大于9的串口时应该使用"\\.\COM10"格式的文件名,更多的信息请参考msdn。
			strComm.Format("\\\\.\\COM%d",i); //注:"\\.\COM10"格式对于10以下的串口也是有效的	
		}
		// 这里的CreateFile函数起了很大的作用,可以用来创建系统设备文件,如果该设备不存在或者被占用,则会返回一个错误,即下面的 INVALID_HANDLE_VALUE ,据此可以判断可使用性。详细参见MSDN中的介绍。
		hComm = CreateFile(strComm,GENERIC_READ | GENERIC_WRITE,0,NULL,\
			OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL| FILE_FLAG_OVERLAPPED, NULL); 
		int nLastError = GetLastError();
		strComm.TrimLeft("\\\\.\\");

		if(hComm == INVALID_HANDLE_VALUE)     
		{//***********如果没有该设备,或者被其他应用程序在用********
			if (2 == nLastError)
			{//串口不存在
				cAryNotExistPort.Add(i);
			}
			else if(5 == nLastError)
			{//串口已被占用
				cAryExistPort.Add(i);
				cAryAlreadyOpenedPort.Add(i);
			}
			else
			{//其他错误
				cAryExistPort.Add(i);
			}
		}
		else
		{//串口存在且未被占用
			cAryExistPort.Add(i);
			cAryNoneOpenedPort.Add(i);
			nAccum++;
		}
		CloseHandle(hComm); //关闭文件句柄 - 必要
	}

}