-
大小: 33.26MB文件类型: .rar金币: 1下载: 0 次发布日期: 2023-07-05
- 语言: C/C++
- 标签: winusb;MFC
资源简介
本代码是用vs 2010 (c++)编写的USB通信的上位机,简单实现了上位机和USB设备的连接,可供初学者参考
代码片段和文件信息
#include “stdafx.h“
#include “pch.h“
#include
//获取设备的实例路径
HRESULT
RetrieveDevicePath(
_Out_bytecap_(BufLen) LPTSTR DevicePath
_In_ ULONG BufLen
_Out_opt_ PBOOL FailureDeviceNotFound
);
HRESULT
OpenDevice(
_Out_ PDEVICE_DATA DeviceData
_Out_opt_ PBOOL FailureDeviceNotFound
)
/*++
Routine description:
Open all needed handles to interact with the device.
If the device has multiple USB interfaces this function grants access to
only the first interface.
If multiple devices have the same device interface GUID there is no
guarantee of which one will be returned.
Arguments:
DeviceData - Struct filled in by this function. The caller should use the
WinusbHandle to interact with the device and must pass the struct to
CloseDevice when finished.
FailureDeviceNotFound - TRUE when failure is returned due to no devices
found with the correct device interface (device not connected driver
not installed or device is disabled in Device Manager); FALSE
otherwise.
Return value:
HRESULT
--*/
{
HRESULT hr = S_OK;
BOOL bResult;
DeviceData->HandlesOpen = FALSE;
hr = RetrieveDevicePath(DeviceData->DevicePath
sizeof(DeviceData->DevicePath)
FailureDeviceNotFound);
if (FAILED(hr)) {
return hr;
}
//CreateFile创建文件句柄
DeviceData->DeviceHandle = CreateFile(DeviceData->DevicePath//所要打开的she别名
GENERIC_WRITE | GENERIC_READ//访问模式
FILE_SHARE_WRITE | FILE_SHARE_READ//共享模式
NULL
OPEN_EXISTING//创建方式
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED//文件属性和标志
NULL);
if (INVALID_HANDLE_VALUE == DeviceData->DeviceHandle) {
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
//WinUsb_Initialize为指定的file创建一个winusb句柄
bResult = WinUsb_Initialize(DeviceData->DeviceHandle
&DeviceData->WinusbHandle);
if (FALSE == bResult) {
hr = HRESULT_FROM_WIN32(GetLastError());
CloseHandle(DeviceData->DeviceHandle);
return hr;
}
DeviceData->HandlesOpen = TRUE;
return hr;
}
VOID
CloseDevice(
_Inout_ PDEVICE_DATA DeviceData
)
/*++
Routine description:
Perform required cleanup when the device is no longer needed.
If OpenDevice failed do nothing.
Arguments:
DeviceData - Struct filled in by OpenDevice
Return value:
None
--*/
{
if (FALSE == DeviceData->HandlesOpen) {
//
// Called on an uninitialized DeviceData
//
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4967424 2018-04-23 20:20 USB通信\usbdemo2\Debug\usbdemo2.pdb
文件 9069 2018-04-09 14:39 USB通信\usbdemo2\OOI_QE65000.cat
文件 2453 2018-04-09 14:39 USB通信\usbdemo2\OOI_QE65000.inf
文件 2934 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\cl.command.1.tlog
文件 72706 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\CL.read.1.tlog
文件 1536 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\CL.write.1.tlog
文件 16595 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\device.obj
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 2012 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 5692 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 1182 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\li
文件 740 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\mt.command.1.tlog
文件 656 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\mt.read.1.tlog
文件 474 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\mt.write.1.tlog
文件 1108 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\rc.command.1.tlog
文件 3426 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\rc.read.1.tlog
文件 478 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\rc.write.1.tlog
文件 642604 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\stdafx.obj
文件 915 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.exe.em
文件 980 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.exe.em
文件 640 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.exe.intermediate.manifest
文件 58 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.lastbuildstate
文件 3278 2018-04-23 20:20 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.log
文件 29342 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.obj
文件 33816576 2018-04-23 16:49 USB通信\usbdemo2\usbdemo2\Debug\usbdemo2.pch
............此处省略105个文件信息
- 上一篇:自绘图软件MFC制作
- 下一篇:深入理解C++11.pdf
评论
共有 条评论