如何使用libusb将数据发送到node.js中的USB设备 [英] How to send data to USB device in node.js using libusb

查看:357
本文介绍了如何使用libusb将数据发送到node.js中的USB设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为了从设备获取温度数据,我应该像那样发送数据\ n'。

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

这是我的代码:

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)


推荐答案

你忘了打电话给interface.claim()。

You forgot to call interface.claim().

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

这篇关于如何使用libusb将数据发送到node.js中的USB设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