cronologic-de / webusb

Configure a Raspberry Pi Pico using SCPI over WebUSB
Mozilla Public License 2.0
7 stars 1 forks source link

Implement WebUSB connection #6

Open scy opened 3 years ago

scy commented 3 years ago

Allow the frontend to connect to the device using WebUSB. No commands are being sent yet.

scy commented 3 years ago

This is causing trouble. According to the docs I could find online, something like this should work:

this.deviceState = this.DEVICE_CONNECTING;
let device;
try {
  device = await navigator.usb.requestDevice({ filters: [{
    classCode: 0x0a,  // CDC (Serial) device
  }]});
} catch (err) {
  console.error(err);
}
if (device === undefined) {
  this.deviceState = this.DEVICE_DISABLED;
} else {
  console.log('device:', device);
  this.device = device;
  this.deviceState = this.DEVICE_CONNECTED;
  try {
    console.log('open:', await device.open());
    console.log('selectConfiguration:', await device.selectConfiguration(1));
    console.log('claimInterface:', await device.claimInterface(2));
  } catch (err) {
    console.error(err);
  }
}

However, the claimInterface() does not return.

According to this StackOverflow answer, the reason might be that the device is already claimed by the operating system (in order to provide a serial port). Sounds reasonable. The way to fix this, apparently, is to set USB vendor & product IDs that are not already claimed by an existing driver.

In my case, the device’s VID/PID are 2e8a/000a (the Raspberry Pi Pico with its C SDK’s serial interface). I’ve also tried claiming the other interfaces (0 and 1) exposed by the device, which instead resulted in an error Unable to claim interface.

In order to be able to progress, I will probably implement the Web Serial API instead of WebUSB for this first proof of concept, because we’re doing SCPI over Serial anyway. Then, once we’re switching to WebUSB and USBTMC, we can deal with the issue.

Cc @sulimma, for your information.

scy commented 3 years ago

Committed my WIP code that tries to use the WebUSB API as 312dd01f8713783fc36f62482df2eebe9230e433 to the webusb-js-wip branch and will leave it there for now.