⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writeimeidlg.cpp

📁 MTK平台烧写序列号的程序
💻 CPP
字号:
// WriteIMEIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WriteIMEI.h"
#include "WriteIMEIDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWriteIMEIDlg dialog

CWriteIMEIDlg::CWriteIMEIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWriteIMEIDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWriteIMEIDlg)
	m_IMEI = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWriteIMEIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWriteIMEIDlg)
	DDX_Control(pDX, IDC_RESULT, m_Result);
	DDX_Control(pDX, IDC_PORT, m_Port);
	DDX_Control(pDX, IDC_FINALRESULT, m_Finalresult);
	DDX_Text(pDX, IDC_IMEI, m_IMEI);
	DDV_MaxChars(pDX, m_IMEI, 15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWriteIMEIDlg, CDialog)
	//{{AFX_MSG_MAP(CWriteIMEIDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWriteIMEIDlg message handlers

BOOL CWriteIMEIDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
		//set the final result box font
	CFont* font;
	font = new CFont;
	font->CreateFont(86,0,0,0,700,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH|FF_DONTCARE,
		"arial"); 
	// Cause the label to use the new font
	m_Finalresult.SetFont(font);

	//set the IMEI input box font
	CFont* ffont;
	ffont = new CFont;
	ffont->CreateFont(36,0,0,0,700,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH|FF_DONTCARE,
		"arial"); 
	// Cause the label to use the new font
	GetDlgItem(IDC_IMEI)->SetFont(ffont);



		for( int i=1; i<256; i++ )
	{

		CString sPort;
		sPort.Format(_T("\\\\.\\COM%d"),i);
		BOOL bSuccess=FALSE;
		HANDLE hPort=::CreateFile(sPort, GENERIC_READ|GENERIC_WRITE, 0, 0,
			OPEN_EXISTING, 0, 0);
		if( hPort == INVALID_HANDLE_VALUE)
		{
			DWORD dwError=GetLastError();
			if( dwError == ERROR_ACCESS_DENIED)
				bSuccess=TRUE;
		}
		else
		{
			bSuccess=TRUE;
			CloseHandle(hPort);
		}
		if(  bSuccess )
		{
			CString str;
			str.Format("COM%d",i);
			m_Port.AddString(str);
		}
	}
	m_Port.SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWriteIMEIDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWriteIMEIDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWriteIMEIDlg::PrintError(CString cs)
{
	CTime t = CTime::GetCurrentTime();
	CString sTemp,szTemp;
	szTemp.Format("%02d:%02d:%02d ",t.GetHour(),t.GetMinute(),t.GetSecond());
	m_Result.GetWindowText(sTemp);
	m_Result.SetWindowText(szTemp+cs+"\r\n"+sTemp);
	m_Finalresult.SetWindowText("Fail");
}

