Struct hidapi_rusb::DeviceInfo
source · pub struct DeviceInfo { /* private fields */ }
Expand description
Device information. Use accessors to extract information about Hid devices.
Note: Methods like serial_number()
may return None, if the conversion to a
String failed internally. You can however access the raw hid representation of the
string by calling serial_number_raw()
Implementations§
source§impl DeviceInfo
impl DeviceInfo
pub fn path(&self) -> &CStr
sourcepub fn vendor_id(&self) -> u16
pub fn vendor_id(&self) -> u16
Examples found in repository?
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn test_lt() -> Rc<HidDevice> {
let api = HidApi::new().expect("Hidapi init failed");
let mut devices = api.device_list();
let dev_info = devices
.nth(0)
.expect("There is not a single hid device available");
let dev = Rc::new(
api.open(dev_info.vendor_id(), dev_info.product_id())
.expect("Can not open device"),
);
let dev_1 = dev.clone();
requires_static_lt_bound(move || {
println!("{}", dev_1.check_error().unwrap()); //<! Can be captured by closure with static lt
});
dev //<! Can be returned from a function, which exceeds the lifetime of the API context
}
More examples
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn run() -> Result<(), HidError> {
let hidapi = HidApi::new()?;
let device_info = hidapi
.device_list()
.next()
.expect("No devices are available!")
.clone();
println!(
"Opening device:\n VID: {:04x}, PID: {:04x}\n",
device_info.vendor_id(),
device_info.product_id()
);
let device = device_info.open_device(&hidapi)?;
let mut buf = vec![0; 64];
println!("Reading data from device ...\n");
loop {
let len = device.read(&mut buf)?;
println!("{:?}", &buf[..len]);
}
}
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("Printing all available hid devices:");
match HidApi::new() {
Ok(api) => {
for device in api.device_list() {
println!(
"VID: {:04x}, PID: {:04x}, Serial: {}, Product name: {}",
device.vendor_id(),
device.product_id(),
match device.serial_number() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
},
match device.product_string() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
}
);
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
sourcepub fn product_id(&self) -> u16
pub fn product_id(&self) -> u16
Examples found in repository?
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn test_lt() -> Rc<HidDevice> {
let api = HidApi::new().expect("Hidapi init failed");
let mut devices = api.device_list();
let dev_info = devices
.nth(0)
.expect("There is not a single hid device available");
let dev = Rc::new(
api.open(dev_info.vendor_id(), dev_info.product_id())
.expect("Can not open device"),
);
let dev_1 = dev.clone();
requires_static_lt_bound(move || {
println!("{}", dev_1.check_error().unwrap()); //<! Can be captured by closure with static lt
});
dev //<! Can be returned from a function, which exceeds the lifetime of the API context
}
More examples
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn run() -> Result<(), HidError> {
let hidapi = HidApi::new()?;
let device_info = hidapi
.device_list()
.next()
.expect("No devices are available!")
.clone();
println!(
"Opening device:\n VID: {:04x}, PID: {:04x}\n",
device_info.vendor_id(),
device_info.product_id()
);
let device = device_info.open_device(&hidapi)?;
let mut buf = vec![0; 64];
println!("Reading data from device ...\n");
loop {
let len = device.read(&mut buf)?;
println!("{:?}", &buf[..len]);
}
}
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("Printing all available hid devices:");
match HidApi::new() {
Ok(api) => {
for device in api.device_list() {
println!(
"VID: {:04x}, PID: {:04x}, Serial: {}, Product name: {}",
device.vendor_id(),
device.product_id(),
match device.serial_number() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
},
match device.product_string() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
}
);
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
sourcepub fn serial_number(&self) -> Option<&str>
pub fn serial_number(&self) -> Option<&str>
Try to call serial_number_raw()
, if None is returned.
Examples found in repository?
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("Printing all available hid devices:");
match HidApi::new() {
Ok(api) => {
for device in api.device_list() {
println!(
"VID: {:04x}, PID: {:04x}, Serial: {}, Product name: {}",
device.vendor_id(),
device.product_id(),
match device.serial_number() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
},
match device.product_string() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
}
);
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
pub fn serial_number_raw(&self) -> Option<&[wchar_t]>
pub fn release_number(&self) -> u16
sourcepub fn manufacturer_string(&self) -> Option<&str>
pub fn manufacturer_string(&self) -> Option<&str>
Try to call manufacturer_string_raw()
, if None is returned.
pub fn manufacturer_string_raw(&self) -> Option<&[wchar_t]>
sourcepub fn product_string(&self) -> Option<&str>
pub fn product_string(&self) -> Option<&str>
Try to call product_string_raw()
, if None is returned.
Examples found in repository?
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("Printing all available hid devices:");
match HidApi::new() {
Ok(api) => {
for device in api.device_list() {
println!(
"VID: {:04x}, PID: {:04x}, Serial: {}, Product name: {}",
device.vendor_id(),
device.product_id(),
match device.serial_number() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
},
match device.product_string() {
Some(s) => s,
_ => "<COULD NOT FETCH>",
}
);
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
pub fn product_string_raw(&self) -> Option<&[wchar_t]>
pub fn usage_page(&self) -> u16
pub fn usage(&self) -> u16
pub fn interface_number(&self) -> i32
sourcepub fn open_device(&self, hidapi: &HidApi) -> HidResult<HidDevice>
pub fn open_device(&self, hidapi: &HidApi) -> HidResult<HidDevice>
Use the information contained in DeviceInfo
to open
and return a handle to a HidDevice.
By default the device path is used to open the device. When no path is available, then vid, pid and serial number are used. If both path and serial number are not available, then this function will fail with HidError::OpenHidDeviceWithDeviceInfoError.
Note, that opening a device could still be done using HidApi::open() directly.
Examples found in repository?
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn run() -> Result<(), HidError> {
let hidapi = HidApi::new()?;
let device_info = hidapi
.device_list()
.next()
.expect("No devices are available!")
.clone();
println!(
"Opening device:\n VID: {:04x}, PID: {:04x}\n",
device_info.vendor_id(),
device_info.product_id()
);
let device = device_info.open_device(&hidapi)?;
let mut buf = vec![0; 64];
println!("Reading data from device ...\n");
loop {
let len = device.read(&mut buf)?;
println!("{:?}", &buf[..len]);
}
}
Trait Implementations§
source§impl Clone for DeviceInfo
impl Clone for DeviceInfo
source§fn clone(&self) -> DeviceInfo
fn clone(&self) -> DeviceInfo
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more