• usb_hid编程


    通过上位机软件给英飞凌芯片发送信号,模拟喷油,节气门开度等信息,但是在重复发送命令和reset过程中,会出现读取不到数据的情况,调查了好久也没有找到原因,与下位机通信的接口都是调用的微软封装的SetupDi系列函数,但是总是读不上来数据,问题原因有待调查,今天找到了HID API的纯C封装版本,而且跨平台,看了下介绍,很强大,记录下来。先贴例子程序

      1 /*******************************************************
      2  Windows HID simplification
      3 
      4  Alan Ott
      5  Signal 11 Software
      6 
      7  8/22/2009
      8 
      9  Copyright 2009, All Rights Reserved.
     10  
     11  This contents of this file may be used by anyone
     12  for any reason without any conditions and may be
     13  used as a starting point for your own applications
     14  which use HIDAPI.
     15 ********************************************************/
     16 
     17 #include <stdio.h>
     18 #include <wchar.h>
     19 #include <string.h>
     20 #include <stdlib.h>
     21 #include "hidapi.h"
     22 
     23 // Headers needed for sleeping.
     24 #ifdef _WIN32
     25     #include <windows.h>
     26 #else
     27     #include <unistd.h>
     28 #endif
     29 
     30 int main(int argc, char* argv[])
     31 {
     32     int res;
     33     unsigned char buf[256];
     34     #define MAX_STR 255
     35     wchar_t wstr[MAX_STR];
     36     hid_device *handle;
     37     int i;
     38 
     39 #ifdef WIN32
     40     UNREFERENCED_PARAMETER(argc);
     41     UNREFERENCED_PARAMETER(argv);
     42 #endif
     43 
     44     struct hid_device_info *devs, *cur_dev;
     45     
     46     if (hid_init())
     47         return -1;
     48 
     49     devs = hid_enumerate(0x0, 0x0);
     50     cur_dev = devs;    
     51     while (cur_dev) {
     52         printf("Device Found
      type: %04hx %04hx
      path: %s
      serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
     53         printf("
    ");
     54         printf("  Manufacturer: %ls
    ", cur_dev->manufacturer_string);
     55         printf("  Product:      %ls
    ", cur_dev->product_string);
     56         printf("  Release:      %hx
    ", cur_dev->release_number);
     57         printf("  Interface:    %d
    ",  cur_dev->interface_number);
     58         printf("
    ");
     59         cur_dev = cur_dev->next;
     60     }
     61     hid_free_enumeration(devs);
     62 
     63     // Set up the command buffer.
     64     memset(buf,0x00,sizeof(buf));
     65     buf[0] = 0x01;
     66     buf[1] = 0x81;
     67     
     68 
     69     // Open the device using the VID, PID,
     70     // and optionally the Serial number.
     71     ////handle = hid_open(0x4d8, 0x3f, L"12345");
     72     handle = hid_open(0x4d8, 0x3f, NULL);
     73     if (!handle) {
     74         printf("unable to open device
    ");
     75          return 1;
     76     }
     77 
     78     // Read the Manufacturer String
     79     wstr[0] = 0x0000;
     80     res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
     81     if (res < 0)
     82         printf("Unable to read manufacturer string
    ");
     83     printf("Manufacturer String: %ls
    ", wstr);
     84 
     85     // Read the Product String
     86     wstr[0] = 0x0000;
     87     res = hid_get_product_string(handle, wstr, MAX_STR);
     88     if (res < 0)
     89         printf("Unable to read product string
    ");
     90     printf("Product String: %ls
    ", wstr);
     91 
     92     // Read the Serial Number String
     93     wstr[0] = 0x0000;
     94     res = hid_get_serial_number_string(handle, wstr, MAX_STR);
     95     if (res < 0)
     96         printf("Unable to read serial number string
    ");
     97     printf("Serial Number String: (%d) %ls", wstr[0], wstr);
     98     printf("
    ");
     99 
    100     // Read Indexed String 1
    101     wstr[0] = 0x0000;
    102     res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
    103     if (res < 0)
    104         printf("Unable to read indexed string 1
    ");
    105     printf("Indexed String 1: %ls
    ", wstr);
    106 
    107     // Set the hid_read() function to be non-blocking.
    108     hid_set_nonblocking(handle, 1);
    109     
    110     // Try to read from the device. There shoud be no
    111     // data here, but execution should not block.
    112     res = hid_read(handle, buf, 17);
    113 
    114     // Send a Feature Report to the device
    115     buf[0] = 0x2;
    116     buf[1] = 0xa0;
    117     buf[2] = 0x0a;
    118     buf[3] = 0x00;
    119     buf[4] = 0x00;
    120     res = hid_send_feature_report(handle, buf, 17);
    121     if (res < 0) {
    122         printf("Unable to send a feature report.
    ");
    123     }
    124 
    125     memset(buf,0,sizeof(buf));
    126 
    127     // Read a Feature Report from the device
    128     buf[0] = 0x2;
    129     res = hid_get_feature_report(handle, buf, sizeof(buf));
    130     if (res < 0) {
    131         printf("Unable to get a feature report.
    ");
    132         printf("%ls", hid_error(handle));
    133     }
    134     else {
    135         // Print out the returned buffer.
    136         printf("Feature Report
       ");
    137         for (i = 0; i < res; i++)
    138             printf("%02hhx ", buf[i]);
    139         printf("
    ");
    140     }
    141 
    142     memset(buf,0,sizeof(buf));
    143 
    144     // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
    145     buf[0] = 0x1;
    146     buf[1] = 0x80;
    147     res = hid_write(handle, buf, 17);
    148     if (res < 0) {
    149         printf("Unable to write()
    ");
    150         printf("Error: %ls
    ", hid_error(handle));
    151     }
    152     
    153 
    154     // Request state (cmd 0x81). The first byte is the report number (0x1).
    155     buf[0] = 0x1;
    156     buf[1] = 0x81;
    157     hid_write(handle, buf, 17);
    158     if (res < 0)
    159         printf("Unable to write() (2)
    ");
    160 
    161     // Read requested state. hid_read() has been set to be
    162     // non-blocking by the call to hid_set_nonblocking() above.
    163     // This loop demonstrates the non-blocking nature of hid_read().
    164     res = 0;
    165     while (res == 0) {
    166         res = hid_read(handle, buf, sizeof(buf));
    167         if (res == 0)
    168             printf("waiting...
    ");
    169         if (res < 0)
    170             printf("Unable to read()
    ");
    171         #ifdef WIN32
    172         Sleep(500);
    173         #else
    174         usleep(500*1000);
    175         #endif
    176     }
    177 
    178     printf("Data read:
       ");
    179     // Print out the returned buffer.
    180     for (i = 0; i < res; i++)
    181         printf("%02hhx ", buf[i]);
    182     printf("
    ");
    183 
    184     hid_close(handle);
    185 
    186     /* Free static HIDAPI objects. */
    187     hid_exit();
    188 
    189 #ifdef WIN32
    190     system("pause");
    191 #endif
    192 
    193     return 0;
    194 }

    网站地址: http://www.signal11.us/oss/hidapi/

    为了方便读者的使用,我已经编译出了一个版本,可以直接下载,压缩包里有dll和lib两个版本。

    http://pan.baidu.com/s/1DuNEP

  • 相关阅读:
    2017-2018-2 20155206 《网络对抗技术》 实验六:信息搜集与漏洞扫描
    20155206 Exp5 MSF基础应用
    20155206 实验4 恶意代码分析
    20155206《网络攻防》第三次实验_免杀及其原理
    20155206 Exp2 后门原理与实践
    20155206赵飞 Exp1PC平台逆向破解及Bof基础实践
    J-19 集合对象
    J-18 集合对象
    J-17 集合对象
    J-16 集合对象
  • 原文地址:https://www.cnblogs.com/lniwn/p/3411528.html
Copyright © 2020-2023  润新知