2

Im pretty much using the Nintendo Switch Joy-Con controllers demo which I've modified a little to make it work with my barcode scanner. And it just wont work and if it does work it works once in 100 site refreshes.

console.log = text => {
    log.textContent += `${text}\r\n`;
  };
  
  let device;
  
  if (!("hid" in navigator)) {
    console.log("WebHID is not available yet.");
  }
  
  navigator.hid.getDevices().then(devices => {
    if (devices.length == 0) {
      console.log(`No HID devices selected. Press the "request device" button.`);
      return;
    }
    device = devices[0];
    console.log(`User previously selected "${device.productName}" HID device.`);
    console.log(`Now press "open device" button to receive input reports.`);
  });
  
  requestDeviceButton.onclick = async event => {
    document.body.style.display = "none";
    try {
        const filters = [
            {
                vendorId: "8792", 
                productId: "9032"
            }
        ];
  
      [device] = await navigator.hid.requestDevice({ filters });
      if (!device) return;
  
      console.log(`User selected "${device.productName}" HID device.`);
      console.log(`Now press "open device" button to receive input reports.`);
    } finally {
      document.body.style.display = "";
    }
  };
  
  openButton.onclick = async event => {
    if (!device) return;
  
    await device.open();
    console.log(`Waiting for user to press button...`);
  
    device.addEventListener("inputreport", event => {
      const { data, device, reportId } = event;
  
      if (device.productId != "9032") return;
  
      const value = data.getUint8(0);
      if (value == 0) return;
  
   
      console.log(`Data: ${value}.`);
    });
  };

openButton.onclick event fires everytime i scan something with the barcode scanner. And because of it, it tries to do device.open() again everytime i scan something. And inputreport event wont fire at all.

Does anyone have any idea what causes this?

Heimot
  • 49
  • 1
  • 7
  • For example, first of all, why not investigate what kind of data is sent and received with a USB protocol analyzer or the like? And why not try to handle them with a program such as C++ instead of a web browser? – kunif May 31 '21 at 13:58
  • @kunif I have a web application online where i would like to be able to scan barcodes (which have ids inside them) and then it finds the ids data from backend and shows it on the frontend. – Heimot Jun 01 '21 at 11:37
  • If you're using it in a web application, it's best to use general keyboard input emulation rather than a weird proprietary protocol. – kunif Jun 01 '21 at 12:08
  • What is your barcode scanner device? Do you have any documentation on it? Have a look at https://web.dev/devices-introduction/ to see if there's another way to access this device. – François Beaufort Jun 03 '21 at 11:54

1 Answers1

1

Hey i switched to WEBUSB api and got it working after reinstalling winusb driver using zadig for the barcode scanner.

Here is the code im using rn. If anyone is interested. RFID function is launched by a button press.

const RFID = async () => {
try {
  const filters = [{
    vendorId: 0x1A86
  }];
  const device = await navigator.usb.requestDevice({ filters })

  const configuration_number = 1  // device.configuration.configurationValue
  const interface_number = 0      // device.configuration.interfaces[1].interfaceNumber
  const interface_class = 255      // device.configuration.interfaces[1].alternates[0].interfaceClass
  console.log(device);
  console.log(`configuration number :  ${configuration_number}`);
  console.log(`interface number : ${interface_number} `);
  console.log(`interface class : ${interface_class} `);

  await device.open();
  await device.selectConfiguration(configuration_number);
  await device.claimInterface(interface_number);
  await device.controlTransferOut({
    requestType: 'class',
    recipient: 'interface',
    request: 0x22,
    value: 0x10,
    index: interface_number
  });

  const read = async (device) => {
    const result = await device.transferIn(2, 64);
    const decoder = new TextDecoder();
    const message = decoder.decode(result.data);
    return message
  }

  var m
  do {
    m = await read(device)
    setQR(oldArr => [...oldArr, m])
    console.log(m)
  } while (m.charCodeAt(0) !== 13)

} catch (error) {
  console.log(error);
}}
Heimot
  • 49
  • 1
  • 7
  • 1
    Can you share code with WebUSB to see how it works? – François Beaufort Jun 10 '21 at 10:09
  • 1
    @François Beaufort I added the code which im using now. – Heimot Jun 11 '21 at 08:15
  • 1
    Thanks! What is the name of your device? – François Beaufort Jun 11 '21 at 08:32
  • 1
    @François Beaufort The barcode scanner which im using right now is Sunlux XL-9309B. – Heimot Jun 11 '21 at 09:46
  • Tried with Honeywell device and WebUSB. Gives this error configuration number : 1 honeywell_barcode_scanner.html:71 interface number : 0 honeywell_barcode_scanner.html:72 interface class : 255 honeywell_barcode_scanner.html:1 An attempt to claim a USB device interface has been blocked because it implements a protected interface class. @FrançoisBeaufort any suggestions? vendorId: 0x0C2E, productId: 0x0B81 – murkle Aug 19 '23 at 13:20
  • Got a Voyager 1400g working with Web Serial. You need to put it in CDC-ACM mode https://murkle.github.io/utils/webserial/honeywell_barcode_scanner.html – murkle Aug 19 '23 at 14:47