VC++HIDAPI USBデータの読み書きを実現

4601 ワード

目次
hidapiの導入
しゅかんすう
初期化と終了
hid_enumerate
hid_open
データの読み取り
データの書き込み
プロジェクトのダウンロード
hidapiはオープンソースのC言語で実現されたusb通信ライブラリで、Linux、MAC、windowsシステムをサポートしていますが、しばらく更新されていません.Windowsシステムでの実装はwinusbに依存して実装されるgithubアドレスであるhttps://github.com/signal11/hidapi
hidapiの導入
主に3つのファイルを導入
`usb_hid.h`:ヘッダファイル、include導入`hidapi.dll`:dllファイル、実行プログラムの同級ディレクトリの下に置く`hidapi.lib`:libライブラリファイル、添付ファイル依存項目に`#pragma comment(lib,“hidapi.lib”)を追加または使用して追加
しゅかんすう
初期化と終了
`hid_init()`と`hid_exit()`ペアが現れ、それぞれ初期化と終了であり、すべてのhid操作が両者の間に含まれる.
hid_enumerate
struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)

デバイス情報を読み出し、デバイスのvidとpidを必要とし、デバイス情報の構造体`hid_を返すdevice_info`、チェーンテーブル構造です.実際には、すべての接続デバイスをクエリーしてvidとpidに基づいてマッチングし、vidまたはpidが0であればデフォルトマッチングに成功します.すなわち、vidとpidが0であれば、すべてのデバイス情報を返します.
struct hid_device_info {
    /** Platform-specific device path */
    char *path;
    /** Device Vendor ID */
    unsigned short vendor_id;
    /** Device Product ID */
    unsigned short product_id;
    /** Serial Number */
    wchar_t *serial_number;
    /** Device Release Number in binary-coded decimal,
        also known as Device Version Number */
    unsigned short release_number;
    /** Manufacturer String */
    wchar_t *manufacturer_string;
    /** Product string */
    wchar_t *product_string;
    /** Usage Page for this Device/Interface
        (Windows/Mac only). */
    unsigned short usage_page;
    /** Usage for this Device/Interface
        (Windows/Mac only).*/
    unsigned short usage;
    /** The USB interface which this logical device
        represents. Valid on both Linux implementations
        in all cases, and valid on the Windows implementation
        only if the device contains more than one interface. */
    int interface_number;
    /** Pointer to the next device */
    struct hid_device_info *next;
};

hid_device_info構造体のデータは以下の方法で解放する必要がある.
void  HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs);

hid_open
デバイスを開いてデバイス操作のハンドルに戻り、デバイスデータの読み取りと書き込みはこのハンドルを使用してデバイスを開くのが独占的であり、使用後は`hid_を使用する必要がある.close`windowsの最下層に閉じる実現方式はwinusbのapi`CreateFile`である.
//    vid pid  
HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
//        
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path);

データの読み取り
`hid_read`
データを読み込み、メッセージを一度に1つ読み込む
 
/**
 * @param device     
 * @param data        buffer
 * @param length          
 *
 * @return           ,-1      
 */
int  HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);

一般的に使用する場合はスレッドを開き、スレッド内でループ読み出し、コードは以下の通りです.
unsigned int _stdcall usb_hid::usb_read_proc(LPVOID lp_param)
{
    usb_hid * params = (usb_hid *)lp_param;
    unsigned char buf[30];
    memset(buf, 0, sizeof(buf));
    int res = 0;
    while (params->is_run_)
    {
        if (!params || !params->dev_handle_)
        {
            break;
        }
        res = hid_read(params->dev_handle_, buf, sizeof(buf));

        if (res > 0)
        {
            for (int i = 0; i < res; i++)
                printf("%02hhx ", buf[i]);
            printf("
"); } if (res < 0) { } Sleep(500); } return 0; }

データの書き込み
`hid_write`データを書く方法で、一度に1つのメッセージを書きます.データを書くときはデータbufferのヘッダがメッセージidであることに注意し、デバイスが単一メッセージの形式しかサポートしていない場合は0 x 0に設定します.これにより、書き込みデータの長さが実際に書き込む必要があるデータより1バイト長くなる.
/** 
	@param device     
	@param data      ,     id
	@param length        (  |byte)

	@returns          ,-1    
*/
int  HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);

実装コードは、状況に応じて非同期で選択されるかどうかです.非同期で操作することをお勧めします.実際の使用中にデータを書くときに詰まりが発生する可能性があります.
int usb_hid::usb_write_data_test(){
	//       (hex):20 00 02 00 07 00 00 00
	unsigned char buffer[9];
	memset(buffer, 0, sizeof(buffer));
	buffer[0] = 0x00;//     report id
	buffer[1] = 0x20;//    
	buffer[2] = 0x00;
	buffer[3] = 0x02;
	buffer[4] = 0x00;
	buffer[5] = 0x07;
	int res = hid_write(dev_handle_, buffer, 9);
	printf("hid_write data res: %d
",res); }

プロジェクトのダウンロード
プロジェクトにusbホットスワップ応答を含むコード
https://download.csdn.net/download/lhangtk/10670821