void CWriteIMEIDlg::OnWrite() 
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	
	//for debug use
	int debug=0;

	CString sPort,sTemp;
	m_Port.GetWindowText(sPort);
	sPort=sPort.Right(sPort.GetLength()-3);
	int port=atoi(sPort);
	sPort.Format(_T("\\\\.\\COM%d"),port);

	//clear final result window
	m_Finalresult.SetWindowText("");

	//open com port
	HANDLE hPort;
	hPort=CreateFile(sPort, GENERIC_READ|GENERIC_WRITE,0, NULL, 
		OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
	if(hPort == INVALID_HANDLE_VALUE)
	{
		CString csTemp;
		m_Port.GetWindowText(csTemp);
		PrintError("Can not open "+csTemp);
		hPort=NULL;
		return;
	}

	//config the com port
	DCB dcb;
   	dcb.DCBlength = sizeof(DCB);
	GetCommState( hPort, &dcb ) ;
	SetupComm( hPort, 4096, 4096 ) ;
	PurgeComm(hPort,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);

	dcb.Parity = NOPARITY;
	dcb.ByteSize = 8;
	dcb.StopBits = ONESTOPBIT;
	dcb.BaudRate = 115200;     //57600(MT6205B), 115200 (MT6218B)			
	dcb.fBinary = TRUE;
	dcb.fParity = FALSE;
	dcb.fOutxCtsFlow = FALSE;
	dcb.fOutxDsrFlow = FALSE;
	dcb.fDtrControl = DTR_CONTROL_DISABLE;
	dcb.fDsrSensitivity = FALSE;
	dcb.fTXContinueOnXoff = FALSE;
	dcb.fOutX = FALSE;
	dcb.fInX = FALSE;
	dcb.fErrorChar = FALSE;
	dcb.fNull = FALSE;
	dcb.fRtsControl = RTS_CONTROL_ENABLE;
	dcb.fAbortOnError = FALSE;
	dcb.XonChar = 0;
	dcb.XoffChar = 0;
	dcb.ErrorChar = 0;
	dcb.EofChar = 0;
	dcb.EvtChar = 0;
	SetCommState(hPort, &dcb);

	//set time out struct
	COMMTIMEOUTS timeouts;
	timeouts.ReadIntervalTimeout = 0xFFFFFFFF;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutMultiplier = 0;
	timeouts.WriteTotalTimeoutConstant = 2000;
	SetCommTimeouts(hPort, &timeouts);

	
//	SetCommMask(hPort, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型


	UpdateData(TRUE);
	if(m_IMEI.GetLength() != 15)
	{
		PrintError("Please check IMEI.");
		CloseHandle(hPort);
		return;
	}
	//AfxMessageBox(m_IMEI);

	//read and write the com port
	BOOL bReadStatus,bWriteStat;
	DWORD dwBytesWritten,dwBytesRead;
	char *buffer;
	CString command;
	char buf[1024];
	int i;
/*
	//send test command
	command.Empty();
	command.Format("AT\n");
	buffer=command.GetBuffer(command.GetLength());
	bWriteStat=WriteFile( hPort, buffer, command.GetLength(), &dwBytesWritten, NULL);
	Sleep(60);
	memset(buf,0,sizeof(buf));
	buffer=buf;
	bReadStatus = ReadFile( hPort, buffer, 8, &dwBytesRead, NULL);
*/
	//write non-sleep mode
	int whetherout=0;
	for(i=0;i<10;i++)
	{
		memset(buf,0,sizeof(buf));
		command.Empty();
		command.Format("AT+ESLP=0\r\n");
		buffer=command.GetBuffer(command.GetLength());
		bWriteStat = WriteFile( hPort, buffer, command.GetLength(), &dwBytesWritten,NULL );
		if( dwBytesWritten != command.GetLength() )
		{
			PrintError("Failed writing to port. Try again.");
			if(debug)	PrintError("AT+ESLP=0");
			CloseHandle(hPort);
			return;
		}
		Sleep(60);
		buffer=buf;
		bReadStatus = ReadFile( hPort, buffer, 8, &dwBytesRead, NULL);
		if(dwBytesRead != 0)
		{
			whetherout=1;
			break;
		}

	}
		if(whetherout == 0)
		{
			PrintError("Failed reading port. Try again.");
			if(debug)	PrintError("AT+ESLP=0");
			CloseHandle(hPort);
			return;
		}

	PurgeComm(hPort,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
	memset(buf,0,sizeof(buf));
	//read sn
	Sleep(60);
	command.Empty();
	command.Format("AT+EGMR=0,5\r\n");
	buffer=command.GetBuffer(command.GetLength());
	bWriteStat = WriteFile( hPort, buffer, command.GetLength(), &dwBytesWritten, NULL );
	if( dwBytesWritten != command.GetLength() )
	{
		PrintError("Failed writing to port. Try again.");
		if(debug)	PrintError("AT+EGMR=0,5");
		CloseHandle(hPort);
		return;
	}
	Sleep(100);
	buffer=buf;
	bReadStatus = ReadFile( hPort, buffer, 90, &dwBytesRead, NULL);
	//check whether sn is illegal
	int start=0;
/*	for(i=0;i<1024;i++)
	{
		if( *(buffer+i)=='\"' && *(buffer+i+1)=='S')
		{
			start=i;
			break;
		}
	}
	if( *(buffer+start+6) == 'A' )  //tobe modified
	{
		if( *(buffer+start+61) != '1' ||
			*(buffer+start+62) != '0' )
		{
			if(	*(buffer+start+60) == '1' &&
				*(buffer+start+61) == '0')
				{}
				else
				{
					//test again
					Sleep(100);
					buffer=buf;
					bReadStatus = ReadFile( hPort, buffer, 90, &dwBytesRead, NULL);
					//check whether sn is illegal
					start=-1;
					for(i=0;i<1024;i++)
					{
						if( *(buffer+i)=='\"' && *(buffer+i+1)=='S' )
						{
							start=i;
							break;
						}
					}
					if( start == -1 ||
						*(buffer+start+61) != '1' ||
						*(buffer+start+62) != '0' ) 
					{

						if(	*(buffer+start+60) == '1' &&
						*(buffer+start+61) == '0')
						{}
						else
						{

							PrintError("Error checking Serial Number. Please try again.");
							CloseHandle(hPort);
							return;
						}

					}
				}
		}
	}
	else
	{
		PrintError("Error checking Serial Number. Please try again.");
		CloseHandle(hPort);
		return;
	}
*/
	//write imei
	memset(buf,0,sizeof(buf));
	Sleep(40);
	command.Empty();
	command.Format("AT+EGMR=1,7,");
	command=command+"\""+m_IMEI+"\"\r\n";
	buffer=command.GetBuffer(command.GetLength());
	bWriteStat = WriteFile( hPort, buffer, command.GetLength(), &dwBytesWritten, NULL );
	if( dwBytesWritten != command.GetLength() )
	{
		PrintError("Failed writing IMEI. Try agian.");
		CloseHandle(hPort);
		return;
	}
	Sleep(40);
	buffer=buf;
	bReadStatus = ReadFile( hPort, buffer, 50, &dwBytesRead, NULL );


	Sleep(40);

	//check imei for rounds
	int rounds=0;
	int imei_ok=0;
	for(rounds=0; rounds<3; rounds++)
	{
		memset(buf,0,sizeof(buf));	//now check imei
		command.Empty();
		command.Format("AT+EGMR=0,7\r\n");
		buffer=command.GetBuffer(command.GetLength());
		bWriteStat = WriteFile( hPort, buffer, command.GetLength(), &dwBytesWritten, NULL );
		Sleep(100);
		buffer=buf;
		bReadStatus = ReadFile( hPort, buffer, 60, &dwBytesRead, NULL );
		//check whether IMEI is right
		for(i=0;i<1024;i++)
		{
			if( *(buffer+i) == 'E' &&
				*(buffer+i+1) == 'G' &&
				*(buffer+i+2) == 'M' &&
				*(buffer+i+3) == 'R' &&
				*(buffer+i+4) == ':' &&
				*(buffer+i+5) == ' ' &&
				*(buffer+i+6) == '\"')
				start=i;
		}
		int check=1;
		for(i=0; i<15; i++)
		{
			char a,b;
			a=m_IMEI.GetAt(i);
			b=*(buffer+start+i+7);
			if(a != b)
			{
				check=0;
				break;
			}
		}
		if(check == 0)
		{
			//test again
			Sleep(100);
			buffer=buf;
			bReadStatus = ReadFile( hPort, buffer, 60, &dwBytesRead, NULL );
			//check whether IMEI is right
			for(i=0;i<1024;i++)
			{
				if( *(buffer+i) == 'E' &&
					*(buffer+i+1) == 'G' &&
					*(buffer+i+2) == 'M' &&
					*(buffer+i+3) == 'R' &&
					*(buffer+i+4) == ':' &&
					*(buffer+i+5) == ' ' &&
					*(buffer+i+6) == '\"')
					start=i;
			}
			check=1;
			for(i=0; i<15; i++)
			{
				char a,b;
				a=m_IMEI.GetAt(i);
				b=*(buffer+start+i+7);
				if(a != b)
				{
					check=0;
					break;
				}
			}
			if(check == 1)
				imei_ok=1;
		}

		if(imei_ok == 1)
			break;
		rounds++;
	}



	//show success information
	m_Finalresult.SetWindowText("Succeed");
	CTime t = CTime::GetCurrentTime();
	CString szTemp;
	szTemp.Format("%02d:%02d:%02d ",t.GetHour(),t.GetMinute(),t.GetSecond());
	m_Result.GetWindowText(sTemp);
	m_Result.SetWindowText(szTemp+"Succeed writing IMEI:"+m_IMEI+".\r\n"+sTemp);

	//close the port
	CloseHandle(hPort);


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -