ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
165 stars 79 forks source link

HidApiError { message: "No HID devices with requested VID/PID found in the system." } #136

Closed junbong closed 7 months ago

junbong commented 7 months ago

I ran example code

fn main() {
    let api = hidapi::HidApi::new().unwrap();
    // Print out information about all connected devices
    for device in api.device_list() {
        println!("{:#?}", device);
    }

    // Connect to device using its VID and PID
    let (VID, PID) = (0x0123, 0x3456);
    let device = api.open(VID, PID).unwrap();

    // Read data from device
    let mut buf = [0u8; 8];
    let res = device.read(&mut buf[..]).unwrap();
    println!("Read: {:?}", &buf[..res]);

    // Write data to device
    let buf = [0u8, 1, 2, 3, 4];
    let res = device.write(&buf).unwrap();
    println!("Wrote: {:?} byte(s)", res);
}

But I got an error.

HidDeviceInfo {
    vendor_id: 1452,
    product_id: 591,
}
HidDeviceInfo {
    vendor_id: 1452,
    product_id: 591,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 9610,
    product_id: 4103,
}
HidDeviceInfo {
    vendor_id: 3468,
    product_id: 316,
}
thread 'main' panicked at src/main.rs:10:37:
called `Result::unwrap()` on an `Err` value: HidApiError { message: "No HID devices with requested VID/PID found in the system." }
stack backtrace:
   0: rust_begin_unwind
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1652:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1077:23
   4: assem_robot::main
             at ./src/main.rs:10:18
   5: core::ops::function::FnOnce::call_once
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I built the source on my Ubuntu 22.04 desktop.

ruabmbua commented 7 months ago

According to the output which lists all discovered devices, you do not have any device with the VID/PID you are trying to open, so this is expected.