基于winusb的上位机与USB的通信


本代码是用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通信usbdemo2Debugusbdemo2.pdb

     文件       9069  2018-04-09 14:39  USB通信usbdemo2OOI_QE65000.cat

     文件       2453  2018-04-09 14:39  USB通信usbdemo2OOI_QE65000.inf

     文件       2934  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debugcl.command.1.tlog

     文件      72706  2018-04-23 20:20  USB通信usbdemo2usbdemo2DebugCL.read.1.tlog

     文件       1536  2018-04-23 20:20  USB通信usbdemo2usbdemo2DebugCL.write.1.tlog

     文件      16595  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debugdevice.obj

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink-cvtres.read.1.tlog

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink-cvtres.write.1.tlog

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.12608-cvtres.read.1.tlog

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.12608-cvtres.write.1.tlog

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.12608.read.1.tlog

     文件          2  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.12608.write.1.tlog

     文件       2012  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.command.1.tlog

     文件       5692  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.read.1.tlog

     文件       1182  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debuglink.write.1.tlog

     文件        740  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugmt.command.1.tlog

     文件        656  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugmt.read.1.tlog

     文件        474  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugmt.write.1.tlog

     文件       1108  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debug
c.command.1.tlog

     文件       3426  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debug
c.read.1.tlog

     文件        478  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debug
c.write.1.tlog

     文件     642604  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugstdafx.obj

     文件        915  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugusbdemo2.exe.embed.manifest

     文件        980  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugusbdemo2.exe.embed.manifest.res

     文件        640  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debugusbdemo2.exe.intermediate.manifest

     文件         58  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debugusbdemo2.lastbuildstate

     文件       3278  2018-04-23 20:20  USB通信usbdemo2usbdemo2Debugusbdemo2.log

     文件      29342  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugusbdemo2.obj

     文件   33816576  2018-04-23 16:49  USB通信usbdemo2usbdemo2Debugusbdemo2.pch

............此处省略105个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)