📄 updatepcdlg.cpp
字号:
// UpdatePCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UpdatePC.h"
#include "UpdatePCDlg.h"
#include "Public.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern BYTE g_iPort;
extern DWORD g_Progress;
BOOL g_HandshakeSucceed=FALSE;
BOOL g_ReadySendData=FALSE;
BOOL g_FileEnd=FALSE;
long g_FileLong,g_MaxWriteCount,g_WriteCount=0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUpdatePCDlg dialog
CUpdatePCDlg::CUpdatePCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUpdatePCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUpdatePCDlg)
m_FileName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUpdatePCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUpdatePCDlg)
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
DDX_Text(pDX, IDC_FILENAME, m_FileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUpdatePCDlg, CDialog)
//{{AFX_MSG_MAP(CUpdatePCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_COMM_RXCHAR, OnRXCHAR)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUpdatePCDlg message handlers
BOOL CUpdatePCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
((CComboBox*)GetDlgItem(IDC_COMBO_COM))->SetCurSel(0);
(GetDlgItem(IDC_STATIC2))->ShowWindow(SW_HIDE);
(GetDlgItem(IDC_PROGRESS))->ShowWindow(SW_HIDE);
(GetDlgItem(IDC_STATIC3))->ShowWindow(SW_HIDE);
WriteBuffer2[0]=0;
WriteBuffer2[1]=0;
WriteBuffer3[0]=0;
WriteBuffer3[1]=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CUpdatePCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CUpdatePCDlg::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 CUpdatePCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
HBRUSH CUpdatePCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(pWnd->GetDlgCtrlID() == IDC_STATIC2)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));
//RGB(255, 0, 0)//红色
//RGB(255, 255, 0)//黄色
//RGB(0, 0, 255)//兰色
// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);
}
if(pWnd->GetDlgCtrlID() == IDC_STATIC3)
{
pDC->SetTextColor(RGB(0, 0, 255));
pDC->SetBkMode(TRANSPARENT);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
void CUpdatePCDlg::OnOpenfile()
{
// TODO: Add your control notification handler code here
static char BASED_CODE szFilter[] = "BIN Files (*.bin)|*.bin||";
CFileDialog m_CFileDialog(TRUE,NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);
if(m_CFileDialog.DoModal()==IDOK)
{
m_FileName=m_CFileDialog.GetPathName();
UpdateData(FALSE);
}
}
/*
void DownloadThread(LPVOID pParam)
{
CUpdatePCDlg* pDlg=(CUpdatePCDlg*)pParam;
pDlg->SetTimer(1,100,NULL);
g_Progress=0;//for progress bar
while(1)
{
if(g_Progress>=100)
{
pDlg->KillTimer(1);
pDlg->GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE);
AfxMessageBox("Format Complete!");
pDlg->GetDlgItem(IDC_COMBO_COM)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_OPENFILE)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_DOWNLOAD)->EnableWindow(TRUE);
AfxEndThread(0);
return;
}
}
}
*/
void CUpdatePCDlg::OnDownload()
{
// TODO: Add your control notification handler code here
int i=MessageBox("All data will be erased!\nWould you like to continue?","Warning",MB_ICONINFORMATION | MB_OKCANCEL);
if(i==IDOK)
{
int PrePort=g_iPort;//获得先前的串口
g_iPort=((CComboBox*)GetDlgItem(IDC_COMBO_COM))->GetCurSel()+1;//获得先前的串口
if((PrePort!=0)&&(PrePort!=g_iPort))//关闭先前的串口
{
m_Port.StopMonitoring();
m_Port.CloseCOM();
}
if (m_Port.InitPort(this, g_iPort,115200))
{
m_Port.StartMonitoring();
}
else
{
AfxMessageBox(IDS_SerialErrorMsg);
return;
}
if((m_pFileName = fopen(m_FileName,"rb")) == NULL )
{
AfxMessageBox("Can not Open file!");
return;
}
fseek(m_pFileName,0,SEEK_END);
g_FileLong=ftell(m_pFileName);
g_MaxWriteCount=g_FileLong/1024;
if((g_MaxWriteCount==0)||(g_FileLong%1024))
{
AfxMessageBox("The BIN file long is inavlid!");
}
fseek(m_pFileName,0,SEEK_SET);
g_WriteCount=0;
//CWinThread* m_pMeThread=AfxBeginThread((AFX_THREADPROC)DownloadThread,this); //创建工作者线程的方法
}
}
void CUpdatePCDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
static int k=0;
char buff1[16]="";
k++;
if(k>15)k=0;
for(int i=0;i<k;i++)
strcat(buff1,">");
GetDlgItem(IDC_STATIC2)->SetWindowText(buff1);
//g_WriteCount++;//for test
if(g_Progress<100)
{
char buff2[5];
g_Progress=(g_WriteCount*1024*100)/g_FileLong;
m_Progress.SetPos(g_Progress);
sprintf(buff2,"%d",g_Progress);
strcat(buff2,"%");
GetDlgItem(IDC_STATIC3)->SetWindowText(buff2);
}
else //if(g_Progress>=100)
{
KillTimer(1);
GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE);
AfxMessageBox("Format Complete!");
GetDlgItem(IDC_COMBO_COM)->EnableWindow(TRUE);
GetDlgItem(IDC_OPENFILE)->EnableWindow(TRUE);
GetDlgItem(IDC_DOWNLOAD)->EnableWindow(TRUE);
}
}
CDialog::OnTimer(nIDEvent);
}
LONG CUpdatePCDlg::OnRXCHAR(WPARAM ch, LPARAM port)
{
if (port <= 0 || port > 4)
return -1;
long i,j;
switch(ch)
{
case '0'://init port
GetDlgItem(IDC_COMBO_COM)->EnableWindow(FALSE);
GetDlgItem(IDC_OPENFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_DOWNLOAD)->EnableWindow(FALSE);
(GetDlgItem(IDC_STATIC2))->ShowWindow(SW_SHOW);
(GetDlgItem(IDC_PROGRESS))->ShowWindow(SW_SHOW);
(GetDlgItem(IDC_STATIC3))->ShowWindow(SW_SHOW);
SetTimer(1,100,NULL);
g_Progress=0;//for progress bar
g_HandshakeSucceed=TRUE;
// for(i=0;i<10;i++)
m_Port.WriteToPort("01234567890123456");
break;
case '2'://check file end
if(g_HandshakeSucceed)
{
if(g_WriteCount<g_MaxWriteCount)
{
g_ReadySendData=TRUE;
m_Port.WriteToPort("3");//file not end
}
else
{
g_HandshakeSucceed=FALSE;
m_Port.WriteToPort("5");//file end
}
}
break;
case '4'://request 1k to pc
if(g_HandshakeSucceed)
{
if(g_ReadySendData)
{
g_ReadySendData=FALSE;
memset(WriteBuffer1,0,1024);
int WriteCount = fread(WriteBuffer1,sizeof(char),1024,m_pFileName);
if(WriteCount!=1024)
{
MessageBox("Read file error!");
return -1;
}
char buf[2];
m_Port.WriteToPort("A");
for(i=0;i<1024;i++)
{
for(j=0;j<1024;j++)
{
;
}
}
m_Port.WriteToPort("B");
for(i=0;i<1024;i++)
{
for(j=0;j<10240;j++)
{
;
}
}
m_Port.WriteToPort("C");
for(i=0;i<10240;i++)
{
for(j=0;j<10240;j++)
{
;
}
}
unsigned int sum=0;
/* for(int i=0;i<1024;i++)
{
sum+=WriteBuffer1[i];
buf[0]=WriteBuffer1[i];
m_Port.WriteToPort(buf);
}
*/ WriteBuffer2[0]=(sum>>8)&0x00ff;
WriteBuffer3[0]=sum&0x00ff;
g_WriteCount++;
// m_Port.WriteToPort(WriteBuffer1);
// m_Port.WriteToPort(WriteBuffer2);//high
// m_Port.WriteToPort(WriteBuffer3);//low
}
}
break;
case '6'://retry current data 1k
if(g_HandshakeSucceed)
{
m_Port.WriteToPort(WriteBuffer1);
m_Port.WriteToPort(WriteBuffer2);//high
m_Port.WriteToPort(WriteBuffer3);//low
}
break;
default:
break;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -