当前位置: 首页 > news >正文

工信部 网站备案查询hotnews wordpress

工信部 网站备案查询,hotnews wordpress,国家网站标题颜色搭配,国外js建设网站前言 这篇文章主要介绍在PC#xff08;上位机#xff0c;Host#xff09;端#xff0c;通过HID与硬件进行通信的一些总结#xff0c;像很多同学肯定和我一样压根不想 去了解什么USB相关的资料#xff0c;毕竟USB太复杂了#xff0c;只想有个API给我们进行下数据就好了上位机Host端通过HID与硬件进行通信的一些总结像很多同学肯定和我一样压根不想 去了解什么USB相关的资料毕竟USB太复杂了只想有个API给我们进行下数据就好了像这里主要是我在进行hid通信的总结。 以下理解只是站在PC开发HID软件时的角度所以讲述的一些USB知识不会很详细 USB 简单介绍 这里只是简单描述一下USB如果感兴趣的同学可以去查看《从零开始学USB》作者to-run-away这是介绍USB一个系类的文章讲的特别详细。 USB传输类型 详细介绍请参考从零开始学USB十三、USB的四种传输类型2 USB协议规定了4种传输类型批量传输、等时传输、中断传输和控制传输像HID主要使用中断传输和控制传输。 USB描述符 详细请参考 USB有好几种描述符 像我们只需要了解设备描述符配置描述符接口描述符端点描述符还有HID描述符就好了。 设备描述符 主要是描述USB设备的信息比如VID,PIDVID是厂商向USB协会申请的一个ID这个ID可以网上查的到的查询链接PID表示这款产品的ID属于每个公司自己定义的。我们一般根据VIDPID去查找USB设备的以及USB设备的名字序列号制造商这些。 BYTE blength; //设备描述符的字节数大小 BYTE bDescriptorType; //设备描述符类型编号 WORD bcdUSB; //USB版本号 BYTE bDeviceClass; //USB分配的设备类代码 BYTE bDeviceSubClass; //USB分配的子类代码 BYTE bDeviceProtocol; //USB分配的设备协议代码 BYTE bMaxPacketSize0; //端点0的最大包大小 WORD idVendor; //厂商编号 WORD idProduct; //产品编号 WORD bcdDevice; //设备版本 BYTE iManufacturer; //设备厂商字符串的索引 BYTE iProduct; //描述产品字符串的索引 BYTE iSerialNumber; //描述设备序列号字符串的索引 BYTE bNumConfigurations; //可能的配置数量 配置描述符 主要描述有多少个接口。 BYTE bLength; //配置描述符的字节数大小 BYTE bDescriptorType; //配置描述符类型编号 WORD wTotalLength; //此配置返回的所有数据大小 BYTE bNumInterfaces; //此配置所支持的接口数量 BYTE bConfigurationValue; //Set_Configuration命令所需要的参数值 BYTE iConfiguration; //描述该配置的字符串的索引值 BYTE bmAttributes; //供电模式的选择 BYTE MaxPower; //设备从总线提取的最大电流 接口描述符 主要描述这个接口下面有多个端点以及这个接口做什么用的比如这个接口是HID或者CDC,音频输入设备UAC视频输入设备(UVC)等。 端点描述符 主要描述这个端点最大一次可以传输多少数据以及这个端点支持什么传输方式。在HID中支持控制传输和中断传输。 控制传输 主要用来获取USB信息以及可以用来下命令去控制设备读取时需要主动去读USB设备无法主动给我们发送数据。 中断传输 如果端点是OUT的话那么我们PC可以往USB设备被发送数据USB设备无法主动向这个端点发送数据如果是端点是IN的话那么PC无法往这个端点发送数据但是USB设备可以主动像里面发送数据。 HID描述符 HID描述符特别复杂想了解的大家可以去看这几篇文章USB HID设备报告描述符详解 USB HID报告描述符教程 HID描述符和端点一样也是在接口下面的主要是描述PC和USB设备之间通信的数据是做什么用的像我们只需要知道这几个东西Usage Page/Usage以及Report ID。 Usage Page/Usage可以理解为一个报表说明里面会描述这个报表数据多长以及这些数据是干啥子用的范围为0x00-0xFFFF,其中0x00-0xFEFF为预留的用于描述一些标准规范的报表比如鼠标指针键盘按键值等0xFF00-0xFFFF用于给开发商自定义使用像在设备管理器中看到有设备名字为符合 HID 标准的供应商定义设备的表示为Usage Page为0xFF00-0xFFFF的HID报表。 Report ID 主要用户区分不同报表的比如一个HID描述符里面可能有多个Usage Page/Usage当PC和HID进行通信时下的数据不知道对应哪个报表的因此需要有个ID进行区分这就是Report ID 。 比如有两个Usage Page0xFF00,0xFF01其中0xFF00描述数据用于设置灯光亮度0xFF01描述数据用于设置音量大小与0xFF000xFF01对应的ReportID分别为0x050x06当我们发一段数据到设备设备收到0x05开头的数据就知道是设置灯光亮度收到0x06的数据是用来调节音量。 HID通信 HID通信我们使用 https://github.com/libusb/hidapi 这个开源库这个支持跨平台不建议去使用libusb去进行HID访问太底层了学习使用成本高。 在进行通信前需要先和硬件了解USB HID设备的以下信息 需要控制的Usage Page/Usage 是多少。需要数据的Report ID是多少不存在的话默认为0。Usage Page/Usage 一次支持写入多少字节的数据。 这些是通信的关键识别标志。 hidapi中有以下几个函数 //写入数据到设备支持控制传输以及中断传输如果设备有ReportId,那么data首字节需要为此ID如果没有首字节默认需要为0 //返回写入的字节数如果写入为-1说明写入失败 int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length); //从设备读取数据只支持中断传输这个数据是设备主动传给PC的如果设备有ReportId,那么data首字节为此ID int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds); // int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length); // int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length); //通过控制传输读取设备数据如果设备有ReportId,那么data首字节需要为此ID如果没有首字节默认需要为0 //返回读取到的数据长度小于0读取失败 int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length);一般使用比较多的是hid_writehid_read_timeouthid_get_input_report具体看设备端的定义。 HID通信 Windows下Windows的hid驱动针对HID每个Usage Page/Usage都抽象出来了一个Path如下图所示因此我们想往某个Usage Page/Usage发送数据时就需要去打开与之对应的HID对象,不然无法写入。 MAC端MAC没有像Windows那样只需要使用hidapi找到指定VID PID的设备直接打开通信即可。 打开设备 hidapi里面这个函数用于打开设备在Mac端使用时没有问题但是当存在多个Usage Page在Windows端使用就会存在问题他只会打开hid_enumerate扫描到的第一个Usage Page比如存在0x01和0xFF00,当想打开0xFF00时它只会打开0x01的。 HID_API_EXPORT hid_device *HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);因此建议将上面的函数改成如下所示通过指定Usage Page进行打开 HID_API_EXPORT hid_device* HID_API_CALL hid_open2(unsigned short vendor_id, unsigned short product_id, unsigned short usage_page) {/* TODO: Merge this functions with the Linux version. This function should be platform independent. */struct hid_device_info* devs, * cur_dev;const char* path_to_open NULL;hid_device* handle NULL;devs hid_enumerate(vendor_id, product_id);cur_dev devs;while (cur_dev) {if (cur_dev-vendor_id vendor_id cur_dev-product_id product_id) {if (usage_page cur_dev-usage_page) {path_to_open cur_dev-path;break;}}cur_dev cur_dev-next;}if (path_to_open) {/* Open the device */handle hid_open_path(path_to_open);}hid_free_enumeration(devs);return handle; } 写入数据 /** brief Write an Output report to a HID device.The first byte of p data[] must contain the Report ID. Fordevices which only support a single report, this must be setto 0x0. The remaining bytes contain the report data. Sincethe Report ID is mandatory, calls to hid_write() will alwayscontain one more byte than the report contains. For example,if a hid report is 16 bytes long, 17 bytes must be passed tohid_write(), the Report ID (or 0x0, for devices with asingle report), followed by the report data (16 bytes). Inthis example, the length passed in would be 17.hid_write() will send the data on the first OUT endpoint, ifone exists. If it does not, it will send the data throughthe Control Endpoint (Endpoint 0).ingroup APIparam dev A device handle returned from hid_open().param data The data to send, including the report number asthe first byte.param length The length in bytes of the data to send.returnsThis function returns the actual number of bytes written and-1 on error.Call hid_error(dev) to get the failure reason.*/int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length);写入数据时假如USB描述设置最大一次可以写入64字节如果存在Report ID那么第一个字节必须是Report ID,那么还可以写入63个字节的有效数据一起64字节。如果不存在Report ID那边第一个字节固件设置为0后面还可能写64字节一起写入65字节。如果写入的数据不足建议补齐到64或者65 例如 //写入数据到Report ID 0x12unsigned char data[64] { 0 };data[0] 0x12;hid_write(pDevice, data, sizeof(data));//没有Report IDunsigned char data[65] { 0 };data[0] 0;data[1] 0x80;hid_write(pDevice, data, sizeof(data));读取数据控制传输 /** brief Get a input report from a HID device.Since version 0.10.0, ref HID_API_VERSION HID_API_MAKE_VERSION(0, 10, 0)Set the first byte of p data[] to the Report ID of thereport to be read. Make sure to allow space for thisextra byte in p data[]. Upon return, the first byte willstill contain the Report ID, and the report data willstart in data[1].ingroup APIparam dev A device handle returned from hid_open().param data A buffer to put the read data into, includingthe Report ID. Set the first byte of p data[] to theReport ID of the report to be read, or set it to zeroif your device does not use numbered reports.param length The number of bytes to read, including anextra byte for the report ID. The buffer can be longerthan the actual report.returnsThis function returns the number of bytes read plusone for the report ID (which is still in the firstbyte), or -1 on error.Call hid_error(dev) to get the failure reason.*/int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length);读取数据时如果存在Report ID那么第一个字节必须先填写Report ID才能读取到指定Report ID的数据,如果没有第一个字节固定设置为0 例如 //读取Report ID 0x12的数据unsigned char data[64] { 0 };data[0] 0x12;hid_get_input_report(pDevice, data, sizeof(data));//没有Report IDunsigned char data[65] { 0 };data[0] 0;hid_get_input_report(pDevice, data, sizeof(data));读取数据中断传输 /** brief Read an Input report from a HID device with timeout.Input reports are returnedto the host through the INTERRUPT IN endpoint. The first byte willcontain the Report number if the device uses numbered reports.ingroup APIparam dev A device handle returned from hid_open().param data A buffer to put the read data into.param length The number of bytes to read. For devices withmultiple reports, make sure to read an extra byte forthe report number.param milliseconds timeout in milliseconds or -1 for blocking wait.returnsThis function returns the actual number of bytes read and-1 on error.Call hid_error(dev) to get the failure reason.If no packet was available to be read withinthe timeout period, this function returns 0.*/int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);读取数据时如果存在Report ID那么设备返回的数据第一个字节是Report ID。 中断传输设备主动给PC发送数据因此如果设备发送过多系统过有一个缓冲区进行缓冲因此直接调用hid_read_timeout可能可以连续调用hid_read_timeout读取多条数据这是因为缓冲了多条数据在系统里面如果全部读取完之后设备也一直没有回复数据过来再次调用会阻塞超时时间通过milliseconds设置。 例如 unsigned char data[64] { 0 };data[0] 0x12;hid_read_timeout(pDevice, data, sizeof(data));
http://www.tj-hxxt.cn/news/219941.html

