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

📄 writeflashdlg.cpp

📁 用vc自己编写flash烧录的源代码 可以实际使用进行flash烧写
💻 CPP
字号:
// writeflashDlg.cpp : implementation file
//

#include "stdafx.h"
#include "writeflash.h"
#include "writeflashDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CWriteflashDlg dialog

CWriteflashDlg::CWriteflashDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWriteflashDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWriteflashDlg)
	m_rxdata = _T("");
	m_page = 0;
	m_count = 0;
	m_count1 = 0;
	m_sendone = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWriteflashDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWriteflashDlg)
	DDX_Control(pDX, IDC_COMBOPORT, m_combport);
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Text(pDX, IDC_RXDATA, m_rxdata);
	DDX_Text(pDX, IDC_PAGE, m_page);
	DDX_Text(pDX, IDC_COUNT, m_count);
	DDX_Text(pDX, IDC_COUNT1, m_count1);
	DDX_Text(pDX, IDC_EDITONE, m_sendone);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWriteflashDlg, CDialog)
	//{{AFX_MSG_MAP(CWriteflashDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FILEOPEN, OnFileopen)
	ON_CBN_SELCHANGE(IDC_COMBOPORT, OnSelchangeComboport)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK, OnCheck)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_ENABLE, OnEnable)
	ON_BN_CLICKED(IDC_NULL, OnNull)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_SENDONE, OnSendone)
	ON_BN_CLICKED(IDC_STOPDOWN, OnStopdown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWriteflashDlg message handlers

BOOL CWriteflashDlg::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
	oxflag=false;
	showflag=true;
	startflag=0;
	incount=0;
	m_chChecksum=0; //校验和置0
	m_nRXErrorCOM1=0; //COM1接收数据错误帧数置0
	m_nRXCounterCOM1=0; //COM1接收数据错误帧数置0
	m_strRXhhCOM1.Empty(); //清空半BYTE校验hh存储变量
	count=0;
	comport=4;
	if (m_ComPort.InitPort(this,comport,9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
	//portnr=1(2),baud=960,parity='N',databits=8,stopsbits=1,
	//dwCommEvents=EV_RXCHAR|EV_RXFLAG,nBufferSize=512
		{
		m_ComPort.StartMonitoring(); //启动串口监视线程
		}
	else
		{
		CString str;
		str.Format("COM%d not found",4);
		AfxMessageBox(str);
		}
	return 0;
}

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

void CWriteflashDlg::OnFileopen() 
{
	// TODO: Add your control notification handler code here
	char temp[150000],temp1[150000];  //数组存在最大空间问题 
	int i=100,j;
	long int cc,cc1;
	CFileException e;
	CString s;
    CFile myFile;
	CString path ;//= m_dlgHelper.GetItemPathName(i);
	CFileDialog FileDlg(TRUE,
	NULL,											  // no default extension
	NULL,											  // ..or file name
	OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
	_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"));

	AfxMessageBox("Please check the protect key up!");
    if ( IDOK == FileDlg.DoModal())
	{
		POSITION pos = FileDlg.GetStartPosition();
		while (pos != NULL)
		{ 
			s=FileDlg.GetNextPathName(pos);
		}
		s = FileDlg.GetPathName();
		myFile.Open( s, CFile::modeRead, &e );
		totalcount=myFile.GetLength();
		UINT nBytesRead = myFile.Read( temp, totalcount);
		myFile.Close(); 
		cc1=0;
		for(cc=0;cc<totalcount;cc++)
		{
			i=temp[cc];
			if((i>47)&&(i<55))
			{
				temp1[cc1++]=temp[cc];
				if(cc1==65532)  
				for(j=0;j<4;j++) temp1[cc1++]=6; // 65532-65535 之间添加几个数字 不要的
			}
		}
		tempf=temp1;
		CString length;
		CString strtemp;
		totalcount=cc1; //总得有效字符数
		totalpage=totalcount/128;  //总得页面数
		//length.Format("%d",totalcount); 
		//AfxMessageBox(length);
		m_page=totalpage*128/12;
		UpdateData(false);
		pagecount=0;
		m_count1=0;
		senddata[1]=0;
		senddata[0]=0x7b;
		m_ComPort.WriteToPort(senddata);//发送数据;//发送数据
		Sleep(50);
		senddata[0]=0x7f;
		m_ComPort.WriteToPort(senddata);//发送数据//发送数据
		Sleep(50);
		SetTimer(2,5,NULL); 
	}
	linecount=0;
	bi_count=1;
	m_rxdata.Format("%d:  ",0);
}

void CWriteflashDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
			int txdata;
			BYTE enddata;
			char *ttr;
			int progress;
			CString strtemp,bi_str;
			senddata[1]=0;
			bi_str.Format("%d:  ",bi_count);
			//if(bi_count==0) m_rxdata=bi_str;
			ttr="\r\n";
			count=pagecount*128+m_count1*2;
			txdata =*(tempf+count); 
			txdata-=48;
			enddata=txdata*16;//<<4;	
			enddata&=0xf0;
			txdata=*(tempf+count+1);
			txdata-=48;
			enddata+=txdata;
			enddata+=0x11;   //0x00发送出错  全部加1 
			senddata[0]=enddata;
			m_ComPort.WriteToPort(senddata);   //发送数据
			m_count1++;
			enddata-=0x11;
			strtemp.Format("%02X ",enddata); 
			if(m_count1!=65)//重复发送不换行  不加进去
			{	m_rxdata+=strtemp;
				linecount++;
			}
			if(linecount>=6)//24)   //换行
				{
				bi_count++;
				linecount=0;
				m_rxdata+=ttr;
				m_rxdata+=bi_str;
				UpdateData(false);
				}
			if(m_count1>=65)//发送完毕一页
			{

				//m_ComPort.WriteToPort(senddata);   //重复发送一次  发送数据
				
				m_count1=0;
				senddata[0]=0x7a;//开始一页得写 入	
				m_ComPort.WriteToPort(senddata);//发送数据
				//Sleep(10); srr而
				pagecount++;
				//if(pagecount%12==0) m_rxdata=_T("");
				if(pagecount>totalpage)
					{
						count=0;
						senddata[0]=0x7c;//
						m_ComPort.WriteToPort(senddata);
						KillTimer(2);
						progress=pagecount*100/totalpage;
						m_progress.SetPos(progress);
						AfxMessageBox("髮送完畢!");
					}
			}
			//Sleep(2);
			m_count=pagecount*128/12;
			progress=pagecount*100/totalpage;
			m_progress.SetPos(progress);
			CDialog::OnTimer(nIDEvent);
	
}
void CWriteflashDlg::OnCheck()   //printf  接受得
{
	// TODO: Add your control notification handler code here
	CString str;
	char *ttr;
	//	AfxMessageBox("ok");
	//checkflag=true;
	//	BYTE enddata;
	senddata[1]=0;
	oxflag=0;
	count=0;
	m_rxdata=_T("");
	senddata[1]=0x7d;//开始一页得写入
	m_ComPort.WriteToPort(senddata);
	count=0;
			str.Format("%d",count);
			ttr=": ";
			str+=ttr;
			m_rxdata+=str;
}
//接受数据处理
LONG CWriteflashDlg::OnCommunication(WPARAM ch, LPARAM port)  //接受数据处理
{
	static char c1,c2;
	char *s=NULL;
	//WORD i;
	CFile myFile;
    CFileException e;
	char *ttr;
	BYTE temp;
	CString str,filename;

	temp=(BYTE)ch;
	if(oxflag)
		str.Format("%02X ",temp);  //十六进制接受
	else 
		str.Format("%c",temp);  //字符型接受
	  KillTimer(2); 
	m_rxdata+=str;
	//	UpdateData(false);
	if(ch==0x0a) 
		{
			count++;
			str.Format("%d",count);
			ttr=": ";
			str+=ttr;
			m_rxdata+=str;
			m_count=count;
			UpdateData(false);
		}

		//存储文件
		if(count>=9999)
		{ 
		ttr="data.txt";
		filename=ttr;
		myFile.Open(filename, CFile::modeCreate | CFile::modeWrite, &e );
		myFile.Write(m_rxdata,count*(incount+19));
		//ttr="";
		//m_rxdata=ttr;
		}

	    //AfxMessageBox("get it!");

	return 0;
}

void CWriteflashDlg::OnSelchangeComboport() 
{
	// TODO: Add your control notification handler code here
		int n;
	this->UpdateData(true);

	this->Invalidate();

	n = m_combport.GetCurSel();
	//show.Format("%d ",n);
	//AfxMessageBox(show);
	if(n == CB_ERR)
	{
	}
	else
	{
	 	comport=n+1;

	if (m_ComPort.InitPort(this,comport,9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
		{
		m_ComPort.StartMonitoring(); //启动串口监视线程

		CString str;
		str.Format("COM%d 打開!",comport);
		AfxMessageBox(str);
		}
	else
		{
		CString str;
		str.Format("COM%d 沒有髮現或者被佔用",comport);
		AfxMessageBox(str);
		}
	}
}

void CWriteflashDlg::OnStop() 
{

	senddata[1]=0;
	count=0;
	senddata[0]=0x7e;//开始一页得写入
	m_ComPort.WriteToPort(senddata);
}


void CWriteflashDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_rxdata=_T("");
	UpdateData(false);
}

void CWriteflashDlg::OnEnable() 
{
	// TODO: Add your control notification handler code here

		count=0;
		senddata[1]=0;
		senddata[0]=0x7b;//开始一页得写入
		m_ComPort.WriteToPort(senddata);
}

void CWriteflashDlg::OnNull() 
{
	// TODO: Add your control notification handler code here

		senddata[1]=0;
		senddata[0]=0x7f;//开始一页得写入

		m_ComPort.WriteToPort(senddata);
}

void CWriteflashDlg::OnWrite() 
{
	// TODO: Add your control notification handler code here
	
		senddata[1]=0;
		senddata[0]=0x7a;//开始一页得写入
		m_ComPort.WriteToPort(senddata);
}

void CWriteflashDlg::OnSendone() 
{
	// TODO: Add your control notification handler code here

		UpdateData(true);
		senddata[1]=0;
		senddata[0]=m_sendone;//开始一页得写入
		m_ComPort.WriteToPort(senddata);
}

void CWriteflashDlg::OnStopdown() 
{
	// TODO: Add your control notification handler code here
	KillTimer(2);
}

⌨️ 快捷键说明

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