📄 usbappdlg.cpp
字号:
// UsbAppDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UsbApp.h"
#include "UsbAppDlg.h"
#include "ezusbsys.h"
#include "devioctl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BULK_TRANSFER_CONTROL btc;
ULONG nBytes;
BOOLEAN bResult;
HANDLE hDevice;
CString sDriverName;
int n;
unsigned char RcvData[300]; //接收数据存储区
static int SavePointer=0; //数据存储指针
unsigned char TxData[300]; //发送数据存储区
/////////////////////////////////////////////////////////////////////////////
// CUsbAppDlg dialog
CUsbAppDlg::CUsbAppDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUsbAppDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUsbAppDlg)
m_ReadCode = _T("");
m_WriteCode = _T("");
m_ReadAddr = 0;
m_WriteAddr = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUsbAppDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUsbAppDlg)
DDX_Control(pDX, IDC_COMBO1, m_DriveName);
DDX_Text(pDX, IDC_READCODET, m_ReadCode);
DDX_Text(pDX, IDC_WRITECODET, m_WriteCode);
DDX_Text(pDX, IDC_READADDR, m_ReadAddr);
DDV_MinMaxInt(pDX, m_ReadAddr, 0, 1024);
DDX_Text(pDX, IDC_WRITEADDR, m_WriteAddr);
DDV_MinMaxInt(pDX, m_WriteAddr, 0, 1024);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUsbAppDlg, CDialog)
//{{AFX_MSG_MAP(CUsbAppDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_READCODE, OnReadcode)
ON_BN_CLICKED(IDC_WRITECODE, OnWritecode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsbAppDlg message handlers
BOOL CUsbAppDlg::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
// TODO: Add extra initialization here
CComboBox* pCBox = GetCComboBox(); // inline defined in BulkXFERDlg.h
// get the driver name from the combobox
n = pCBox->GetLBTextLen( pCBox->GetCurSel() );
pCBox->GetLBText( pCBox->GetCurSel(), sDriverName.GetBuffer(n));
sDriverName.ReleaseBuffer();
if (OpenDriver(&hDevice,sDriverName))
AfxMessageBox("设备已打开!");
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CUsbAppDlg::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 CUsbAppDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUsbAppDlg::OnClear()
{
// TODO: Add your control notification handler code here
}
void CUsbAppDlg::OnReset()
{
// TODO: Add your control notification handler code here
/* UpdateData(TRUE);
int i;
unsigned char t[20],z[20];
if(m_Code.GetLength()==4)
{
for(i=0;i<m_Code.GetLength();i++)
{
t[i]=m_Code.GetAt(i);
{if (48<=t[i]&&57>=t[i])
t[i]=t[i]-48;
else if (97<=t[i]&&102>=t[i])
t[i]-=87;
else if (65<=t[i]&&70>=t[i])
t[i]-=55;
else
goto ps;
}
}
for(i=0;i<m_Code.GetLength();i++)
{ z[i]=(t[i]<<4)+t[i+1];
i++;
}
for(i=0;i<m_Code.GetLength()/2;i++)
*(TxData+i)=z[2*i];
CByteArray array;
char sOutput[10];
array.RemoveAll(); //清空数组
array.SetSize(4); //设置数组大小为帧长度
for(i=0; i<4; i++) //把待发送数据存入数组
array.SetAt(i,TxData[i]);
if (!m_ctrlComm.GetPortOpen()) // 打开串口并发送数据
m_ctrlComm.SetPortOpen(TRUE);
m_ctrlComm.SetOutput(COleVariant(array));
m_TxData=""; // 在对话框中显示发送出去的数据
for(i=0; i<2; i++)
{ sprintf(sOutput,"%02X ",TxData[i]);
m_TxData+=sOutput;
}
UpdateData(FALSE); //更新对话框
AfxMessageBox("码字已送出!");
}
else
{ps: AfxMessageBox("请正确输入码字!");
m_Code="";
UpdateData(FALSE); //更新对话框
} */
}
void CUsbAppDlg::OnReadcode()
{
char sOutput[10];
int i,n;
// update member variables from text box controls
UpdateData(TRUE);
TxData[0]=0x3F;
TxData[1]=unsigned char(m_ReadAddr>>8);
TxData[2]=unsigned char(m_ReadAddr&0xFF);
// read data IN from EZUSB device
// 读的是从 0 到 127 共 128 个数据,顺序存放在 client.dat 文件中
// 因为一次只能读 64 个,所以读了两次
if (OpenDriver(&hDevice,sDriverName))
{
btc.pipeNum = 1;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_WRITE,
&btc,
sizeof (BULK_TRANSFER_CONTROL),
TxData,
8,
(unsigned long *)&nBytes,
NULL);
if (!bResult)
AfxMessageBox("Correct Pipe not found. Perhaps \"USB.hex\" was not downloaded to a development board.", MB_OK|MB_ICONSTOP);
else
AfxMessageBox("读出码字请求已发出!");
btc.pipeNum = 0;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_READ,
&btc,
sizeof (BULK_TRANSFER_CONTROL),
RcvData,
8,
(unsigned long *)&nBytes,
NULL);
if (!bResult)
AfxMessageBox("Correct Pipe not found. Perhaps \"USB.hex\" was not downloaded to a development board.", MB_OK|MB_ICONSTOP);
else
{
m_ReadCode="";
for(i=0; i<2; i++)
{ sprintf(sOutput,"%02X",RcvData[i]);
m_ReadCode+=sOutput;
}
UpdateData(FALSE);
AfxMessageBox("码字已读出!");
}
}
}
void CUsbAppDlg::OnWritecode()
{ // TODO: Add your control notification handler code here
// update member variables from text box controls
UpdateData(TRUE);
//*************************************/
/* if(m_WriteAddr.GetLength()>4)
goto ps;
else
{ for(i=0;i<m_WriteAddr.GetLength();i++)
{ Addrt[i]=m_WriteAddr.GetAt(i);
if (48<=t[i]&&57>=t[i])
Addrt[i]=Addrt[i]-48;
else
goto ps;
}
for(i=0;i<m_WriteAddr.GetLength();i++)
{ Addrz[i]=(Addrt[i]<<4)+Addrt[i+1];
i++;
}
}*/
TxData[0]=unsigned char(m_WriteAddr>>8);
TxData[1]=unsigned char(m_WriteAddr&0xFF);
unsigned char t[20],z[20];
if(m_WriteCode.GetLength()==4)
{
for(int i=0;i<m_WriteCode.GetLength();i++)
{
t[i]=m_WriteCode.GetAt(i);
{if (48<=t[i]&&57>=t[i])
t[i]=t[i]-48;
else if (97<=t[i]&&102>=t[i])
t[i]-=87;
else if (65<=t[i]&&70>=t[i])
t[i]-=55;
else
goto ps;
}
}
for(i=0;i<m_WriteCode.GetLength();i++)
{ z[i]=(t[i]<<4)+t[i+1];
i++;
}
for(i=0;i<m_WriteCode.GetLength()/2;i++)
*(TxData+i+2)=z[2*i];
// if (OpenDriver(&hDevice,sDriverName))
// {
// we use pipes 1 and 0 because that's what "ep-pair.hex" sets up
btc.pipeNum = 1;
// write data OUT to EZUSB device
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_WRITE,
&btc,
sizeof (BULK_TRANSFER_CONTROL),
TxData,
8,
(unsigned long *)&nBytes,
NULL);
if (!bResult)
AfxMessageBox("Correct Pipe1 not found. Perhaps \"USB.hex\" was not downloaded to a development board.", MB_OK|MB_ICONSTOP);
else
{
// no rabbits up our sleeve
for (i = 0; i < 8; i++)
TxData[i] = 0;
}
ps: AfxMessageBox("请正确输入码字!");
m_WriteCode="";
UpdateData(FALSE); //更新对话框
}
}
void CUsbAppDlg::SetupDriverComboBox()
{
CString sDriverName, sCompleteName;
char num[3] = {0,0,0};
//HANDLE hDriver;
CComboBox* pCBox = GetCComboBox(); // inline defined in BulkXFERDlg.h
pCBox->ResetContent ();
// Assume 32 EZ-USB boards are attached to the PC.
// Try to open each driver. If successful, add driver name to combobox
for (int i = 0; i < MAX_USB_DEV_NUMBER; i++)
{
_itoa( i, num, 10 );
sDriverName = (CString)"Ezusb-" + num;
if (OpenDriver(&hDriver, sDriverName))
pCBox->AddString ((LPCTSTR) sDriverName);
else
{
if (i==0)
{
AfxMessageBox("No EZ-USB device drivers were found. Perhaps no device is connected", MB_OK|MB_ICONSTOP);
exit(0);
}
}
}
pCBox->SetCurSel(0);
}
BOOLEAN CUsbAppDlg::OpenDriver(HANDLE *phDriver, CString devname)
{
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(security);
security.lpSecurityDescriptor = NULL;
security.bInheritHandle = false;
CString completeDeviceName = (CString)"\\\\.\\" + devname;
*phDriver = CreateFile( completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
&security,
OPEN_EXISTING,
0,
NULL);
return (*phDriver != INVALID_HANDLE_VALUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -