Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ libc = "0.2"
[dev-dependencies]
regex = "1"
usb-ids = "0.2.2"
tokio = { version = "1", features = ["full"] }
49 changes: 49 additions & 0 deletions examples/read_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use rusb::{Context, UsbContext};

use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

const BUF_SIZE: usize = 64;

fn convert_argument(input: &str) -> u16 {
if input.starts_with("0x") {
return u16::from_str_radix(input.trim_start_matches("0x"), 16).unwrap();
}
u16::from_str_radix(input, 10)
.expect("Invalid input, be sure to add `0x` for hexadecimal values.")
}

#[tokio::main]
async fn main() {
let args: Vec<String> = std::env::args().collect();

if args.len() < 4 {
eprintln!("Usage: read_async <base-10/0xbase-16> <base-10/0xbase-16> <endpoint>");
return;
}

let vid = convert_argument(args[1].as_ref());
let pid = convert_argument(args[2].as_ref());
let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap();

let ctx = Context::new().expect("Could not initialize libusb");
let device = Arc::new(
ctx.open_device_with_vid_pid(vid, pid)
.expect("Could not find device"),
);

let timeout = Duration::from_secs(10);
let mut buffer = Vec::with_capacity(BUF_SIZE);

loop {
let (bytes, n) = device
.read_bulk_async(endpoint, buffer, timeout)
.await
.expect("Failed to submit transfer");

println!("Got data: {} {:?}", n, &bytes[..n]);

buffer = bytes;
}
}
Loading