How to send data to USB device in node.js using libusb
Asked Answered
R

3

11

I have try to get data from device (USB thermometer), following this documentation, but I have not any result.

For getting themperature data from device, I should send the data like that 'd\n'.

This is my code:

var usb = require('usb'),
    term = usb.findByIds(65535, 2);

term.open();

var endpoints = term.interfaces[0].endpoints,
    inEndpoint = endpoints[0],
    outEndpoint = endpoints[1];

inEndpoint.transferType = 2;
inEndpoint.startStream(1, 64);
inEndpoint.transfer(64, function (error, data) {
    if (!error) {
        console.log(data);
    } else {
        console.log(error);
    }
});
inEndpoint.on('data', function (data) {
    console.log(data);
});
inEndpoint.on('error', function (error) {
    console.log(error);
});
outEndpoint.transferType = 2;
outEndpoint.startStream(1, 64);
outEndpoint.transfer(new Buffer('d\n'), function (err) {
    console.log(err);
});

After running I have got this:

D:\Dev\USBTest\node_modules\usb\usb.js:261
    t.submit(new Buffer(self.streamTransferSize), transferDone)
      ^
Error: LIBUSB_ERROR_NOT_FOUND
    at startTransfer (D:\Dev\USBTest\node_modules\usb\usb.js:261:5)
    at Array.forEach (native)
    at InEndpoint.startStream (D:\Dev\USBTest\node_modules\usb\usb.js:264:23)
    at D:\Dev\USBTest\app.js:15:16
    at Object.<anonymous> (D:\Dev\USBTest\app.js:35:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
Richella answered 27/3, 2014 at 17:59 Comment(0)
D
13

You forgot to call interface.claim().

https://github.com/nonolith/node-usb#claim

Derange answered 9/5, 2014 at 17:53 Comment(0)
P
0

The interface should be declared using the term.interface(interfaceId) method, not term.interfaces[0]

https://www.npmjs.com/package/usb#interfaceinterface

Punchboard answered 17/7, 2019 at 15:54 Comment(0)
B
-10

Use sudo before running your command.

Wrong:

node index.js

Right:

sudo node inex.js
Bobbery answered 4/8, 2018 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.