📄 usbdlg.cpp
字号:
// usbDlg.cpp : implementation file
//
#include "stdafx.h"
#include "usb.h"
#include "usbDlg.h"
#include <string.h>
#include "usb_diag_lib.h"
#include "usb_lib.h"
static char line [4096];
char temp[256];
CUsbDlg *pDlg;
#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()
/////////////////////////////////////////////////////////////////////////////
// CUsbDlg dialog
CUsbDlg::CUsbDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUsbDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUsbDlg)
m_Message = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_Message = "USB Device Experiment\x0D\x0A";
m_Message += "CSE BUAA\x0D\x0A\x0D\x0A";
}
void CUsbDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUsbDlg)
DDX_Control(pDX, IDC_EDIT2, m_Input);
DDX_Control(pDX, IDC_STOP, m_STOP);
DDX_Control(pDX, IDC_SET, m_SET);
DDX_Control(pDX, IDC_SCAN, m_SCAN);
DDX_Control(pDX, IDC_LISTEN, m_LISTEN);
DDX_Control(pDX, IDC_DISPPIPE, m_DISPPIPE);
DDX_Control(pDX, IDC_DISPCONF, m_DISPCONF);
DDX_Control(pDX, IDC_EDIT1, m_Mess);
DDX_Control(pDX, IDC_CLOSE, m_Close);
DDX_Control(pDX, IDC_OPEN, m_Open);
DDX_Text(pDX, IDC_EDIT1, m_Message);
DDV_MaxChars(pDX, m_Message, 65536);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUsbDlg, CDialog)
//{{AFX_MSG_MAP(CUsbDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_BN_CLICKED(IDC_SCAN, OnScan)
ON_BN_CLICKED(IDC_DISPCONF, OnDispconf)
ON_BN_CLICKED(IDC_DISPPIPE, OnDisppipe)
ON_BN_CLICKED(IDC_LISTEN, OnListen)
ON_BN_CLICKED(IDC_SET, OnSet)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsbDlg message handlers
BOOL CUsbDlg::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
m_Input.SetLimitText(2);
m_Input.SetWindowText("00");
hUSB = NULL;
m_Message += "USB diagnostic utility.\x0D\x0A";
m_Message += "Application accesses hardware using WinDriver.\x0D\x0A\x0D\x0A";
Display();
if (!USB_Get_WD_handle(&hWD))
return FALSE;
WD_Close (hWD);
sprintf (temp, "Opening the device with VendorID %x, ProductID %x ...\x0D\x0A",
USB_DEFAULT_VENDOR_ID, USB_DEFAULT_PRODUCT_ID);
m_Message += temp;
Display();
hUSB = USB_LocateAndOpenDevice(TRUE);
pDlg = (CUsbDlg *) AfxGetMainWnd();//GetDlgItem(IDD_USB_DIALOG);
return TRUE; // return TRUE unless you set the focus to a control
}
void CUsbDlg::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 CUsbDlg::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 CUsbDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
CUsbDlg::~CUsbDlg()
{
if (hUSB)
USB_Close(hUSB);
}
USB_HANDLE CUsbDlg::USB_LocateAndOpenDevice(BOOL fUseDefault)
{
USB_HANDLE hUSB;
DWORD uniqueId = 0, configNum = 0, dwInterface = 0, dwAlternate = 0;
WD_USB_SCAN_DEVICES usbScan;
WD_USB_CONFIGURATION config;
HANDLE hWD;
DWORD scanIndex;
USB_RegisterWinDriver();
if (!USB_Get_WD_handle(&hWD))
return NULL;
BZERO(usbScan);
if (fUseDefault)
{
usbScan.searchId.dwVendorId = USB_DEFAULT_VENDOR_ID;
usbScan.searchId.dwProductId = USB_DEFAULT_PRODUCT_ID;
}
WD_UsbScanDevice(hWD, &usbScan);
if (!usbScan.dwDevices)
{
m_Message += "No deviced found.\x0D\x0A\x0D\x0A";
Display();
// strcpy(temp,"No device found.\x0D\x0A\x0D\x0A");
// for(unsigned char i=0;i<strlen(temp);i++)
// SendMessage(WM_KEYDOWN,temp[i]);
WD_Close(hWD);
// m_Close.EnableWindow(FALSE);
// m_DISPPIPE.EnableWindow(FALSE);
// m_LISTEN.EnableWindow(FALSE);
// m_SET.EnableWindow(FALSE);
// GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
// return NULL;
}
else
{
m_Open.EnableWindow(FALSE);
m_Close.EnableWindow();
m_DISPPIPE.EnableWindow();
m_LISTEN.EnableWindow();
m_SET.EnableWindow();
GetDlgItem(IDC_EDIT2)->EnableWindow();
m_Message += "Device Opened.\x0D\x0A\x0D\x0A";
Display();
}
if (usbScan.dwDevices>1 && !fUseDefault)
{
// printf ("Please enter the unique ID of the device: ");
// fgets(line, sizeof(line), stdin);
// sscanf (line, "%d", &uniqueId);
if (!uniqueId)
return NULL;
for (scanIndex=0; scanIndex<usbScan.dwDevices; scanIndex++)
{
if (usbScan.uniqueId[scanIndex]==uniqueId)
break;
}
if (scanIndex==usbScan.dwDevices)
{
m_Message += "Unique ID not found\x0D\x0A";
WD_Close(hWD);
return NULL;
}
}
else
{
uniqueId = usbScan.uniqueId[0];
scanIndex = 0;
}
if (fUseDefault)
{
configNum = USB_DEFAULT_CONFIG;
dwInterface = USB_DEFAULT_INTERFACE;
dwAlternate = USB_DEFAULT_ALTERNATE;
}
else
{
configNum = 0;
if (usbScan.deviceGeneralInfo[scanIndex].dwConfigurationsNum>1)
{
// printf ("Please enter the index of the configuration: (0-%d)"),
// usbScan.deviceGeneralInfo[scanIndex].dwConfigurationsNum - 1;
// fgets(line, sizeof(line), stdin);
// sscanf (line, "%d", &configNum);
}
BZERO(config);
config.uniqueId = uniqueId;
config.dwConfigurationIndex = configNum;
WD_UsbGetConfiguration(hWD, &config);
if (config.dwInterfaceAlternatives==0)
{
m_Message += "No interfaces found\n";
WD_Close(hWD);
return NULL;
}
if (config.dwInterfaceAlternatives==1)
{
dwInterface = config.Interface[0].Interface.dwNumber;
dwAlternate = config.Interface[0].Interface.dwAlternateSetting;
}
else
{
printf ("Select 'Display Device Configurations' in the main menu to learn\n");
printf ("about the interfaces of the selected device\n");
printf ("Please enter interface number: ");
fgets(line, sizeof(line), stdin);
sscanf (line, "%d", &dwInterface);
printf ("Please enter interface alternate: ");
fgets(line, sizeof(line), stdin);
sscanf (line, "%d", &dwAlternate);
}
}
hUSB = USB_Open (uniqueId, configNum, dwInterface, dwAlternate);
if (!hUSB)
{
printf ("%s\n", USB_ErrorString);
WD_Close(hWD);
return NULL;
}
WD_Close(hWD);
return hUSB;
}
void CUsbDlg::OnOpen()
{
// TODO: Add your control notification handler code here
if (!hUSB)
hUSB = USB_LocateAndOpenDevice(TRUE);
}
void CUsbDlg::OnClose()
{
// TODO: Add your control notification handler code here
if (hUSB)
{
USB_Close(hUSB);
m_Close.EnableWindow(FALSE);
m_Open.EnableWindow();
m_DISPPIPE.EnableWindow(FALSE);
m_LISTEN.EnableWindow(FALSE);
m_SET.EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}
hUSB = NULL;
m_Message += "Device Closed.\x0D\x0A\x0D\x0A";
Display();
}
void CUsbDlg::OnScan()
{
// TODO: Add your control notification handler code here
USB_Print_device_info(0,0);
}
void CUsbDlg::OnDispconf()
{
// TODO: Add your control notification handler code here
USB_Print_device_Configurations();
}
void CUsbDlg::OnDisppipe()
{
// TODO: Add your control notification handler code here
WD_USB_DEVICE_INFO devInfo;
USB_GetDeviceInfo(hUSB, &devInfo);
sprintf (temp, "pipe00 - type: Control, direction: in & out, Max packet Size: %d, interval: %d\x0D\x0A",
devInfo.Pipe[0].dwMaximumPacketSize, devInfo.Pipe[0].dwInterval);
m_Message += temp;
sprintf (temp, "pipe81 - type: Interrupt, direction: in, Max packet Size: %d, interval: %d\x0D\x0A\x0D\x0A",
devInfo.Pipe[1].dwMaximumPacketSize, devInfo.Pipe[1].dwInterval);
m_Message += temp;
Display();
}
void CUsbDlg::OnListen()
{
// TODO: Add your control notification handler code here
// USB_LISTEN_PIPE listenPipe;
WD_USB_DEVICE_INFO devInfo;
// int c;
BZERO(listenPipe);
BZERO(devInfo);
listenPipe.read_pipe_func = (unsigned long (__cdecl *)(void *,void *,unsigned long)) USB_ReadPipe81;
listenPipe.stop_pipe_func = (void (__cdecl *)(void *)) USB_StopTransferPipe81;
listenPipe.hDevice = hUSB;
USB_GetDeviceInfo(hUSB, &devInfo);
listenPipe.dwPacketSize = devInfo.Pipe[1].dwMaximumPacketSize;
// printf ("Press <Enter> to start listening. While listening, press <Enter> to stop\n\n");
// getchar();
m_Message += "Start listening to pipe\x0D\x0A";
Display();
ListenToPipe(&listenPipe);
// while ((c=getchar()) != 10) {} // ESC code
// CloseListening(&listenPipe);
m_Close.EnableWindow(FALSE);
m_LISTEN.ShowWindow(SW_HIDE);
m_STOP.ShowWindow(SW_SHOW);
KillTimer(1);
SetTimer(1, 200, NULL);
}
void CUsbDlg::OnSet()
{
// TODO: Add your control notification handler code here
// UpdateData();
unsigned char SetupPacket[] = "\x20\x09\x00\x01\x00\x00\x12\x00";
char tmp[3] = {'0', '0', 0};
m_Input.GetWindowText(tmp, 3);
if (tmp[1]=='\0')
{
tmp[1] = tmp[0];
tmp[0] = '0';
}
if (!((tmp[0]>='0' && tmp[0]<='9' || tmp[0]>='A' && tmp[0]<='F' || tmp[0]>='a' && tmp[0]<='f')
&& (tmp[1]>='0' && tmp[1]<='9' || tmp[1]>='A' && tmp[1]<='F' || tmp[1]>='a' && tmp[1]<='f')))
{
MessageBox("输入数据无效!");
return;
}
BYTE a;
sscanf(tmp, "%x", &a);
a--;
DWORD dwBytesTransferred = USB_WritePipe00(hUSB, /*pBuffer*/&a, /*dwSize*/1, SetupPacket);
/*
if (dwBytesTransferred==0xffffffff)
m_Message += "error on transfer\x0D\x0A";
else
{
sprintf(temp, "transferred %d bytes\n", dwBytesTransferred);
m_Message += temp;
}
*/
Display();
}
void CUsbDlg::OnStop()
{
// TODO: Add your control notification handler code here
KillTimer(1);
CloseListening(&listenPipe);
UpdateData();
m_STOP.ShowWindow(SW_HIDE);
m_LISTEN.ShowWindow(SW_SHOW);
m_Close.EnableWindow();
}
void CUsbDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent != 1)
return;
Display();
// int Min, Max;
// m_Mess.GetScrollRange(1, &Min, &Max);
// m_Mess.SetScrollPos(1, GetScrollLimit(1));
CDialog::OnTimer(nIDEvent);
}
void CUsbDlg::Display()
{
UpdateData(FALSE);
m_Mess.LineScroll(m_Mess.GetLineCount());
}
void CUsbDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_Input.SetWindowText("00");
m_Message.Empty();
Display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -