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.
ParameterTypeDescriptionExample
DataArray1D ArrayAn Array containing Hex bytes to send to the device[0x08,0xAB,0xFF,0x37]
LengthIntAn Int value representing the total packet length to send4

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

Javascript
Copy

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().
ParameterTypeDescriptionExample
DataArray1D ArrayAn array containing the report Id needed for the read command[0x08, 0x02]
LengthIntAn Int value representing the number of bytes to read65
ReturnTypeDescriptionExample
DataArray1D ArrayAn 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()
Javascript
Copy

device.getLastReadSize()

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

ReturnTypeDescriptionExample
BytesReadIntNumber of bytes read from the Device64

Below is an example from the Glorious Model 0 Mouse.

Javascript
Copy

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.
Javascript
Copy
Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard