0

I have a USB2CAN device plugged into my computer. hidapi-rs for Rust can't see this device, but when I use rusb, I can see the device, but I can't connect to it (NotSupported error).

The USB2CAN device VID is 32903 (u16) and the PID is 38 (u16).

Is there any other library that I can use to connect to the USB2CAN device so I can read and write data to and from it?

I have tried both hidapi and rusb to connect to the USB2CAN device.

Hidapi Code to connect to device

let api = hidapi::HidApi::new().unwrap();
// Print out information about all connected devices
for device in api.device_list() {
  // Filter out devices that are built into the laptop
  if device.product_id() != 12632 && device.product_id() != 131 && device.product_id() != 65261 {
    println!("{:#?}", device);
  }
  if device.vendor_id() == 32903 && device.product_id() == 38 {
    read_write_data(tx.clone(), rx.clone(), device, &api);
  }
}

fn read_write_data(tx: Sender<USBWD>, rx: Receiver<USBMSG>, device: &DeviceInfo, api: &HidApi) {
  println!("Device: {:?}", device);
  let d = api.open(device.vendor_id(), device.product_id()).unwrap();
  loop {
    // Read data from device
    println!("LOOP: {:?}", d);
    let mut buf = [0u8; 20];
    let res = d.read_timeout(&mut buf[..], 1000).unwrap();
    println!("Read: {:?}", &buf[..res]);
  } 
}

RUSB code

for device in rusb::devices().unwrap().iter() {
  let device_desc = device.device_descriptor().unwrap();

  println!("Bus {:03} Device {:03} ID {:04}:{:04}",
      device.bus_number(),
      device.address(),
      device_desc.vendor_id(),
      device_desc.product_id());
  let vid = 32903;
  let pid = 38;
  println!("Found device: {:?} , {:?}, {:?}", device_desc.vendor_id() == vid, device_desc.vendor_id(), vid);

  if device_desc.vendor_id() == vid && device_desc.product_id() == pid {
    // if device_desc.product_id() == 26 {
    println!("Connecting to device");
    let tx_clone = tx.clone();
    let rx_clone = rx.clone();
    read_write_data(tx_clone, rx_clone, device);
    // }
  }
}

fn read_write_data(tx: Sender<USBWD>, rx: Receiver<USBMSG>, device: Device<GlobalContext>) {
   let d = device.open().expect("Failed to open Device");
   loop {
     // Read data from device
     let mut buf: Vec<u8> = vec![];
     d.read_bulk( 0, &mut buf, 
     Duration::from_millis(100)).expect("Failed to read Message's");
     println!("Buffer: {:?}", buf);
  } 
}

0 Answers0