USB驱动源代码
作者: UserVC
// usblistenDlg.cpp : implementation file //
#include "stdafx.h"
#include "usblisten.h"
#include "usblistenDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CUsblistenDlg dialog
CUsblistenDlg::CUsblistenDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUsblistenDlg::IDD, pParent) {
//{{AFX_DATA_INIT(CUsblistenDlg) // NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon =
AfxGetApp()->LoadIcon(IDR_MAINFRAME); }
void
CUsblistenDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUsblistenDlg) // NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUsblistenDlg, CDialog) //{{AFX_MSG_MAP(CUsblistenDlg) ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend) ON_WM_TIMER()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CUsblistenDlg message handlers BOOL CUsblistenDlg::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);
SetTimer(1,3000,NULL);
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
return TRUE; // return TRUE unless you set the focus to a control
}
void CUsblistenDlg::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 CUsblistenDlg::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 CUsblistenDlg::OnQueryDragIcon() {
return (HCURSOR) m_hIcon;
}
//-----------------Initilize USB port---------
unsigned int VendorID,ProductID; char temp_buffer[100];
unsigned char DataBuffer[1],BufferLength; //接收
unsigned char
SendDataBuffer[1],SendBufferLength; //发送
int HIDCounter=0;
GUID hidGuid;
HDEVINFO AllHIDDeviceInfo=NULL;
HANDLE HIDDevice=NULL,ReadHIDDevice=NULL; HIDD_ATTRIBUTES Attributes;
PSP_DEVICE_INTERFACE_DETAIL_DATA ClassDeviceData;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG neededLength,requiredLength;
DWORD bytesRead;
bool InitUSB()
{
VendorID = 0x0000;
ProductID = 0x0000;
// 查找设备
// 本例程使用HID设备的API,它查找HID设备列
表,找出与Vendor ID 和 Product ID匹配的设备
ClassDeviceData =NULL;
HIDDevice =NULL;
deviceInfoData.cbSize
=sizeof(deviceInfoData); // 从操作系统获取HIDs 的GUID
HidD_GetHidGuid(&hidGuid);
// 获取所有HIDs的设备信息
AllHIDDeviceInfo=SetupDiGetClassDevs(&hidGuid,NULL,NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
HIDCounter=0;
while (TRUE)
{
// 这个API将发现的设备信息写入
deviceInfoData
// HIDCounter 允许这个API重复调用所有HID设备
// 如果API调用返回0,没有更多的HID设备发现
if
(!SetupDiEnumDeviceInterfaces(AllHIDDeviceInfo,0,&hidGuid,
HIDCounter,&deviceInfoData))
{
// 没有发现与Vendor ID 和 Product ID匹配的HID设备
SetupDiDestroyDeviceInfoList(AllHIDDeviceIn
fo);
return FALSE;
}
else
{
// 发现一个HID设备,获取设备的详细信息
// 第一次调用
SetupDiGetDeviceInterfaceDetail得到
ClassDeviceData
// 的大小,但返回错误
SetupDiGetDeviceInterfaceDetail(AllHIDDevic
eInfo,&deviceInfoData, NULL,0,&requiredLength,NULL);
neededLength =requiredLength;
ClassDeviceData
=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(neededLength);
ClassDeviceData->cbSize
=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //第二次调用SetupDiGetDeviceInterfaceDetail // 使用 合适的neededLength.
if
(!SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData,
ClassDeviceData,neededLength,&requiredLength,NULL))
{
free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
return FALSE;
}
// 建立HID设备的句柄
HIDDevice=CreateFile(ClassDeviceData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL);
// 获取 attributes 以便得到Vendor ID 和
Product ID
HidD_GetAttributes(HIDDevice,&Attributes);
if ((Attributes.VendorID == VendorID) && (Attributes.ProductID == ProductID)) {
// 找到了匹配的Vendor ID 和 Product ID的HID
设备
// 建立ReadHIDDevice设备的句柄以便读取信息
ReadHIDDevice=CreateFile(ClassDeviceData->D
evicePath,
GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL); free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceIn
fo);
//初始化成功
return TRUE;
}
else
{
CloseHandle(HIDDevice); }
free(ClassDeviceData);
HIDCounter = HIDCounter+1; }
}
}
//Read from usb prot,that is listen to usb port ,if it has character,system would display it on user interface
bool ReadUSB()
{
BufferLength=1;
memset(DataBuffer,0,1);
if(!ReadFile(ReadHIDDevice,DataBuffer,BufferLength,&bytesRead,NULL)) //失败
return false; //读数失败立即返回
else
return true;
}
//Wirte a character or a string to usb port bool WriteUSB()
{
CString str;
SendBufferLength=1;
SendDataBuffer[0]=65; str=SendDataBuffer;
if
(!WriteFile(ReadHIDDevice,SendDataBuffer,Se
ndBufferLength,&bytesRead,NULL))
{
return false;
//失败
}
else
{
AfxMessageBox("Success!"); return true;
//成功
}
}
void CUsblistenDlg::OnCancel() {
int button;
button=AfxMessageBox("Do you want to
exit?",MB_YESNO+MB_ICONWARNING); if(button==IDYES)
{
KillTimer(1);
EndDialog(IDYES);
}
}
//Thread callback function
void CUsblistenDlg::OnSend()
{
// TODO: Add your control notification handler code here
SendDataBuffer[0]='C';
WriteUSB();
}
void CUsblistenDlg::OnTimer(UINT nIDEvent) {
// TODO: Add your message handler code here and/or call default
//CString str;
//ReadUSB();
//str=DataBuffer;
//if(DataBuffer[0]!=0)
//{
// CListBox
*listbox=(CListBox*)GetDlgItem(IDC_LIST1);
// listbox->AddString(str);
//}
//else
//{
// CListBox
*listbox=(CListBox*)GetDlgItem(IDC_LIST1); // listbox->AddString("No Charcters is sent to usb port");
//}
WriteUSB();
CDialog::OnTimer(nIDEvent);
}
void CUsblistenDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
HBRUSH CUsblistenDlg::OnCtlColor(CDC* pDC,
CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
UINT uID=pWnd->GetDlgCtrlID(); switch(uID)
{
case IDC_LIST1:
pDC->SetTextColor(RGB(255,255,0)); pDC->SetBkColor(RGB(255,0,0)); break;
}
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
本文档为【USB驱动源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。