📄 downloadtooldlg.cpp
字号:
// DownloadToolDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DownloadTool.h"
#include "DownloadToolDlg.h"
#include "SettingDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CHECK_TIME 100
#define RECV_TIME_OUT 60*60*1000
CEdit *g_pTheEdit = NULL;
CProgressCtrl *g_pProgressCtrl = NULL;
CString CDownloadToolDlg::m_strSelFileName;
/*map*/
CString Order_to_COMName[] = {
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
};
int Order_to_BitPerSec[] ={
CBR_110,
CBR_300,
CBR_1200,
CBR_2400,
CBR_4800,
CBR_9600,
CBR_19200,
CBR_38400,
CBR_57600,
CBR_115200,
230400,
460800,
921600,
};
/* order:DataBits:
5
6
7
8
*/
int CDownloadToolDlg::m_nBitPerSec = DEFAULT_BitPerSec;
int CDownloadToolDlg::m_nSelCOM = DEFAULT_SelCOM;
int CDownloadToolDlg::m_nDataBits = DEFAULT_DataBits;
int CDownloadToolDlg::m_nFlowCtrl = DEFAULT_FlowCtrl;
int CDownloadToolDlg::m_nParity = DEFAULT_Parity;
int CDownloadToolDlg::m_nBlockSize = DEFAULT_BlockSize;
UINT CDownloadToolDlg::m_nTimeOut = DEFAULT_TimeOut;
int g_bDownloading = 0;
int g_bNeedToStop = 0;
char g_DisBuf[5] = {'-','\\','|','/','-'};
/////////////////////////////////////////////////////////////////////////////
void DisplayMsg(CString msg)
{
if(g_pTheEdit == NULL)
{
return;
}
if(g_pTheEdit->GetLineCount() > 50)
{
g_pTheEdit->SetSel(0,g_pTheEdit->GetWindowTextLength());
g_pTheEdit->ReplaceSel("\r\n");
}
g_pTheEdit->SetSel(g_pTheEdit->GetWindowTextLength(),g_pTheEdit->GetWindowTextLength());
g_pTheEdit->ReplaceSel(msg);
}
void DisplayMsgEx(CString msg)
{
if(g_pTheEdit == NULL)
{
return;
}
if(g_pTheEdit->GetLineCount() > 50)
{
g_pTheEdit->SetSel(0,g_pTheEdit->GetWindowTextLength());
g_pTheEdit->ReplaceSel("\r\n");
}
g_pTheEdit->SetSel(g_pTheEdit->GetWindowTextLength() - 1,g_pTheEdit->GetWindowTextLength());
g_pTheEdit->ReplaceSel(msg);
}
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDownloadToolDlg dialog
CDownloadToolDlg::CDownloadToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDownloadToolDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDownloadToolDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDownloadToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDownloadToolDlg)
DDX_Control(pDX, IDC_PROGRESS1, m_ProgressCtrl);
DDX_Control(pDX, IDC_EDIT_MSG, m_ctrlMsgEdit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDownloadToolDlg, CDialog)
//{{AFX_MSG_MAP(CDownloadToolDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnOk)
ON_BN_CLICKED(IDSELECT, OnSelect)
ON_BN_CLICKED(IDSTART, OnStart)
ON_BN_CLICKED(IDSTOP, OnStop)
ON_BN_CLICKED(IDSETTING, OnSetting)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDownloadToolDlg message handlers
BOOL CDownloadToolDlg::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
g_pTheEdit = &m_ctrlMsgEdit;
g_pProgressCtrl = &m_ProgressCtrl;
SetTimer(1, 200, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDownloadToolDlg::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 CDownloadToolDlg::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 CDownloadToolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDownloadToolDlg::OnOk()
{
// TODO: Add your control notification handler code here
}
void CDownloadToolDlg::OnSelect()
{
// TODO: Add your control notification handler code here
CFileDialog filedlg(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT, NULL, this);
if (filedlg.DoModal() != IDOK)
{
return;
}
m_strSelFileName = filedlg.GetPathName();
}
void CDownloadToolDlg::OnStart()
{
// TODO: Add your control notification handler code here
if(g_bDownloading == 0)
{
AfxBeginThread(DownloadThread, (LPVOID)NULL);
g_bDownloading = 1;
}
else
{
g_bNeedToStop = 1;
}
}
void CDownloadToolDlg::OnStop()
{
// TODO: Add your control notification handler code here
g_bNeedToStop = 1;
Sleep(100);
}
void CDownloadToolDlg::OnSetting()
{
// TODO: Add your control notification handler code here
CSettingDlg dlg;
dlg.m_nBitPerSec =m_nBitPerSec;
dlg.m_nSelCOM =m_nSelCOM ;
dlg.m_nDataBits =m_nDataBits;
dlg.m_nFlowCtrl =m_nFlowCtrl;
dlg.m_nParity =m_nParity ;
dlg.m_nBlockSize =m_nBlockSize;
dlg.m_nTimeOut =m_nTimeOut;
if(dlg.DoModal() != IDOK)
{
return;
}
m_nBitPerSec = dlg.m_nBitPerSec;
m_nSelCOM = dlg.m_nSelCOM;
m_nDataBits = dlg.m_nDataBits;
m_nFlowCtrl = dlg.m_nFlowCtrl;
m_nParity = dlg.m_nParity ;
m_nBlockSize = dlg.m_nBlockSize;
m_nTimeOut = dlg.m_nTimeOut;
}
UINT CDownloadToolDlg::DownloadThread(LPVOID)
{
HANDLE hFile_com;
CFile file_data;
int nFileSize, nReadSize, nWriteSize;
unsigned short usCurBlock, usTotalBlock, usTotalBlock_tmp;
static char buf[15000], buf_com[1500];
DWORD dwError;
CString msg;
DWORD error;
COMSTAT ComStat;
//SECURITY_ATTRIBUTES sa;
int nBitPerSec = m_nBitPerSec;
int nSelCOM = m_nSelCOM ;
//int nDataBits = m_nDataBits ;
//int nFlowCtrl = m_nFlowCtrl ;
//int nParity = m_nParity ;
int nBlockSize = m_nBlockSize;
UINT nTimeOut = m_nTimeOut ;
g_bDownloading = 1;
Sleep(100); /*wait the idle thread end*/
// TODO: Add your command handler code here
if(m_strSelFileName.GetLength() == 0)
{
msg.Format("Please select a file first!\r\n");
DisplayMsg( msg );
g_bDownloading = 0;
return -1;
}
else
{
msg.Format("\r\nStart to download %s...\r\n", m_strSelFileName);
DisplayMsg( msg );
}
hFile_com = CreateFile(
/*"COM5"*/Order_to_COMName[nSelCOM],
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if(hFile_com == NULL)
{
msg.Format("fail to open %s!",Order_to_COMName[nSelCOM]);
AfxMessageBox( msg );
g_bDownloading = 0;
return -1;
}
DCB dcb;
memset(&dcb, 0x00, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
if(::GetCommState( hFile_com, &dcb )==0)
{
msg.Format("\r\nGet %s State failed! error=%d.\r\n",Order_to_COMName[nSelCOM],GetLastError());
DisplayMsg( msg );
g_bDownloading = 0;
CloseHandle(hFile_com);
return -1;
}
dcb.BaudRate = Order_to_BitPerSec[nBitPerSec]/*CBR_115200*/;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -