javascript 如何使用 libusb 将数据发送到 node.js 中的 USB 设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22695614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:35:36  来源:igfitidea点击:

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

javascriptnode.jsusblibusblibusb-1.0

提问by Alex Skakun

I have try to get data from device (USB thermometer), following this docs https://www.npmjs.org/package/usb, but I have not any result.

我尝试按照此文档https://www.npmjs.org/package/usb从设备(USB 温度计)获取数据,但没有任何结果。

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

为了从设备获取温度数据,我应该发送像“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)

回答by user3621456

You forgot to call interface.claim().

您忘记调用 interface.claim()。

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

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

回答by Zenkylo

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

接口应该使用term.interface(interfaceId)方法来声明,而不是term.interfaces[0]

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

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

回答by Bhoopesh Singh

Use sudo before running your command.

在运行命令之前使用 sudo。

Wrong:

错误的:

node index.js

Right:

对:

sudo node inex.js