node-hid / node-hid

Access USB & Bluetooth HID devices through Node.js
BSD 3-Clause "New" or "Revised" License
1.49k stars 280 forks source link

'cannot open device with vendor id' when new device plug in #369

Closed saifeiLee closed 4 years ago

saifeiLee commented 4 years ago

Description

I plug in a new device and using usb-detect to monitor the changes. As expected, I can detect new device pluged in. image And I want to open the new HID device with its vid/pid. But I got the error.

Uncaught Exception:
Error: cannot open device with vendor id 0x1ff7 and product id 0xf05
    at new HID (/Users/waltermitty/Code/cvte/electron-easi-camera/app/node_modules/node-hid/nodehid.js:49:17)

Is there anything I went wrong? If you need and further info, please let me know and I will append as soon as possible.


Update

I append the code snippet and the output.

var HID = require('node-hid')
var devices = HID.devices()
var usbDetect = require('usb-detection');
usbDetect.startMonitoring();

// Detect add/insert
usbDetect.on('add', function (device) {
  try {
    console.log('add new device', device);
    const deviceInfo = devices.find((item) => {
      return item.productId === 3845 && item.vendorId === 8183
    })
    console.log('filtered device info:', deviceInfo)
    const openedDevice = new HID.HID(deviceInfo.path)
    console.log(openedDevice)
  } catch (error) {
    console.error(error)
  }
});

usbDetect.on('remove', function (device) { console.log('remove device:', device); });

output when I plug in the device:

add new device { locationId: 338821120,
  vendorId: 6720,
  productId: 257,
  deviceName: 'USB 2.0 Hub',
  manufacturer: '',
  serialNumber: '',
  deviceAddress: 46 }
filtered device info: { vendorId: 8183,
  productId: 3845,
  path:
   'IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS08@14300000/USB2.1 Hub@14300000/AppleUSB20Hub@14300000/AppleUSB20HubPort@14320000/USB 2.0 Hub@14320000/AppleUSB20Hub@14320000/AppleUSB20HubPort@14321000/SC03@14321000/IOUSBHostInterface@4/AppleUserUSBHostHIDDevice',
  serialNumber: '',
  manufacturer: 'Seewo',
  product: 'SC03',
  release: 26,
  interface: 4,
  usagePage: 65280,
  usage: 1 }
Error: cannot open device with path IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS08@14300000/USB2.1 Hub@14300000/AppleUSB20Hub@14300000/AppleUSB20HubPort@14320000/USB 2.0 Hub@14320000/AppleUSB20Hub@14320000/AppleUSB20HubPort@14321000/SC03@14321000/IOUSBHostInterface@4/AppleUserUSBHostHIDDevice
    at new HID (/Users/waltermitty/Code/node/my-event/node_modules/node-hid/nodehid.js:49:17)
    at EventEmitter.<anonymous> (/Users/waltermitty/Code/node/my-event/src/hid/detect.js:14:26)
    at EventEmitter.emit (/Users/waltermitty/Code/node/my-event/node_modules/eventemitter2/lib/eventemitter2.js:384:22)
    at /Users/waltermitty/Code/node/my-event/node_modules/usb-detection/index.js:69:12
add new device { locationId: 338825216,
  vendorId: 8183,
  productId: 3845,
  deviceName: 'SC03',
  manufacturer: 'Seewo',
  serialNumber: '',
  deviceAddress: 47 }
filtered device info: { vendorId: 8183,
  productId: 3845,
  path:
   'IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS08@14300000/USB2.1 Hub@14300000/AppleUSB20Hub@14300000/AppleUSB20HubPort@14320000/USB 2.0 Hub@14320000/AppleUSB20Hub@14320000/AppleUSB20HubPort@14321000/SC03@14321000/IOUSBHostInterface@4/AppleUserUSBHostHIDDevice',
  serialNumber: '',
  manufacturer: 'Seewo',
  product: 'SC03',
  release: 26,
  interface: 4,
  usagePage: 65280,
  usage: 1 }
Error: cannot open device with path IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS08@14300000/USB2.1 Hub@14300000/AppleUSB20Hub@14300000/AppleUSB20HubPort@14320000/USB 2.0 Hub@14320000/AppleUSB20Hub@14320000/AppleUSB20HubPort@14321000/SC03@14321000/IOUSBHostInterface@4/AppleUserUSBHostHIDDevice
    at new HID (/Users/waltermitty/Code/node/my-event/node_modules/node-hid/nodehid.js:49:17)
    at EventEmitter.<anonymous> (/Users/waltermitty/Code/node/my-event/src/hid/detect.js:14:26)
    at EventEmitter.emit (/Users/waltermitty/Code/node/my-event/node_modules/eventemitter2/lib/eventemitter2.js:384:22)
    at /Users/waltermitty/Code/node/my-event/node_modules/usb-detection/index.js:69:12

update#2

I tried to put HID.HID() into setTimout(cb, 5000), also failed. image

todbot commented 4 years ago

The device isn't being opened by another process, like before. :)

But really I think the problem may be that the devices variable is stale. You need to do devices = HID.devices() to pick up the updated device list after the device insertion. With the code you quote, devices contains the list of HID devices at the start of the code, not when add was called.

saifeiLee commented 4 years ago

The device isn't being opened by another process, like before. :)

But really I think the problem may be that the devices variable is stale. You need to do devices = HID.devices() to pick up the updated device list after the device insertion. With the code you quote, devices contains the list of HID devices at the start of the code, not when add was called.

Hah.. Pretty sure the device not been opened. In fact, I tried to call HID.devices() when device added. The weirdest part is the new-added device is not in the updated list.

Update

I tried with my mouse, same problem.

todbot commented 4 years ago

Depending on the OS, you will not see the device immediately on usb insertion. It takes time for the OS to hook up the HID driver to the USB device (this can take more than a second sometimes I've seen).

Try doing a setTimeout(function(){console.log(HID.devices()},3000) in your .on('add') and see if you see the device then.

saifeiLee commented 4 years ago

Depending on the OS, you will not see the device immediately on usb insertion. It takes time for the OS to hook up the HID driver to the USB device (this can take more than a second sometimes I've seen).

Try doing a setTimeout(function(){console.log(HID.devices()},3000) in your .on('add') and see if you see the device then.

Well. You're right. It is OS delay. This time I tried setTimeout works. But last time failed. Anyway, thank you so much! May you have a good day!