-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Device busy #100
Comments
I found that we cannot call device synchronous API in hot-plug events, so how can I do in rusb |
It's libusb limitation.
I think we must add this to documentation. You can add std::sync::mpsc::Sender in to |
I tried many times, but failed.
|
use rusb::{Context, Device, HotplugBuilder, UsbContext};
use std::sync::mpsc;
struct HotPlugHandler<T: UsbContext> {
sender: mpsc::Sender<Device<T>>,
}
impl<T: UsbContext> rusb::Hotplug<T> for HotPlugHandler<T> {
fn device_arrived(&mut self, device: Device<T>) {
println!("device arrived {:?}", device);
self.sender.send(device);
}
fn device_left(&mut self, device: Device<T>) {
println!("device left {:?}", device);
}
}
impl<T: UsbContext> Drop for HotPlugHandler<T> {
fn drop(&mut self) {
println!("HotPlugHandler dropped");
}
}
fn main() -> rusb::Result<()> {
if rusb::has_hotplug() {
let context = Context::new()?;
let (tx, rx) = mpsc::channel::<Device<Context>>();
let mut reg = Some(
HotplugBuilder::new()
.enumerate(true)
.register(&context, Box::new(HotPlugHandler { sender: tx }))?,
);
std::thread::spawn(move || loop {
let dev = rx.recv().unwrap();
let desc = dev.device_descriptor().unwrap();
println!("{:?}", desc);
});
loop {
context.handle_events(None).unwrap();
if let Some(reg) = reg.take() {
context.unregister_callback(reg);
break;
}
}
Ok(())
} else {
eprint!("libusb hotplug api unsupported");
Ok(())
}
} |
When I try to get the USB information, like
read_serial_number_string_ascii
in the Hotplug event, it prompts Device busy.error log
The text was updated successfully, but these errors were encountered: