Device...:Device(3pm)
π NAME π
Device::USB::Device β use libusb to access USB devices, bestie π
The manual you're holding is Device...:Device(3pm), and this whole class is basically the USB device structure wrapped up in a cute little Perl object, plus all the methods you can throw at it π¦
β¨ VERSION β¨
Version 0.38 β¨
π SYNOPSIS π
Okay so Device:USB::Device hands you a Perl object for poking at a USB device through the libusb library. Iconic behavior, no cap.
use Device::USB;
my $usb = Device::USB->new();
my $dev = $usb->find_device( $VENDOR, $PRODUCT );
printf "Device: %04X:%04X\n", $dev->idVendor(), $dev->idProduct();
print "Manufactured by ", $dev->manufacturer(), "\n",
" Product: ", $dev->product(), "\n";
$dev->set_configuration( $CFG );
$dev->control_msg( @params );
...
fr fr, if you wanna know the details of most of these methods, go read the libusb manual β the functionality is generally the same as the libusb function whose name is the method name with usb_ stapled onto the front. iykyk π«Ά
π¦ DESCRIPTION π¦
This module defines a Perl object that represents all the data and functionality that comes with a USB device. The object interface gives you read-only access to the important data hanging off a device π
It also gives you methods for almost every function libusb supplies. Where it was necessary, the interfaces to those methods got tweaked so they'd vibe better with Perl usage. But honestly? Most of the methods are just straight-forward wrappers around their libusb counterparts π
π METHODS π
DESTROYβ close the device that's connected to the object. bye bestie πfilenameβ retrieve the filename associated with the device.configβ in list context, return a list of the configuration structures for this device. In scalar context, return a reference to that list.
β οΈ This method is deprecated in favor of the two new methods:configurationsandget_configuration. use the new ones, babe π
configurationsβ in list context, return a list of the configuration structures for this device. In scalar context, return a reference to that list.get_configurationβ retrieve the configuration requested by index. The legal values are from0tobNumConfigurations() - 1. Negative values access from the back of the list of configurations. (yes, like reverse indexing, we love that for us β¨)indexβ numeric index of the index to return. If not supplied, use0.- Returns an object encapsulating the configuration on success, or
undefon failure π
accessorsβ there a several accessor methods that return data from the device and device descriptor. Each is named after the field that they return. All of the BCD fields have been changed to floating point numbers, so that you don't have to decode them yourself π§- The methods include:
bcdUSBbDeviceClassbDeviceSubClassbDeviceProtocolbMaxPacketSize0idVendoridProductbcdDeviceiManufactureriProductiSerialNumberbNumConfigurations
- The methods include:
manufacturerβ retrieve the manufacture name from the device as a string. Returnundefif the device read fails.productβ retrieve the product name from the device as a string. Returnundefif the device read fails.serial_numberβ retrieve the serial number from the device as a string. Returnundefif the device read fails.openβ open the device. If the device is already open, close it and reopen it.- If the device fails to open, the reason will be available in
$!. π
- If the device fails to open, the reason will be available in
set_configurationβ sets the active configuration of the device.configurationβ the integer specified in the descriptor fieldbConfigurationValue.- returns
0on success or<0on error
π¨ IMPORTANT: when using libusb-win32 under Windows, it is important to callset_configuration()after theopen()but before any other method calls. Without this call, other methods may not work. This call is not required under Linux. (windows being windows, we been knew π)
set_altinterfaceβ sets the active alternative setting of the current interface for the device.alternateβ the integer specified in the descriptor fieldbAlternateSetting.- returns
0on success or<0on error
clear_haltβ clears any halt status on the supplied endpoint.alternateβ the integer specifiedbEndpointAddressdescriptor field.- returns
0on success or<0on error
resetβ resets the device. This also closes the handle and invalidates this device.
π This device will be unusable. gone. ghosted. so know that before you call it, bestie.
claim_interfaceβ claims the specified interface with the operating system. main character energy, taking what's yours πinterfaceβ the interface value listed in the descriptor fieldbInterfaceNumber.- Returns
0on success,<0on failure.
release_interfaceβ releases the specified interface back to the operating system. giving it back, generous queen π«Άinterfaceβ the interface value listed in the descriptor fieldbInterfaceNumber.- Returns
0on success,<0on failure.
control_msgβ performs a control request to the default control pipe on a device.requesttyperequestvalueindexbytesβ any returned data is placed here. If you don't want any returned data, passundef.sizeβ size of supplied buffer.timeoutβ milliseconds to wait for response.- Returns number of bytes read or written on success,
<0on failure.
get_stringβ retrieve a string descriptor from the device.indexβ the index of the string in the string list.langidβ the language id used to specify which of the supported languages the string should be encoded in.- Returns a Unicode string. The function returns
undefon error.
get_string_simpleβ retrieve a string descriptor from the device.indexβ the index of the string in the string list.- Returns a C-style string if successful, or
undefon error.
get_descriptorβ retrieve a descriptor from the device.typeβ the type of descriptor to retrieve.indexβ the index of that descriptor in the list of descriptors of that type.
TODO: this method needs major rewrite to be Perl-ish. I need to provide a better way to specify the type (or at least document which are available), and I need to return a Perl data structure, not a buffer of binary data. π
get_descriptor_by_endpointβ retrieve an endpoint-specific descriptor from the device.epβ endpoint to query.typeβ the type of descriptor to retrieve.indexβ the index of that descriptor in the list of descriptors.bufβ buffer into which to write the requested descriptor.sizeβ max size to read into the buffer.
TODO: this method needs major rewrite to be Perl-ish. I need to provide a better way to specify the type (or at least document which are available), and I need to return a Perl data structure, not a buffer of binary data. π
bulk_readβ perform a bulk read request from the specified endpoint.epβ the number of the endpoint to readbytesβ buffer into which to write the requested data.sizeβ max size to read into the buffer.timeoutβ maximum time to wait (in milliseconds)- The function returns the number of bytes returned or
<0on error.
πΈ USB is packet based, not stream based. So usingbulk_read()to read part of the packet acts like a peek. The next time you read, all of the packet is still there. The data is only removed when you read the entire packet. For this reason, you should always callbulk_read()with the total packet size. noted, fr fr.
interrupt_readβ perform a interrupt read request from the specified endpoint.epβ the number of the endpoint to readbytesβ buffer into which to write the requested data.sizeβ max size to read into the buffer.timeoutβ maximum time to wait (in milliseconds)- The function returns the number of bytes returned or
<0on error.
bulk_writeβ perform a bulk write request to the specified endpoint.epβ the number of the endpoint to writebytesβ buffer from which to write the requested data.timeoutβ maximum time to wait (in milliseconds)- The function returns the number of bytes written or
<0on error.
interrupt_writeβ perform a interrupt write request to the specified endpoint.epβ the number of the endpoint to writebytesβ buffer from which to write the requested data.timeoutβ maximum time to wait (in milliseconds)- The function returns the number of bytes written or
<0on error.
get_driver_npβ this function returns the name of the driver bound to the interface specified by the parameterinterface.$interfaceβ the interface number of interest.- Returns
undefon error.
detach_kernel_driver_npβ this function will detach a kernel driver from the interface specified by parameterinterface. Applications using libusb can then try claiming the interface. Returns0on success or< 0on error. π€ (the_npis giving "not portable", just so you know)
π DIAGNOSTICS π
This is an explanation of the diagnostic and error messages this module can generate.
Cannot open device: reason stringβ unable to open the USB device for the reason given. π
π DEPENDENCIES π
This module depends on the Carp, Inline and Inline::C modules, as well as the strict and warnings pragmas. And obviously, libusb must be available, since that is the entire reason for the module's existence π like, why else would we even be here.
π AUTHOR π
G. Wade Johnson (gwadej at cpan dot org) Paul Archer (paul at paularcher dot org)
Houston Perl Mongers Group π
π BUGS π
Please report any bugs or feature requests to bug-device-usb@rt.cpan.org, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Device::USB>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. π«Ά we love a feedback loop.
π ACKNOWLEDGEMENTS π
Thanks go to various members of the Houston Perl Mongers group for input on the module. But thanks mostly go to Paul Archer who proposed the project and helped with the development. π§β¨
π COPYRIGHT & LICENSE π
Copyright 2006-2013 Houston Perl Mongers
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page, bestie π«Ά this one came from perl v5.42.3, dated 2026-08-19, and it's the Device...:Device(3pm) page in the User Contributed Perl Documentation section β go forth and talk to your USB devices, slay π
β¨πΈ
π¦ where this page comes from
- ubuntu stonking: libdevice-usb-perl 0.38-3build3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms