Writes and Reads

These functions are all of the available ways to communicate with the connected USB devices in your plugins. The commands are broken up into two flavors of reads and writes depending on the device's protocols, and control transfers which allow for handshake commands and advanced functionality.

device.write()[send_report]

This function performs a Hid_Write command on the devices currently selected endpoint. This is the write function that most devices use and will be the most commonly used command.

  • Note: Another Flavor of write is the device.send_report. This is the write command used to send HID feature reports to the device. The functionality is otherwise the same as device.write().

  • Note: Setting a length longer than the provided data array will pad the end of the array with 0x00.

  • Note: Most HID devices will zero pad the front of their write commands. This is done by placing an extra 0x00 in front of your DataArray and increasing the length by one. these values are removed by the device during data transfer. This primarily happens when the device has no Report ID for the selected Endpoint.

Parameter

Type

Description

Example

DataArray

1D Array

An Array containing Hex bytes to send to the device

[0x08,0xAB,0xFF,0x37]

Length

Int

An Int value representing the total packet length to send

4

Below is an example of a packet from the ASUS LED controllers showing creation and delivery to the device.

var packet = []; packet[0] = 0xEC; //This is the Report Id packet[1] = 0x40; //Command packet[2] = apply ? 0x80 | channel : channel; //Channel Number packet[3] = start; //Led to Start on packet[4] = count; //Led Count packet = 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 Bytes

device.read()[get_report]

This function call takes an array containing an endpoint Report Id and the length of bytes to read and performs a Hid_Read on the device. The function returns an array of bytes read from the device Some devices require a read to prevent buffer overflows and occasionally need to read configuration or setting data from devices.

  • Note: Another flavor of read is:

  • device.read_report*. This is the read command used to get HID feature reports from the device. The functionality is otherwise the same as device.read().

Parameter

Type

Description

Example

DataArray

1D Array

An array containing the report Id needed for the read command

[0x08, 0x02]

Length

Int

An Int value representing the number of bytes to read

65

Return

Type

Description

Example

DataArray

1D Array

An array containing all of the read HEX Bytes from the device

[0x08, 0x02,0x00,0x64]

  • Note: This function returns a Data Array matching the devices Report length. If you need the actual read byte count use device.getLastReadSize()

var config = device.read(packet, 65);

device.getLastReadSize()

This function returns the number of bytes read by the last read/get_report done to the device.

Return

Type

Description

Example

BytesRead

Int

Number of bytes read from the Device

64

Below is an example from the Glorious Model 0 Mouse.

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.control_transfer()

device.flush()

This function attempts to clear the device's read buffer entirely. This can be useful to reset before any critical reads are performed on the device.

  • Note: This function's usefulness is dependent on the device, and the endpoint. If the device doesn't handle flushing you may need to manually clear it with looping reads until device.getLastReadSize() returns 0 bytes.

export function Initialize(){ device.flush() if(Corsair_Get(CORSAIR_MODE) == CORSAIR_HARDWARE_MODE){ EnableSoftwareControl(); } }