打印

谁能提供一个读USB的类?

[复制链接]
261|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pengf|  楼主 | 2019-11-15 22:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这几天老做一些底层的东西,API又不熟,谁能提供一个读USB的类?

使用特权

评论回复
沙发
pengf|  楼主 | 2019-11-15 22:26 | 只看该作者
明天买本API的书看看吧。。不想学都不行。。

使用特权

评论回复
板凳
morrisk| | 2019-11-15 22:30 | 只看该作者
你哪本书是C#操作USB口的吗?

使用特权

评论回复
地板
zyf部长| | 2019-11-15 22:33 | 只看该作者
用C#操作比较麻烦吧?

使用特权

评论回复
5
pengf|  楼主 | 2019-11-15 22:38 | 只看该作者
没有现成的读USB口的例子吗?

使用特权

评论回复
6
xxmmi| | 2019-11-15 22:40 | 只看该作者

自己找个现成的库文件,调用就行了。

使用特权

评论回复
7
zwll| | 2019-11-15 22:47 | 只看该作者
USB这东西哟,感觉底层简单,上层协议复杂程度可以和TCPIP相比了

使用特权

评论回复
8
kangzj| | 2019-11-15 22:51 | 只看该作者
最好找个实例看看,CY的芯片都会有相应的API手册。

使用特权

评论回复
9
pengf|  楼主 | 2019-11-15 22:54 | 只看该作者
唉,还是没有什么结果,算了,先结贴吧,多谢大家啦

使用特权

评论回复
10
probedog| | 2024-1-17 16:28 | 只看该作者
cpp
#include <windows.h>  
#include <winusb.h>  
  
class USBReader  
{  
private:  
    HANDLE deviceHandle;  
    WINUSB_INTERFACE_HANDLE winusbHandle;  
    ULONG bufferSize;  
    PUCHAR buffer;  
    ULONG transferSize;  
  
public:  
    USBReader(const char* devicePath)  
    {  
        // Open a handle to the USB device.  
        deviceHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  
        if (deviceHandle == INVALID_HANDLE_VALUE) {  
            printf("Error: Unable to open USB device handle.\n");  
            return;  
        }  
  
        // Claim the interface to get a WinUSB handle.  
        DWORD interfaceNumber = 0; // The interface number to open.  
        DWORD alternateSetting = 0; // The alternate setting of the interface to open.  
        DWORD deviceHandleSize = sizeof(deviceHandle);  
        if (!WinUsb_ClaimInterface(deviceHandle, &interfaceNumber, &winusbHandle)) {  
            printf("Error: Unable to claim USB interface.\n");  
            CloseHandle(deviceHandle);  
            return;  
        }  
  
        // Allocate a buffer for receiving data from the USB device.  
        bufferSize = 64 * 1024; // The size of the buffer to allocate. Adjust this based on your needs.  
        buffer = (PUCHAR)malloc(bufferSize);  
        if (!buffer) {  
            printf("Error: Unable to allocate memory for the USB buffer.\n");  
            WinUsb_Free(winusbHandle);  
            CloseHandle(deviceHandle);  
            return;  
        }  
    }  
  
    ~USBReader()  
    {  
        free(buffer);  
        WinUsb_Free(winusbHandle);  
        CloseHandle(deviceHandle);  
    }  
  
    bool ReadData()  
    {  
        // Set up the transfer parameters. Adjust these based on your needs.  
        ULONG transferLength = 0; // The length of the data to transfer. Adjust this based on your needs.  
        ULONG transferOffset = 0; // The offset at which to start reading data from the device. Adjust this based on your needs.  
        if (!WinUsb_ReadPacket(winusbHandle, transferOffset, buffer, transferLength, &transferSize, 0)) { // Read the data from the device. Adjust this based on your needs.  
            printf("Error: Unable to read data from the USB device.\n");  
            return false;  
        } else {  
            // Do something with the data that was read from the USB device. This might involve printing it or storing it in a file. Adjust this based on your needs.  
            printf("Read %d bytes of data from the USB device.\n", transferSize); // Print the number of bytes that were read. Adjust this based on your needs.  
            return true; // Return true if the data was read successfully, false otherwise. Adjust this based on your needs.  
        }  
    }  
};

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

718

主题

10129

帖子

3

粉丝