写入和读取
这些函数是在插件中与已连接 USB 设备通信的所有可用方式。命令根据设备的协议分为两种读取和写入方式,以及允许握手命令和高级功能的控制传输。
有关高级方法(bulk_transfer、control_transfer、input_report)和网络/串口通信,请参阅高级通信。
device.write()[send_report]
Section titled “device.write()[send_report]”此函数对设备当前选定的端点执行 Hid_Write 命令。这是大多数设备使用的写入函数,也是最常用的命令。
- 注意:另一种写入方式是 device.send_report。这是用于向设备发送 HID 功能报告的写入命令。其功能与 device.write() 相同。
- 注意:设置比提供的数据数组更长的长度将在数组末尾填充 0x00。
- 注意:大多数 HID 设备会在其写入命令前面填充零。这是通过在 DataArray 前面放置一个额外的 0x00 并将长度增加一来实现的。这些值在数据传输期间由设备删除。当所选端点没有 Report ID 时,这种情况主要会发生。
| 参数 | 类型 | 描述 | 示例 |
|---|---|---|---|
| DataArray | 1D 数组 | 包含要发送到设备的十六进制字节的数组 | [0x08,0xAB,0xFF,0x37] |
| Length | Int | 表示要发送的总数据包长度的整数值 | 4 |
以下是来自 ASUS LED 控制器的数据包示例,显示了其创建和传递到设备的过程。
var packet = [];packet[0] = 0xEC; //This is the Report Idpacket[1] = 0x40; //Commandpacket[2] = apply ? 0x80 | channel : channel; //Channel Numberpacket[3] = start; //Led to Start onpacket[4] = count; //Led Countpacket = packet.concat(RGBData); //Array of RGB values in a [R,G,B ...] format
device.write(packet, 65); //Writing the packet to the device in 65 Bytesdevice.read()[get_report]
Section titled “device.read()[get_report]”此函数调用接受一个包含端点 Report ID 和要读取的字节长度的数组,并对设备执行 Hid_Read。该函数返回从设备读取的字节数组。某些设备需要读取以防止缓冲区溢出,偶尔也需要从设备读取配置或设置数据。
- 注意:另一种读取方式是 device.get_report。这是用于从设备获取 HID 功能报告的读取命令。其功能与 device.read() 相同。
| 参数 | 类型 | 描述 | 示例 | 默认值 |
|---|---|---|---|---|
| DataArray | 1D 数组 | 包含读取命令所需 Report ID 的数组 | [0x08, 0x02] | |
| Length | Int | 表示要读取的字节数的整数值 | 65 | |
| Timeout | Int | 返回前等待数据的毫秒数。传入 0 进行非阻塞检查。 | 100 | 100 |
| 返回值 | 类型 | 描述 | 示例 |
|---|---|---|---|
| DataArray | 1D 数组 | 包含从设备读取的所有十六进制字节的数组,如果超时到期且无数据则为空数组 | [0x08, 0x02,0x00,0x64] |
- 注意:此函数返回与设备报告长度匹配的数据数组。如果需要实际读取的字节数,请使用
device.getLastReadSize()。 - 注意:当超时到期且无数据时,返回值为空数组,
device.getLastReadSize()返回0。
// 标准阻塞读取(默认 100ms 超时)var config = device.read(packet, 65);
// 非阻塞快速读取 — 立即返回,使用 getLastReadSize() 检查数据是否到达var data = device.read([], 64, 0);if(device.getLastReadSize() > 0) { processResponse(data);}device.getLastReadSize()
Section titled “device.getLastReadSize()”此函数返回最后一次对设备进行读取/get_report 所读取的字节数。
| 返回值 | 类型 | 描述 | 示例 |
|---|---|---|---|
| BytesRead | Int | 从设备读取的字节数 | 64 |
以下是来自 Glorious Model 0 鼠标的示例。
function CheckPacketLength(){
var packet = [0x52]
packet = device.get_report(packet,200) //attempts to read up to 200 bytes
return device.getLastReadSize(); //Returns 186 on a successful read of the config packet
}device.clearReadBuffer()
Section titled “device.clearReadBuffer()”清除设备读取缓冲区中排队的任何待处理数据。在关键读取之前调用此函数,以确保接收到新数据而不是陈旧的缓冲响应。
- 注意:此函数的实用性取决于设备和端点。如果设备不支持刷新,请通过循环读取手动清空它,直到
device.getLastReadSize()返回 0。
device.clearReadBuffer();device.write(requestPacket, 64);var response = device.read([], 64);device.flush()
Section titled “device.flush()”此函数尝试使用设备的本机刷新机制完全清除设备的读取缓冲区。在大多数情况下,首选 device.clearReadBuffer()。
export function Initialize(){ device.flush() if(Corsair_Get(CORSAIR_MODE) == CORSAIR_HARDWARE_MODE){ EnableSoftwareControl(); }}device.control_transfer()
Section titled “device.control_transfer()”有关 control_transfer、bulk_transfer 和 input_report 的完整文档,请参阅高级通信。