相关文章:

  • 陇南网站网站建设朝阳网站建设怎么样
  • 法律网站建设免费logo网站
  • wordpress安装后输入什么域名枫树seo
  • 自己怎么做网站模块本地黄页小程序
  • 网站建设方法冫金手指排名26好看的wordpress模版
  • 上海网站制作股权分配系统建设网站
  • 传媒公司网站制作线上广告投放收费标准
  • 有经验的做网站wordpress分类指定页面
  • 网站升级方案公司起名字大全免费三个字
  • 网站26个页面收费全球快速建站工具
  • 海外医疗兼职网站建设阿里巴巴网站怎么做推广方案
  • 在元典公司做网站有合同吗石家庄谷歌seo公司
  • 企业建站公司流程外贸电商网站制作
  • 杭州网站建设哪家快速上线新手怎么做销售
  • 网站加载动画效果loading新型建筑模板设备
  • 企业服务工作站网页搜索优化
  • 宁波网站优化公司价格wordpress手机端滑动侧栏
  • 常州外贸集团 网站建设网站接入服务单位
  • 网站源码在哪里wordpress idc模板
  • 网站建设公司效益怎么样wordfence wordpress
  • 北京网站维护公司河南网站推广优化多少钱
  • 专业的手机网站建设公司哪家好网络平台制作公司
  • 杭州网站seo公司哈尔滨建设厅网站
  • 建设银行网站是多少钱西安app定制开发公司
  • 使用php做的学校网站广告设计与制作需要学什么
  • 新手怎么做自己网站广告长春seo网站排名优化
  • 网站建设毕业设计个人总结网站建设现状调查研究
  • 门户网站的建立手机做兼职的网站设计
  • 东营网站开发招聘北京装修公司前十名有哪些
  • 如何开发网站平台qq上如何做文学网站