ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
173 stars 82 forks source link

Having issues with open_path #102

Closed Manul-Keyboard closed 1 year ago

Manul-Keyboard commented 1 year ago

Code:

use hidapi::HidApi;

fn main() {
    let r_id = 2;
    let msg = "test";
    let msg_bytes = msg.as_bytes();

    let api = HidApi::new().unwrap();

    const path: &str = "1-3:1.0";

    for dev in api.device_list() {
        let cstr_path = dev.path();
        let str_path: &str = cstr_path.to_str().unwrap();

        if str_path == path {
            let device = api.open_path(&cstr_path).unwrap();
            let mut package = vec![r_id];
            package.extend_from_slice(&msg_bytes);

            device.write(&package).unwrap();
        }
    }
}

Error: image The error was produced on Linux.

ruabmbua commented 1 year ago

If you are trying to open a specific interface of a device in a portable way, maybe this is of interest: https://github.com/ruabmbua/hidapi-rs/issues/101

ruabmbua commented 1 year ago

Note that the paths are not portable at all across operating systems, and more of an implementation detail. They are mostly useful, if you somehow already have the path you want to use. The "1-3:1.0" might only be e.g. for windows.

Also I just noticed that the legacy libusb backend has paths like this, but hidapi uses hidraw as the default now.

ruabmbua commented 1 year ago

Also a small tip: when filing issues, please do not include code snippets as pictures, rather use the proper code comments, or just plain text. Its not super important but it makes life a bit more pleasant for people trying to reproduce issues, because they can just copy the text. Also, for search purposes text is much better.

Manul-Keyboard commented 1 year ago

Thank you for your answer. I will use code comments in the future.