node.js - 如何识别osx上USB设备的 "path"并从 Node createReadStream其数据?

标签 node.js usb hardware-interface

我想编写一个应用程序来重新映射 NodeJS 中的脚踏板的按键。我为我的脚踏板找到了一个类似的库;但是,它对设备的 USB 路径进行了硬编码,该路径适用于 Linux,而不是 OSX。这会引发错误:

failed to open file { Error: ENOENT: no such file or directory, open '/dev/usb/hiddev0'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/dev/usb/hiddev0'

问题: 如何识别我的设备的 USB 路径,以便我可以从 USB 设备创建数据的ReadStream。

终端命令:

system_profiler SPUSBDataType

输出:

VEC USB Footpedal:

              Product ID: 0x00ff
              Vendor ID: 0x05f3  (PI Engineering, Inc)
              Version: 1.20
              Speed: Up to 1.5 Mb/sec
              Manufacturer: VEC
              Location ID: 0x1d112000 / 9
              Current Available (mA): 500
              Current Required (mA): 100
              Extra Operating Current (mA): 0


#! /usr/bin/env node
'use strict';

const fs = require('fs');
const robot = require('robotjs');

const DEFAULT_DEVICE_PATH = '/dev/usb/hiddev0'; // this path needs to change
const DEFAULT_KEYS = ['a', 'b', 'c'];
const RETRY_INTERVAL = 5 * 1000;

const argv = require('yargs')
             .array('k')
             .alias('k', 'keys')
             .alias('p', 'path')
             .default('path', DEFAULT_DEVICE_PATH)
             .default('k', [])
             .argv;

const keyMap = DEFAULT_KEYS.map((key, i) => (argv.keys[i] || key));
const state = [ false, false, false ];

function updateState(index, value) {
  const previousState = state[index];
  const currentState = (value === 0x01);

  if (previousState !== currentState) {
    const key = keyMap[index];
    robot.keyToggle(key, currentState ? 'down' : 'up');
  }

  state[index] = currentState;
}

function openFile() {
  const stream = fs.createReadStream(argv.path); // so that I can read the stream here.
  const size = 8;
  const offset = 4;

  stream.on('data', function(chunk) {
    for (var i=0; i<chunk.length / size; i++) {
      updateState(i, chunk[i * size + offset]);
    }
  });

  stream.on('error', function(err) {
    console.log('failed to open file', err);
    setTimeout(openFile, RETRY_INTERVAL);
  });
}

openFile();

最佳答案

这是一个库,可为您提供 OSX 中任何 HID USB 设备的路径:

https://www.npmjs.com/package/node-hid

关于node.js - 如何识别osx上USB设备的 "path"并从 Node createReadStream其数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46351699/

相关文章:

c++ - 在不轮询 GetMessage() 的情况下检测 USB 连接/断开连接

javascript - 在 JSON.parse 函数中传递外部变量

javascript - 如何验证nestjs中的查询参数

node.js - mongoosejs 在方法内执行查询

android - 在 Android 中为 pH 测试仪读取毫伏

unit-testing - 如何集成/单元测试软件硬件接口(interface)

没有edittext View 的Android捕获硬件键盘事件

javascript - 服务器未在 Node/Express 中运行

c# - 使用 USB 激活 MOSFET/继电器

compiler-errors - 用于MPLAB的示例USB项目上的编译错误