thegecko/webusb

Sending and receiving data

Closed this issue · 1 comments

I'm new to Node JS, I've been trying to send and receive data but I'm always getting an error on the line below. Is there any sample code I can refer to? Thanks in advanced.

await selectedDevice.open();
^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

Node JS code

var USB = require("webusb").USB;
var usb = new USB();
usb.requestDevice({ filters: [{ vendorId: 0x0828 }] })
.then(selectedDevice => {
    await selectedDevice.open();

   if (selectedDevice.configuration === null)
   await selectedDevice.selectConfiguration(1);

   await selectedDevice.claimInterface(1);
   await selectedDevice.controlTransferOut({
    requestType: 'vendor',
    recipient: 'interface',
    request: 0x01,  // vendor-specific request: enable channels
    value: 0x0013,  // 0b00010011 (channels 1, 2 and 5)
    index: 0x0001   // Interface 1 is the recipient
    });

    let result = await selectedDevice.transferIn(1, 6);
    console.log("Result", result.status);
 })
.catch(error => { console.log(error); });

Looks like you have missed out an async on your execution block in the then() function.

I recommend you don't mix promises with async/await. To only use async/await, omit the then() e.g. something like:

let selectedDevice = await usb.requestDevice({ filters: [{ vendorId: 0x0828 }] });
await selectedDevice.open();
...

Closed as not related to library usage.