谁能提供一个读USB的类?

[复制链接]
741|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#操作比较麻烦吧?
 楼主| pengf 发表于 2019-11-15 22:38 | 显示全部楼层
没有现成的读USB口的例子吗?
xxmmi 发表于 2019-11-15 22:40 | 显示全部楼层

自己找个现成的库文件,调用就行了。
zwll 发表于 2019-11-15 22:47 | 显示全部楼层
USB这东西哟,感觉底层简单,上层协议复杂程度可以和TCPIP相比了
kangzj 发表于 2019-11-15 22:51 | 显示全部楼层
最好找个实例看看,CY的芯片都会有相应的API手册。
 楼主| pengf 发表于 2019-11-15 22:54 | 显示全部楼层
唉,还是没有什么结果,算了,先结贴吧,多谢大家啦
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

粉丝
快速回复 在线客服 返回列表 返回顶部