Dylan Buth
Dylan Buth

Reputation: 1666

How to find the functions you use to interact with USB device

Disclaimer: I'm very new to accessing USB devices from the web.

Reading through this doc, I've successfully connected to the device. My page has a button, the button brings up the window to connect to the device, it can then choose a configuration and claim an interface.

From what I understand, devices then have different capabilities based on what they do. How do I know what the device is capable of? I've searched through everything that logging the device gives me and can't find a list of methods or props or anything.

The device is an x-ray sensor so documentation is pretty scarce from what I've seen.

If I'm asking the wrong question, sorry. Again, very new to this. May be going about it the wrong way.

Code Snippet that gets me connected to device:

navigator.usb.requestDevice({ filters: [{ vendorId: 0x0547 }] })
                .then(device => {
                    console.log(device);
                })
            .catch(error => { console.log(error.message); });

Console Log Output

Upvotes: 3

Views: 1470

Answers (3)

Flavien Volken
Flavien Volken

Reputation: 21259

Assuming you did your homework of:

  • serving your page through https
  • usinf a browser supporting WebUSB
  • Running the following snippet within an action called from the user, i.e. typically a callback after the user clicked on a button.

You can get your usb device informations as follow:

async function onClick(){
  const vendorId = 0x0547; // your vendor id
  const device = await navigator.usb.requestDevice({ filters: [{vendorId}] });

  console.log(device.configurations); // <- this will print conf. with interfaces and endpoints per interface.

}

Upvotes: 1

Jordi
Jordi

Reputation: 21

Checkout this example (it's about setting up a USB thermal printer, it's not the same but it helped me): https://github.com/drffej/webusb.printer/blob/master/printer.html

I am in a similar position as you, I want to use a very specific hardware I have, and I don't have a clear idea on how to interact with it.

Regards,

Upvotes: 2

Ben
Ben

Reputation: 62336

I have never used USB connectivity, but from looking at the documentation for it and not seeing any ability to ask a device what it's capable of. I suspect interacting with the device might be comparable to a socket connection in that there aren't methods specific to that device but perhaps json that is sent/received once an interface is claimed. Then, controlTransferIn() and controlTransferOut() could probably be used to send/receive information to/from the device.

Take note that controlTransferIn/controlTransferOut both describe vendor-specific request / value properties.

I think the best path forward would be to contact the vendor directly for documentation on how to interact with their USB device. This would need to be done for any devices you wish to support. Also keep in mind from an architecture perspective in your code, abstraction is your friend.

Upvotes: 2

Related Questions