📄 writeimeidlg.cpp
字号:
// WriteIMEIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WriteIMEI.h"
#include "WriteIMEIDlg.h"
#include "ColReadOnlyEdit.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_AUTO, m_Auto);
DDX_Control(pDX, IDC_STATIC_TYPE, m_Static_Type);
DDX_Control(pDX, IDC_WRITE, m_Write);
DDX_Control(pDX, IDC_TYPE, m_Type);
DDX_Control(pDX, IDC_BAUD, m_Baud);
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);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWriteIMEIDlg, CDialog)
//{{AFX_MSG_MAP(CWriteIMEIDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_CBN_SELCHANGE(IDC_TYPE, OnSelchangeType)
ON_CBN_SELCHANGE(IDC_AUTO, OnSelchangeAuto)
ON_EN_CHANGE(IDC_IMEI, OnChangeImei)
//}}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 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);
//init baud
m_Baud.AddString("115200");
m_Baud.AddString("57600");
m_Baud.SetCurSel(0);
//init port
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);
//init type
m_Type.AddString("SN");
m_Type.AddString("IMEI");
m_Type.SetCurSel(0);
//init auto
m_Auto.AddString("OFF");
m_Auto.AddString("ON");
m_Auto.SetCurSel(0);
//init Barcode length limit
CEdit *pEdit1;
pEdit1=(CEdit*)GetDlgItem(IDC_IMEI);
pEdit1->SetLimitText(30);
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)
{
extern COLORREF gColor;
extern CString gFinalResult;
gColor = RGB(255,0,0);
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);
gFinalResult.Format(" Fail");
RedrawWindow();
//enable write button
if(!autow)
m_Write.EnableWindow(TRUE);
}
void CWriteIMEIDlg::OnWrite()
{
// TODO: Add your control notification handler code here
extern CString gFinalResult;
extern COLORREF gColor;
int debug=0;//for debug use
CString sPort,sBaud,sTemp;
int port,baud,type;
//get port
m_Port.GetWindowText(sPort);
sPort=sPort.Right(sPort.GetLength()-3);
port=atoi(sPort);
sPort.Format(_T("\\\\.\\COM%d"),port);
//get baud
m_Baud.GetWindowText(sBaud);
baud=atoi(sBaud);
//get type
type = m_Type.GetCurSel();
//get auto
autow = m_Auto.GetCurSel();
//clear final result window
gFinalResult.Format("");
RedrawWindow();
//disable write button
m_Write.EnableWindow(FALSE);
//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't 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 = baud; //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 );//设置事件驱动的类型
//check imei
UpdateData(TRUE);
int i;
int IMEI_length=m_IMEI.GetLength();
if(type == 1)
{
//check length
if(IMEI_length!= 15)
{
PrintError("Please check IMEI. 15 digits required!");
CloseHandle(hPort);
return;
}
//check whether digits
for(i=0;i<IMEI_length;i++)
if(m_IMEI.GetAt(i) <'0' || m_IMEI.GetAt(i) >'9')
{
PrintError("Please check IMEI. Only digits allowed!");
CloseHandle(hPort);
return;
}
}
//check SN
if(type == 0 && m_IMEI.GetLength() == 0 )
{
PrintError("Please check SN.");
CloseHandle(hPort);
return;
}
//AfxMessageBox(m_IMEI);
//read and write the com port
BOOL bReadStatus,bWriteStat;
DWORD dwBytesWritten,dwBytesRead;
char *buffer;
char *p=NULL;
CString command;
char buf[1024];
int timeout=1;
//write "AT"
for(i=0;i<50;i++)
{
//send command
command.Empty();
command.Format("AT\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");
CloseHandle(hPort);
return;
}
//receive response
Sleep(200);
memset(buf,0,sizeof(buf));
buffer=buf;
bReadStatus = ReadFile( hPort, buffer, 10, &dwBytesRead, NULL);
if(debug)
PrintError(CString(buffer)+CString("."));
if(dwBytesRead != 0)
{
p=strstr(buffer,"OK");
if(p)
{
timeout=0;
break;
}
}
Sleep(100);
}
//check if failed
if(timeout == 1)
{
PrintError("Failed reading port. Try again.");
if(debug) PrintError("AT");
CloseHandle(hPort);
return;
}
//make sure rx data cleaned
Sleep(100);
bReadStatus = ReadFile( hPort, buffer, 10, &dwBytesRead, NULL);
//write "AT+ESLP=0"
Sleep(50);
timeout = 1;
for(i=0;i<8;i++)
{
//send command
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;
}
//receive response
Sleep(100);
memset(buf,0,sizeof(buf));
buffer=buf;
bReadStatus = ReadFile( hPort, buffer, 30, &dwBytesRead, NULL);
if(debug)
PrintError(CString(buffer));
if(dwBytesRead != 0)
{
p=strstr(buffer,"OK");
if(p)
{
timeout=0;
break;
}
}
}
//check if failed
if(timeout == 1)
{
PrintError("Failed reading port. Try again.");
if(debug) PrintError("AT+ESLP=0");
CloseHandle(hPort);
return;
}
//make sure rx data cleaned
Sleep(100);
bReadStatus = ReadFile( hPort, buffer, 10, &dwBytesRead, NULL);
//write imei
timeout = 1;
Sleep(50);
for(i=0;i<8;i++)
{
//send command
command.Empty();
if(type == 0)
command.Format("AT+EGMR=1,5,");
else
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 to port. Try again.");
if(debug) PrintError("AT+EGMR=1");
CloseHandle(hPort);
return;
}
//receive response
Sleep(200);
memset(buf,0,sizeof(buf));
buffer=buf;
if(type == 0)
bReadStatus = ReadFile( hPort, buffer, 80, &dwBytesRead, NULL);
else
bReadStatus = ReadFile( hPort, buffer, 40, &dwBytesRead, NULL);
if(debug)
PrintError(CString(buffer));
if(dwBytesRead != 0)
{
p=strstr(buffer,"OK");
if(p)
{
timeout=0;
break;
}
}
}
//check if failed
if(timeout == 1)
{
PrintError("Failed reading port. Try again.");
if(debug) PrintError("AT+EGMR=1");
CloseHandle(hPort);
return;
}
//read imei and check
timeout = 1;
Sleep(50);
for(i=0;i<8;i++)
{
//send command
command.Empty();
if(type == 0)
command.Format("AT+EGMR=0,5\r\n");
else
command.Format("AT+EGMR=0,7\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");
CloseHandle(hPort);
return;
}
//receive response
Sleep(200);
memset(buf,0,sizeof(buf));
buffer=buf;
if(type == 0)
bReadStatus = ReadFile( hPort, buffer, 80, &dwBytesRead, NULL);
else
bReadStatus = ReadFile( hPort, buffer, 60, &dwBytesRead, NULL);
if(debug)
PrintError(CString(buffer));
if(dwBytesRead != 0)
{
//check readback imei
p=strstr(buffer,"OK");
if(p)
{
timeout=0;
break;
}
}
}
//check if failed
if(timeout == 1)
{
PrintError("Failed reading port. Try again.");
if(debug) PrintError("AT+EGMR=0");
CloseHandle(hPort);
return;
}
//show success information
gColor = RGB(0,255,0);
gFinalResult.Format(" OK");
RedrawWindow();
CTime t = CTime::GetCurrentTime();
CString szTemp;
szTemp.Format("%02d:%02d:%02d ",t.GetHour(),t.GetMinute(),t.GetSecond());
m_Result.GetWindowText(sTemp);
if(type == 0)
m_Result.SetWindowText(szTemp+"Succeed writing SN:"+m_IMEI+".\r\n"+sTemp);
else
m_Result.SetWindowText(szTemp+"Succeed writing IMEI:"+m_IMEI+".\r\n"+sTemp);
//close the port
CloseHandle(hPort);
//enable write button
if(!autow)
m_Write.EnableWindow(TRUE);
}
void CWriteIMEIDlg::OnSelchangeType()
{
// TODO: Add your control notification handler code here
int type;
//get type
type = m_Type.GetCurSel();
if(type== 0)
{
m_Write.SetWindowText("Write SN");
m_Static_Type.SetWindowText("SN:");
CEdit *pEdit1;
pEdit1=(CEdit*)GetDlgItem(IDC_IMEI);
pEdit1->SetLimitText(30);
pEdit1->SetWindowText("");
}
else
{
m_Write.SetWindowText("Write IMEI");
m_Static_Type.SetWindowText("IMEI:");
CEdit *pEdit1;
pEdit1=(CEdit*)GetDlgItem(IDC_IMEI);
pEdit1->SetLimitText(15);
pEdit1->SetWindowText("");
}
}
void CWriteIMEIDlg::OnSelchangeAuto()
{
// TODO: Add your control notification handler code here
//get auto
autow = m_Auto.GetCurSel();
if(autow)
m_Write.EnableWindow(FALSE);
else
m_Write.EnableWindow(TRUE);
//clear IMEI
CEdit *pEdit1;
pEdit1=(CEdit*)GetDlgItem(IDC_IMEI);
pEdit1->SetWindowText("");
}
void CWriteIMEIDlg::OnChangeImei()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
autow = m_Auto.GetCurSel();
UpdateData(TRUE);
if(autow)
{
if(m_IMEI.GetLength())
{
OnWrite();
CEdit *pEdit1;
pEdit1=(CEdit*)GetDlgItem(IDC_IMEI);
pEdit1->SetWindowText("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -