Javascript 如何使用libusb将数据发送到node.js中的USB设备
我尝试从设备(USB温度计)获取数据,如下所示,但没有任何结果 为了从设备获取温度数据,我应该发送类似“d\n”的数据 这是我的代码:Javascript 如何使用libusb将数据发送到node.js中的USB设备,javascript,node.js,usb,libusb,libusb-1.0,Javascript,Node.js,Usb,Libusb,Libusb 1.0,我尝试从设备(USB温度计)获取数据,如下所示,但没有任何结果 为了从设备获取温度数据,我应该发送类似“d\n”的数据 这是我的代码: var usb = require('usb'), term = usb.findByIds(65535, 2); term.open(); var endpoints = term.interfaces[0].endpoints, inEndpoint = endpoints[0], outEndpoint = endpoints[
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);
});
运行后,我得到以下信息:
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)
D:\Dev\USBTest\node\u modules\usb\usb.js:261
t、 提交(新缓冲区(self.streamTransferSize),transferDone)
^
错误:找不到LIBUSB\u错误
开始传输时(D:\Dev\USBTest\node\u modules\usb\usb.js:261:5)
at Array.forEach(本机)
在InEndpoint.startStream(D:\Dev\USBTest\node\u modules\usb\usb.js:264:23)
在D:\Dev\USBTest\app.js:15:16
反对。(D:\Dev\USBTest\app.js:35:2)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Function.Module.runMain(Module.js:497:10)
您忘记调用interface.claim()
在运行命令之前使用sudo 错:
node index.js
对:
sudo node inex.js
应该使用
term.interface(interfaceId)
方法声明接口,而不是term.interfaces[0]