简体   繁体   English

如何在osx上识别USB设备的“路径”并从节点createReadStream读取其数据?

[英]How to identify the “path” of USB device on osx and createReadStream its data from node?

I want to write an applications to remap the keys of this foot pedal I have in NodeJS. 我想编写一个应用程序来重新映射NodeJS中此脚踏板的按键。 I found a similar library for my footpedal; 我为我的脚踏板找到了一个类似的图书馆。 however, it hardcodes the USB path for the device which is for linux not OSX. 但是,它硬编码用于Linux而不是OSX的设备的USB路径。 which throws an error: 这会引发错误:

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'

Question: How can I identify the usb path for my device so that I can createReadStream of data from USB device. 问题:如何识别设备的USB路径,以便可以从USB设备创建数据的ReadStream。

terminal command: 终端命令:

system_profiler SPUSBDataType

Output: 输出:

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();

Here is a library that will give you Path for any HID usb device in OSX: 这是一个库,可为您提供OSX中任何HID usb设备的路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM