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

📄 m8dlg.cpp

📁 通过串口升级mega8的自编程例子原码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// M8Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "M8.h"
#include "M8Dlg.h"
#include "SerialPort.h"
#include "PortSetDlg.h"
//#include "ReceiveData.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CM8Dlg dialog

CM8Dlg::CM8Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CM8Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CM8Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_ReceiveCount = -1;
	m_ReceiveStartCount = 0;
	m_ReceiveStart = FALSE;
	m_ReceiveFlag = FALSE;
	m_SendData.m_Type = 0x01;
	m_Type_B = 0;
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CM8Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CM8Dlg)
	DDX_Control(pDX, IDC_RECEVE_EDIT, m_ReceveEdit);
	DDX_Control(pDX, IDC_SEND_EDIT, m_SendEdit);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CM8Dlg, CDialog)
	//{{AFX_MSG_MAP(CM8Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OpenFile, OnOpenFile)
	ON_BN_CLICKED(IDC_PORT_SET, OnPortSet)
	ON_BN_CLICKED(IDC_SEND_AUTO, OnSendAuto)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CM8Dlg message handlers

BOOL CM8Dlg::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
	CSerialPort m_TestPort;
	CString n_str;
	for (int i = 0; i < 4; i++)
	{
		if (m_TestPort.InitPort(this, i+1, 9600))
		{
			n_str.Format(_T("COM%d"), i+1);
			m_PortArray.Add(n_str);
		}
	}
	m_SendPortData.nPort = 1;
	m_SendPortData.nBaud = 9600;
	m_SendPortData.nParity = 'n';
	m_SendPortData.nDatabits = 8;
	m_SendPortData.nStopsbits = 1;

	if (nSendPort.InitPort(this, 1, 9600, 'n'))
	{
		nSendPort.StartMonitoring();
		SetDlgItemText(IDC_PORT_STATIC, "COM1 9600 n 8 1");
		GetDlgItem(IDC_PORT_SET)->SetWindowText("COM1 9600");
	}
	else 
	{
		MessageBox("COM1: not fount!!!");
		SetDlgItemText(IDC_PORT_STATIC, "COM1: not fount!!!");
		GetDlgItem(IDC_PORT_SET)->SetWindowText("串口设置");
		m_SendPortData.nPort = 0;

	}
	GetDlgItem(IDC_SEND_AUTO)->EnableWindow(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CM8Dlg::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 CM8Dlg::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 CM8Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CM8Dlg::OnOpenFile() 
{
	m_SendEdit.SetWindowText("");
	m_ReceveEdit.SetWindowText("");
	
	m_ReceiveCount = -1;
	m_ReceiveStartCount = 0;
	m_ReceiveStart = FALSE;
	m_ReceiveFlag = FALSE;

	m_SendData.m_Type = 0x01;
	m_Type_B = 0;//初始化全局变量

	m_SendData.m_Style = 0;
	SetDlgItemText(IDC_SELECT, "升级文件:ATMEGA8L");
	CString m_DlgFileName;
	CString m_FileName;
	CString n_Str =  _T("") ;
	CFileDialog dlg(TRUE, "HEX", n_Str, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		  "程序文件 (*.HEX)|*.HEX||", this);
	if (dlg.DoModal() == IDOK)
	{
		m_SendData.m_HexData.RemoveAll();
		m_SendData.m_HexPage.RemoveAll();
		for (int i =0 ; i < 61440 ; i++) m_SendData.m_HexData.Add(0xFF);
		m_DlgFileName = dlg.GetPathName();	
		m_FileName = dlg.GetFileName();
		ProcessReplace(m_DlgFileName);
		CString n_str_temp;
		GetDlgItemText(IDC_SELECT, n_str_temp);
		n_str_temp = n_str_temp + _T(" ") +m_FileName;
		SetDlgItemText(IDC_SELECT, n_str_temp);
		int n_Size = m_SendData.m_HexPage.GetSize();//页数
		m_Progress.SetRange(0, (n_Size-1)*2);
		m_Progress.SetStep(1);
		m_Progress.ShowWindow(SW_HIDE);
	}
}
BOOL CM8Dlg::ProcessReplace(CString n_FileName)
{
	CFile m_File;
	CFileException ex;
	if (!m_File.Open(n_FileName, CFile::modeRead|CFile::shareExclusive, &ex)) return FALSE;

	CString n_Str;
	BYTE n_temp;
	int  n_Num,n_Num1;
	int n_Length; //行长度
	int n_Count,n_Count1; //计数器

	n_Count = 0;
	n_Count1 = 129;
	while (1)
	{	
		if (m_File.Read(n_Str.GetBuffer(1), 1) != 1) break; //行头
		n_Str.ReleaseBuffer(1);
		if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break; //长度
		n_Str.ReleaseBuffer(2);
		n_temp = HexToByte(n_Str);
		n_Length = n_temp;
		if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break; //起始地址1
		n_Str.ReleaseBuffer(2);
		n_temp = HexToByte(n_Str);
		n_Num = n_temp * 256;
		if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break;//起始地址2
		n_Str.ReleaseBuffer(2);
		n_temp = HexToByte(n_Str);
		n_Num = n_temp + n_Num;  //起始地址
		if(n_Length == 0 && n_Num == 0)
		{
			if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break; //长度
			n_Str.ReleaseBuffer(2);
			n_temp = HexToByte(n_Str);
			if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break; //长度
			n_Str.ReleaseBuffer(2);
			n_temp = HexToByte(n_Str);
			break;

		}

	/* 记录页码	*/	
		n_Num1 = (n_Num / 64) ;

		if (n_Count1 != n_Num1)
		{
			n_Count1 = n_Num1;
			m_SendData.m_HexPage.Add(n_Count1);
		}
		
		if (m_File.Read(n_Str.GetBuffer(2), 2) != 2)  break;
		n_Str.ReleaseBuffer(2);
		n_Str.Remove(' ');

		n_Count = n_Num;

⌨️ 快捷键说明

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