• PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 44KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 32
    下载次数
  • 2008-11-04 11:34
    上传日期
修正的USB读取端口程序 能正确读出USB设备的相关信息
USBPort.rar
  • USBPortDlg.cpp
    9.3KB
  • StdAfx.h
    999B
  • USBPort.cpp
    2KB
  • USBPort.h
    1.3KB
  • USBPort.dsw
    537B
  • USBPort.clw
    1.6KB
  • USBPortDlg.h
    1.6KB
  • www.pudn.com.txt
    218B
  • USBPort.bbs
    67B
  • USBPort.aps
    35.7KB
  • USBPort.ncb
    121KB
  • USBPort.rc
    6.5KB
  • USBPort.opt
    54.5KB
  • StdAfx.cpp
    209B
  • Resource.h
    1.1KB
  • USBPort.plg
    1.2KB
  • USBPort.dsp
    4.2KB
内容介绍
// USBPortDlg.cpp : implementation file // #include "stdafx.h" #include "USBPort.h" #include "USBPortDlg.h" #include <initguid.h> #include <regstr.h> #include <winioctl.h> extern "C" { #include "setupapi.h" #include "hidsdi.h" } #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif DEFINE_GUID(GUID_DEVINTERFACE_DISK, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CUSBPortDlg dialog CUSBPortDlg::CUSBPortDlg(CWnd* pParent /*=NULL*/) : CDialog(CUSBPortDlg::IDD, pParent) { //{{AFX_DATA_INIT(CUSBPortDlg) // 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 CUSBPortDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CUSBPortDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CUSBPortDlg, CDialog) //{{AFX_MSG_MAP(CUSBPortDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_FIND, OnSearch) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CUSBPortDlg message handlers BOOL CUSBPortDlg::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 GetDlgItem(IDC_FIND)->SetFocus(); return FALSE; // return TRUE unless you set the focus to a control } void CUSBPortDlg::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 CUSBPortDlg::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 CUSBPortDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CUSBPortDlg::OnSearch() { // TODO: Add your control notification handler code here char szVolumeAccessPath[] = "\\\\.\\H:"; // "\\.\X:" HANDLE hCom; long DiskNumber = -1; HANDLE hVolume = CreateFile(szVolumeAccessPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL); if (hVolume == INVALID_HANDLE_VALUE) { return; } STORAGE_DEVICE_NUMBER sdn; DWORD dwBytesReturned = 0; long res = DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL); if ( res ) { DiskNumber = sdn.DeviceNumber; } CloseHandle(hVolume); m_strLog.Empty(); m_strLog = _T("正在查找系统中HID类的GUID标识...\n"); SetDlgItemText(IDC_EDIT_REPORT1,m_strLog); // 查找本系统中HID类的GUID标识 GUID guidHID; guidHID = (GUID)GUID_DEVINTERFACE_DISK; // HidD_GetHidGuid(&guidHID); // m_strLog = _T("HID类的GUID标识为\r\n"); CString strShow; strShow.Format("%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n", guidHID.Data1,guidHID.Data2,guidHID.Data3,guidHID.Data4[0], guidHID.Data4[1],guidHID.Data4[2],guidHID.Data4[3],guidHID.Data4[4], guidHID.Data4[5],guidHID.Data4[6],guidHID.Data4[7]); m_strLog = strShow; SetDlgItemText(IDC_EDIT_REPORT2,m_strLog); // 准备查找符合HID规范的USB设备 HDEVINFO hDevInfo = SetupDiGetClassDevs(&guidHID,NULL,NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); UINT nIndex = 0; m_strLog = _T("正在查找可用的USB设备...\n"); SetDlgItemText(IDC_EDIT_REPORT3,m_strLog); // 查找USB设备接口 SP_DEVICE_INTERFACE_DATA strtInterfaceData; SP_DEVICE_INTERFACE_DATA spdid; strtInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); spdid.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); DWORD dwIndex = 0; while ( true ) { BOOL bSuccess = SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,dwIndex, &spdid); if (!bSuccess) { m_strLog = _T("抱歉,未找到可用的USB设备\n"); SetDlgItemText(IDC_EDIT_REPORT4,m_strLog); SetupDiDestroyDeviceInfoList(hDevInfo); return; } SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,dwIndex,&strtInterfaceData); // 若找到了设备,则读取设备路径名 SP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData; strtDetailData.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); DWORD dwSize = 0; SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData, NULL,0,&dwSize,NULL); BYTE Buf[1024]; PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf; SP_DEVINFO_DATA spdd; pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes! ZeroMemory((PVOID)&spdd, sizeof(spdd)); spdd.cbSize = sizeof(spdd); if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData, pspdidd,dwSize,&dwSize,&spdd)) { AfxMessageBox(_T("查找设备路径时出错!")); SetupDiDestroyDeviceInfoList(hDevInfo); return; } // 找到了设备路径 m_strPath = pspdidd->DevicePath; strShow.Format("可以通过路径%s访问设备\r\n",m_strPath); m_strLog = strShow; SetDlgItemText(IDC_EDIT_REPORT4,m_strLog); SetDlgItemText(IDC_EDIT_ID,m_strPath); // 开放与设备的通信 // HANDLE hCom = CreateFile(m_strPath,GENERIC_READ | GENERIC_WRITE, // 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); hCom = CreateFile(pspdidd->DevicePath, GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); // hCom = CreateFile(pspdidd->DevicePath, 0, FILE_SHARE_READ | // FILE_SHARE_WRITE, NULL
评论
    相关推荐
    • USBPort.rar
      USB端口驱动,mfc电脑界面通过USB端口控制单片机上Led矩阵灯的显示。
    • USBPort.rar
      实现USB端口的通信。此方法是一个通用的方法,致力于为usb通信的初学者提供参考!
    • usbport.rar
      usb打印机的端口定位,本程序通过遍历设备找到USB打印机,根据注册表确定端口与状态
    • usb-VCPP.rar
      用VC++编写的USB接口编程(示例代码) 编译通过。
    • USBINTERFACESPURCECODE.rar
      USB INTERFACE SOURCE CODE
    • USB2PPM:用于从PC控制远程设备的界面
      USB2PPM电路通过PC的USB端口进行连接,并通过虚拟串行端口进行通信。 控制输入​​的来源仅取决于PC上运行的软件,该软件会将此信息传输到USB2PPM设备。 通讯协议 通信协议保持非常简单。 与USB2PPM的连接为115200...
    • jsr80 java 访问 usb
      USB 设备附加到 USB 端口上时,就将这个 惟一端口 ID 分配给这台设备,并且 USB 控制器会读取 设备描述符。设备描述符包括适用于该设备的全局信息、以及设备的 配置信息。配置定义了一台 USB 设备的功能和 I/O ...
    • 获得USB的信息vc++
      现在USB设备却很多,因此对USB设备的查找与读写就必不可少了。但是能找到关于USB读写的资料很少。这里使用VC++示范了一些获得USB的信息的方法。 一、枚举USB设备 ...参考了USBPortUSBview等代码,一并致谢。
    • C#USBPort.rar
      C# usb端口检测程序.
    • USB-COM(HL340).rar
      usb转串口,找了很久才找到,现在别的地方很难下到,要不用不了,我这个可以用 HL340D 的