📄 commtestdlg.cpp
字号:
#include "stdafx.h"
#include "commtest.h"
#include "commtestDlg.h"
#include "configdlg.h"
#include "MediaTimer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define REC_MARK 0x01
#define REC_LEN_1 0x02
#define REC_LEN_2 0x03
#define OFFSET_1 0x04
#define OFFSET_2 0x05
#define OFFSET_3 0x06
#define OFFSET_4 0x07
#define REC_TYP_1 0x08
#define REC_TYP_2 0x09
#define DATA_1 0x0A
#define DATA_2 0x0B
#define CHEKSUM_1 0x0C
#define CHEKSUM_2 0x0D
#define HEX_DEC_OK 0x00
#define HEX_DEC_START 0x01 //开始
#define HEX_DEC_REDY 0x02 //就绪
#define HEX_DEC_SEG_DATA 0x03 //段地址
#define HEX_DEC_SEG_ERR 0x04 //段错误
#define HEX_DEC_SEG_OK 0x05 //段正确
#define HEX_DEC_SEG_IRQ 0x06 //段请求
#define HEX_DEC_SEG_NEXT 0x07 //下一段
#define HEX_DEC_SEG_END 0x08 //段结束
#define HEX_DEC_FRAME_DATA 0x09 //帧数据
#define HEX_DEC_FRAME_ERR 0x0A //帧错误
#define HEX_DEC_FRAME_OK 0x0B //帧数据
#define HEX_DEC_FRAME_IRQ 0x0C //帧请求
#define HEX_DEC_FRAME_NEXT 0x0D //下一帧
#define HEX_DEC_FRAME_END 0x0E //帧结束
#define HEX_DEC_END 0x0F //结束
#define HEX_DEC_VERIFY_START 0x10 //帧比较开始
#define HEX_DEC_VERIFY_REDY 0x11 //比较就绪
#define HEX_DEC_SEG_VERIFY_DATA 0x12 //比较段地址
#define HEX_DEC_SEG_VERIFY_ERR 0x13 //比较段段错误
#define HEX_DEC_SEG_VERIFY_OK 0x14 //比较段段正确
#define HEX_DEC_FRAME_VERIFY_DATA 0x15 //比较帧数据
#define HEX_DEC_FRAME_VERIFY_ERR 0x16 //接收帧错误
#define HEX_DEC_VERIFY_FRAME_IRQ 0x17 //比较帧请求
#define HEX_DEC_VERIFY_FRAME_NEXT 0x18 //比较帧继续
#define HEX_DEC_VERIFY_FRAME_END 0x19 //比较帧结束
#define HEX_DEC_PROGRAM_ERR 0x1A //烧写错误
#define HEX_DEC_SECTOR_IRQ 0x1B //FLASH扇区设置
#define HEX_DEC_SECTOR_BANK 0x1C //FLASH扇区
#define HEX_DEC_ERR 0x20 //错误
//定时器回调函数
void PASCAL TimerProc(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2);
CCommtestDlg::CCommtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommtestDlg)
// NOTE: the ClassWizard will add member initialization here
m_setStep = 1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subseqent DestroyIcon in Win32
status = HEX_DEC_OK;
Binptr = Filesbuf; //64K数据地址
m_CounterSector = 0; //扇区数初始化
m_Bank = 0; //程序段初始化
ErrNum = 0;
FileErrNum = 0;
m_ProcessType = 0x88; //处理过程代码
m_FileNameEx[0]= "H00";
m_FileNameEx[1]= "H01";
m_FileNameEx[2]= "H02";
m_FileNameEx[3]= "H03";
m_Pathname.Empty();
memset(Filesbuf, 0, sizeof(Filesbuf));
m_hIcon = AfxGetApp()->LoadIcon(IDR_LOGO);
}
void CCommtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommtestDlg)
DDX_Control(pDX, IDC_PROGRESSCTRL, m_prog);
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
//{{AFX_MSG_MAP(CCommtestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
ON_BN_CLICKED(IDC_SeriaPortSet, OnSeriaPortSet)
ON_BN_CLICKED(IDC_FILE_OPEN, OnFileOpen)
ON_BN_CLICKED(IDC_VERIFY, OnVerify)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CCommtestDlg::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
m_setStep=1;
// init the ports
if (m_Ports.InitPort(this, 1, 115200))
m_Ports.StartMonitoring();
m_mediaTimer.SetProcParameter(TimerProc,DWORD(this));
m_prog.SetRange32(0,100); //设置进度条范围
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCommtestDlg::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();
}
}
HCURSOR CCommtestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
static char m_RxChar;
static char m_RecCom;
static char mReadHex;
static CString str;
static int counter(0);
static int NumC(0);
static float PerNum;
static float PerN(0);
if(port <= 0 || port > 4)
return -1;
m_RxChar = (char)ch;
m_RecCom = vHexDdecoder(m_RxChar); //命令分析
switch(m_RecCom) //命令执行
{
case HEX_DEC_SECTOR_BANK :
if(offset == 0xDA45)
{
m_Sector = 128;
m_DataLong = 128;
vCOmSend(2, 0x1D, 0x0080); //下载FLASH扇区数
}
else if(offset == 0x1FDA)
{
m_Sector = 256;
m_DataLong = 255;
vCOmSend(2, 0x1D, 0x0100); //下载FLASH扇区数
}
break;
case HEX_DEC_START :
if(m_ProcessType == 0xFF)vCOmSend(0, 0x03, 0); //下载开始
if(m_ProcessType == 0x00)vCOmSend(0, 0x10, 0); //比较开始
break;
case HEX_DEC_REDY : //就绪
m_Bank = 0;
NumC = 0;
PerN = 0;
m_CounterSector = 0x00000;
OnFileChange("H00");
OnOpenFile("H00");
m_prog.SetStep(m_setStep); //设置进度条步长
m_prog.SetPos(0); //设置进度条当前位置
vCOmSend(2, 0x02, m_Bank); //下载程序段数据
break;
case HEX_DEC_SEG_ERR : //段错误
vCOmSend(2, 0x02, m_Bank); //重复下载程序段数据
break;
case HEX_DEC_SEG_OK : //段正确
NumC++;
PerN += (float) m_Sector;
if(7<NumC)
{
NumC = 0;
PerNum = (float)(PerN/(4*65536.0));
PerNum = (float)(PerNum*100+0.5);
counter = (this->m_prog).GetPos();
str.Format("Download Process %3d%c", counter,'%');
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
(this->m_prog).SetPos((int)PerNum);
(this->m_prog).StepIt();
}
vDataDownLoad(m_CounterSector); //下载数据帧
break;
case HEX_DEC_FRAME_ERR : //帧错误
vDataDownLoad(m_CounterSector); //重复下载数据帧
break;
case HEX_DEC_FRAME_OK : //帧正确
vCOmSend(0, 0x0D, 0); //帧请求命令
break;
case HEX_DEC_FRAME_NEXT : //帧继续
NumC++;
PerN += (float) m_Sector;
if(7<NumC)
{
NumC = 0;
PerNum = (float)(PerN/(4*65536.0));
PerNum = (float)(PerNum*100+0.5);
counter = (this->m_prog).GetPos();
str.Format("Download Process %3d%c", counter,'%');
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
(this->m_prog).SetPos((int)PerNum);
(this->m_prog).StepIt();
}
m_CounterSector += m_Sector ; //调整帧计数
vDataDownLoad(m_CounterSector); //下载数据帧
break;
case HEX_DEC_PROGRAM_ERR :
FileErrNum++;
if(FileErrNum == 5)
{
FileErrNum = 0;
str.Format("Rom Err! Change Another Piece!");
}
else
{
str.Format("Error! Try Again!");
}
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
{
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
}
vCOmSend(0, 0x1B, 0); //发送错误结束命令
break;
case HEX_DEC_FRAME_END : //帧结束
vCOmSend(0, 0x0A, 0); //段请求命令
break;
case HEX_DEC_SEG_NEXT : //段继续
m_Bank++; //调整段计数
m_CounterSector = 0x0000;
if(m_Bank<4)
{
OnOpenFile(m_FileNameEx[m_Bank]);
}
vCOmSend(2, 0x02, m_Bank); //下载程序段地址
break;
case HEX_DEC_SEG_END : //段结束
m_CounterSector = 0;
m_Bank = 0;
FileErrNum = 0;
str.Format("Download OK!");
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
{
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
}
vCOmSend(0, 0x01, 0); //发送结束命令
break;
case HEX_DEC_VERIFY_REDY : //比较就绪
m_Bank = 0; //置段地址初值
ErrNum = 0;
NumC = 0;
PerN = 0;
(this->m_prog).SetPos(0);
(this->m_prog).StepIt();
m_CounterSector = 0x0000; //置帧初值
OnFileChange("H00");
OnOpenFile("H00");
vCOmSend(2, 0x12, m_Bank); //下载比较段地址
break;
case HEX_DEC_SEG_VERIFY_ERR : //段错误
vCOmSend(2, 0x12, m_Bank); //重复下载段数据
break;
case HEX_DEC_SEG_VERIFY_OK : //段正确
vCOmSend(2, 0x15, m_CounterSector); //下载比较帧地址
break;
case HEX_DEC_FRAME_VERIFY_DATA : //接收比较帧数据
//比较统计错误总数
ErrNum += vVerify(Filesbuf,hexbuff,offset,ulength);
NumC++;
PerN += (float) m_Sector;
if(7<NumC)
{
NumC = 0;
PerNum = (float)(PerN/(4*65536.0));
PerNum = (float)(PerNum*100+0.5);
counter = (this->m_prog).GetPos();
str.Format("Verify Processing %3d%c", counter,'%');
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
(this->m_prog).SetPos((int)PerNum);
(this->m_prog).StepIt();
}
vCOmSend(0, 0x16, 0); //比较帧请求
break;
case HEX_DEC_FRAME_VERIFY_ERR : //接收帧错误
vCOmSend(2, 0x15, m_CounterSector); //重复下载帧地址
break;
case HEX_DEC_VERIFY_FRAME_NEXT : //比较帧继续
m_CounterSector += m_Sector; //调整帧计数
vCOmSend(2, 0x15, m_CounterSector); //下一帧地址
break;
case HEX_DEC_VERIFY_FRAME_END : //比较帧结束
m_Bank++;
if(3<m_Bank) //比较数据结束
{
m_Bank = 0;
m_CounterSector = 0;
if(ErrNum == 0)
{
str.Format("256K Byte Verify ... OK");
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
}
else
{
str.Format("Verify Err %d Byte", ErrNum);
if(this->GetDlgItem(IDC_STATIC_SHOW)->GetSafeHwnd())
this->GetDlgItem(IDC_STATIC_SHOW)->SetWindowText(str);
}
vCOmSend(0, 0x19, 0); //发送比较结束命令
}
else //下载段地址
{
OnOpenFile(m_FileNameEx[m_Bank]);
m_CounterSector = 0x0000;
vCOmSend(2, 0x12, m_Bank); //下载下一段地址
}
break;
case HEX_DEC_OK : //接受结束
break;
case HEX_DEC_ERR : //错误
break;
default : break;
}
return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -