📄 e2prom_1581dlg.cpp
字号:
// E2PROM_1581Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "E2PROM_1581.h"
#include "E2PROM_1581Dlg.h"
#include "UsbDeviceCommand.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()
/////////////////////////////////////////////////////////////////////////////
// CE2PROM_1581Dlg dialog
CE2PROM_1581Dlg::CE2PROM_1581Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CE2PROM_1581Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CE2PROM_1581Dlg)
m_strReadData = _T("");
m_strWriteData = _T("");
m_nEditReadLength = 1;
m_nEditWritestartAddr = 0;
m_nEditReadstartAddr = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CE2PROM_1581Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CE2PROM_1581Dlg)
DDX_Text(pDX, IDC_EDIT_READDATA, m_strReadData);
DDX_Text(pDX, IDC_EDIT_WRITEDATA, m_strWriteData);
DDX_Text(pDX, IDC_EDIT_READLENGTH, m_nEditReadLength);
DDX_Text(pDX, IDC_EDIT_WRITESTARTADDR, m_nEditWritestartAddr);
DDX_Text(pDX, IDC_EDIT_READSTARTADDR, m_nEditReadstartAddr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CE2PROM_1581Dlg, CDialog)
//{{AFX_MSG_MAP(CE2PROM_1581Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CLEARREAD, OnButtonClearread)
ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
ON_BN_CLICKED(IDC_BUTTON_CLEARWRITE, OnButtonClearwrite)
ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
ON_EN_CHANGE(IDC_EDIT_READLENGTH, OnChangeEditReadlength)
ON_EN_CHANGE(IDC_EDIT_READSTARTADDR, OnChangeEditReadstartaddr)
ON_EN_CHANGE(IDC_EDIT_WRITESTARTADDR, OnChangeEditWritestartaddr)
//}}AFX_MSG_MAP
ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CE2PROM_1581Dlg message handlers
BOOL CE2PROM_1581Dlg::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
CheckBoard();
return TRUE; // return TRUE unless you set the focus to a control
}
void CE2PROM_1581Dlg::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 CE2PROM_1581Dlg::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 CE2PROM_1581Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CE2PROM_1581Dlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CE2PROM_1581Dlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CE2PROM_1581Dlg::OnButtonClearread()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_strReadData="";
UpdateData(false);
}
void CE2PROM_1581Dlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
OnButtonClearread();
UpdateData(true);
if(m_nEditReadLength+m_nEditReadstartAddr>256)
{
MessageBox("所要读取的数据超过E2PROM地址区间,溢出。","错误",MB_OK|MB_ICONSTOP);
return;
}
m_readparam.length=m_nEditReadLength;
m_readparam.startaddr=m_nEditReadstartAddr;
UpdateData(false);
m_readparam.testDlg=this;
EnableButton(false,false,false,false);
CWinThread * wt = AfxBeginThread(
BulkReadPipe, // thread function
&m_readparam); // argument to thread function
}
void CE2PROM_1581Dlg::OnButtonClearwrite()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_strWriteData="";
UpdateData(false);
}
void CE2PROM_1581Dlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
char tmp[200];
int len;
UpdateData(true);
len=m_strWriteData.GetLength();
if(len>192)
{
MessageBox("所写入的数据过长,每次最多只能写入64个字节的数据。","警告",MB_OK|MB_ICONWARNING);
return;
}
if(len==0)
{
MessageBox("请输入所要写入的数据。","警告",MB_OK|MB_ICONWARNING);
return;
}
if(len%3==1)
{
MessageBox("您所输入的数据格式不对,请检查是否漏掉了空格或有多余的空格或数据的位数不对。","错误",MB_OK|MB_ICONSTOP);
return;
}
len=len/3+!(!(len%3));
strcpy(tmp,(LPCTSTR)m_strWriteData);
if(strtodata((unsigned char*)tmp,m_writeparam.buffer,len)==1)
{
MessageBox("您所输入的数据格式不对,其中有可能有些不是16进制数。","错误",MB_OK|MB_ICONSTOP);
return;
}
if(len+m_nEditWritestartAddr>256)
{
MessageBox("所要写入的数据超过E2PROM地址区间,溢出。","错误",MB_OK|MB_ICONSTOP);
return;
}
m_writeparam.length=len;
m_writeparam.startaddr=m_nEditWritestartAddr;
UpdateData(false);
m_writeparam.testDlg=this;
EnableButton(false,false,false,false);
CWinThread * wt = AfxBeginThread(
BulkWritePipe, // thread function
&m_writeparam); // argument to thread function
}
void CE2PROM_1581Dlg::EnableButton(bool clearread, bool read, bool clearwrirte, bool write)
{
GetDlgItem(IDC_BUTTON_CLEARREAD)->EnableWindow(clearread);
GetDlgItem(IDC_BUTTON_READ)->EnableWindow(read);
GetDlgItem(IDC_BUTTON_CLEARWRITE)->EnableWindow(clearwrirte);
GetDlgItem(IDC_BUTTON_WRITE)->EnableWindow(write);
}
BOOL CE2PROM_1581Dlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
Sleep(200);
CheckBoard();
return 0;
}
int CE2PROM_1581Dlg::CheckBoard()
{
m_ControlIn.testDlg=this;
ControlIn((void*)&m_ControlIn);
return 0;
}
//-----------------------------------------------------
//参数:
//str:要转换的字符串
//data:储存转换过来的数据串
//len:数据长度
//函数功能:字符串转换为数据串
//-----------------------------------------------------
int CE2PROM_1581Dlg::strtodata(unsigned char *str, unsigned char *data,int len,int flag)
{
unsigned char cTmp=0;
int i=0;
for(int j=0;j<len;j++)
{
if(chartoint(str[i++],&cTmp))
return 1;
data[j]=cTmp;
if(chartoint(str[i++],&cTmp))
return 1;
data[j]=(data[j]<<4)+cTmp;
if(flag==1)
i++;
}
return 0;
}
//-----------------------------------------------------
//参数:
//chr:要转换的字符
//cint:储存转换过来的数据
//函数功能:字符转换为数据
//-----------------------------------------------------
int CE2PROM_1581Dlg::chartoint(unsigned char chr, unsigned char *cint)
{
unsigned char cTmp;
cTmp=chr-48;
if(cTmp>=0&&cTmp<=9)
{
*cint=cTmp;
return 0;
}
cTmp=chr-65;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
cTmp=chr-97;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
return 1;
}
void CE2PROM_1581Dlg::OnChangeEditReadlength()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_nEditReadLength<1)
m_nEditReadLength=1;
else if(m_nEditReadLength>64)
m_nEditReadLength=64;
UpdateData(false);
}
void CE2PROM_1581Dlg::OnChangeEditReadstartaddr()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_nEditReadstartAddr<0)
m_nEditReadstartAddr=0;
else if(m_nEditReadstartAddr>255)
m_nEditReadstartAddr=255;
UpdateData(false);
}
void CE2PROM_1581Dlg::OnChangeEditWritestartaddr()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_nEditWritestartAddr<0)
m_nEditWritestartAddr=0;
else if(m_nEditWritestartAddr>255)
m_nEditWritestartAddr=255;
UpdateData(false);
}
void CE2PROM_1581Dlg::UpdateReadData()
{
CString olddata,strreaddata="",strtmp;
for(int i=0;i<m_readparam.length;i++)
{
if(m_readparam.buffer[i]<16)
strtmp.Format("0%x",m_readparam.buffer[i]);
else
strtmp.Format("%x",m_readparam.buffer[i]);
strreaddata+=strtmp;
strreaddata+=" ";
}
GetDlgItem(IDC_EDIT_READDATA)->GetWindowText(olddata);
GetDlgItem(IDC_EDIT_READDATA)->SetWindowText(olddata+strreaddata);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -