📄 usingapidlg.cpp
字号:
// UsingAPIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UsingAPI.h"
#include "UsingAPIDlg.h"
#include "5410api.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define buff_len 4096
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CUsingAPIDlg dialog
CUsingAPIDlg::CUsingAPIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUsingAPIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUsingAPIDlg)
m_string = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUsingAPIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUsingAPIDlg)
DDX_Control(pDX, IDC_OPEN, m_open);
DDX_Text(pDX, IDC_EDIT_WRITE, m_string);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUsingAPIDlg, CDialog)
//{{AFX_MSG_MAP(CUsingAPIDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_RESET_FIFO, OnResetFifo)
ON_BN_CLICKED(IDC_FIFO_STATUS, OnFifoStatus)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsingAPIDlg message handlers
BOOL CUsingAPIDlg::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
flagOpen=FALSE;
CAboutDlg AboutDlg;
AboutDlg.DoModal();
CWnd::SetDlgItemText(IDC_EDIT_DEVSTATUS,"设备状态:");
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 请先打开设备.");
return TRUE; // return TRUE unless you set the focus to a control
}
void CUsingAPIDlg::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 CUsingAPIDlg::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 CUsingAPIDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUsingAPIDlg::OnOpen()
{
// TODO: Add your control notification handler code here
if(flagOpen==FALSE)
{
hDevHandle=OpenUSB(); //句柄hDevHandle是类CUsingAPIDlg的一个public成员变量(类型HANDLE)
if(hDevHandle== INVALID_HANDLE_VALUE)
{
MessageBox("设备不能被打开!");
CWnd::SetDlgItemText(IDC_EDIT_DEVSTATUS,"设备状态: 未打开");
return;
}
else
{
flagOpen=TRUE;
CWnd::SetDlgItemText(IDC_EDIT_DEVSTATUS,"设备状态: 已打开");
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 可以进行写数据到FIFO或检查FIFO的状态等操作.");
}
}
else
MessageBox("设备状态: 已打开");
}
void CUsingAPIDlg::OnWrite()
{
// TODO: Add your control notification handler code here
LPBYTE outbuffer; //类型LPBYTE:指向BYTE的指针。
if(flagOpen==TRUE)
{
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 选择文件(如文本文件:*.txt,*.bin,*.dat等)写到USB...");
outbuffer=(LPBYTE)malloc(buff_len);
for(int j=0;j<buff_len;j++)
{
// *(outbuffer+j)=0x30+j; //for debug
*(outbuffer+j)=0;
}
CFileDialog dlg(TRUE, //construct a File Open dialog box
"*", //Specifies the default filename extension
"*.*", //Specifies the initial filename that appears in the filename edit box
OFN_HIDEREADONLY , //read only
"TXT Files (*.txt)|*.txt|Binary Files (*.bin)|*.bin\
|Data Files (*.dat;*.ini)|*.dat; *.ini|All Files (*.*)|*.*||",NULL);
dlg.m_ofn.lpstrTitle = "选择要打开的文件名";
dlg.m_ofn.nFilterIndex=1;
if (dlg.DoModal() != IDOK)
return;
// CByteArray arrByte;
TRY
{
CFile fileRead(dlg.GetPathName(), CFile::modeRead);
unsigned char pBuf[buff_len];
int nCount = 0;
while (nCount = fileRead.Read(pBuf, buff_len))
{
// arrByte.SetSize(nCount);
for (int i = 0; i < nCount; i++)
{
*(outbuffer+i) = pBuf[i];
}
}
}
CATCH (CFileException, e)
{
MessageBox("文件" + e->m_strFileName + "操作出错!");
e->Delete();
}
END_CATCH
bResult=USB_Write(hDevHandle, outbuffer, buff_len); //bResult是类CUsingAPIDlg的一个public成员变量(类型BOOL)
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 数据已写到FIFO,可读或检测fifo中的数据.");
}
else
{
MessageBox("请先打开USB设备!");
}
}
void CUsingAPIDlg::OnRead()
{
// TODO: Add your control notification handler code here
if(flagOpen==TRUE)
{
if(FIFO_Status(hDevHandle))
{
LPBYTE inbuffer;
char str[buff_len];
inbuffer=(LPBYTE)malloc(buff_len);
bResult=USB_Read(hDevHandle, inbuffer, buff_len); //bResult是类CUsingAPIDlg的一个public成员变量(类型BOOL)
for(int i=0;i<buff_len;i++)
{
str[i]=*(inbuffer+i);
}
m_string=str;
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 显示屏只能显示文本文件的数据.");
UpdateData(FALSE);
}
else
{
MessageBox("没有数据在FIFO中!");
}
}
else MessageBox("请先打开USB设备!");
}
void CUsingAPIDlg::OnReset()
{
// TODO: Add your control notification handler code here
if(flagOpen==TRUE)
{
Reset_DSP(hDevHandle);
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: DSP和FIFO已复位.");
}
else
{
MessageBox("请先打开USB设备!");
}
}
void CUsingAPIDlg::OnResetFifo()
{
// TODO: Add your control notification handler code here
if(flagOpen==TRUE)
{
Reset_FIFO(hDevHandle);
CWnd::SetDlgItemText(IDC_EDIT_HINT,"提示: 复位FIFO.");
}
else
{
MessageBox("请先打开USB设备!");
}
}
void CUsingAPIDlg::OnFifoStatus()
{
// TODO: Add your control notification handler code here
int DataNum;
char str[buff_len];
if(flagOpen==TRUE)
{
DataNum=FIFO_Status(hDevHandle);
if(DataNum!=0)
sprintf(str,"FIFO中有数据.");
else
sprintf(str,"FIFO中无数据.");
MessageBox(str);
}
else
{
MessageBox("请先打开USB设备!");
}
}
void CUsingAPIDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_string="";
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -