• This repository has been archived on 07/Mar/2023
  • Stars
    star
    371
  • Rank 115,103 (Top 3 %)
  • Language
    C++
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

List USB devices in system and detect changes on them.

🛑🛑 Deprecated 🛑🛑

This library is now deprecated. We recommend using usb for hotplug detection instead. It supports a wider range of platforms, using a more proven codebase.

For help migrating, refer to the documentation for usb


npm version Gitter

usb-detection

usb-detection allows you to listen for insert/remove events of USB devices on your system.

Changelog

Install

npm install usb-detection

Install for Electron

This module uses native extensions and needs to be compiled for your target version of Electron. Precompiled binaries for recent Node.js and Electron versions are built and published using prebuild and can be installed automatically using electron-rebuild.

See the Electron docs for using native modules to ensure your project is set up to correctly use the prebuilt binaries for your version of Electron.


If you run into the following error, here are the exact steps you can use:

detection.node was compiled against a different Node.js version using NODE_MODULE_VERSION 72. This version of Node.js requires NODE_MODULE_VERSION 80. Please try re-compiling or re-installing
  1. npm i electron-rebuild --save-dev
  2. ./node_modules/.bin/electron-rebuild

Usage

var usbDetect = require('usb-detection');

usbDetect.startMonitoring();

// Detect add/insert
usbDetect.on('add', function(device) { console.log('add', device); });
usbDetect.on('add:vid', function(device) { console.log('add', device); });
usbDetect.on('add:vid:pid', function(device) { console.log('add', device); });

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

// Detect add or remove (change)
usbDetect.on('change', function(device) { console.log('change', device); });
usbDetect.on('change:vid', function(device) { console.log('change', device); });
usbDetect.on('change:vid:pid', function(device) { console.log('change', device); });

// Get a list of USB devices on your system, optionally filtered by `vid` or `pid`
usbDetect.find(function(err, devices) { console.log('find', devices, err); });
usbDetect.find(vid, function(err, devices) { console.log('find', devices, err); });
usbDetect.find(vid, pid, function(err, devices) { console.log('find', devices, err); });
// Promise version of `find`:
usbDetect.find().then(function(devices) { console.log(devices); }).catch(function(err) { console.log(err); });

// Allow the process to exit
//usbDetect.stopMonitoring()

API

usbDetect.startMonitoring()

Start listening for USB add/remove/change events. This will cause the Node.js process to stay open until you call usbDetect.stopMonitoring() (see below).

usbDetect.stopMonitoring()

Stop listening for USB add/remove/change events. This will also allow the Node.js process to exit.

This is really only meant to be called once on exit. No guarantees if you start/stop monitoring multiple times, see #53

usbDetect.on(eventName, callback)

  • eventName
    • add: also aliased as insert
      • add:vid
      • add:vid:pid
    • remove
      • remove:vid
      • remove:vid:pid
    • change
      • change:vid
      • change:vid:pid
  • callback: Function that is called whenever the event occurs
    • Takes a device
var usbDetect = require('usb-detection');
usbDetect.startMonitoring();

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

/* Console output:
{
	locationId: 0,
	vendorId: 5824,
	productId: 1155,
	deviceName: 'Teensy USB Serial (COM3)',
	manufacturer: 'PJRC.COM, LLC.',
	serialNumber: '',
	deviceAddress: 11
}
*/

usbDetect.find(vid, pid, callback)

Note: All find calls return a promise even with the node-style callback flavors.

  • find()
  • find(vid)
  • find(vid, pid)
  • find(callback)
  • find(vid, callback)
  • find(vid, pid, callback)

Parameters:

  • vid: restrict search to a certain vendor id
  • pid: restrict search to s certain product id
  • callback: Function that is called whenever the event occurs
    • Takes a err and devices parameter.
var usbDetect = require('usb-detection');
usbDetect.startMonitoring();

usbDetect.find(function(err, devices) {
	console.log(devices, err);
});
// Equivalent to:
//		usbDetect.find().then(function(devices) { console.log(devices); }).catch(function(err) { console.log(err); });

/* Console output:
[
	{
		locationId: 0,
		vendorId: 0,
		productId: 0,
		deviceName: 'USB Root Hub',
		manufacturer: '(Standard USB Host Controller)',
		serialNumber: '',
		deviceAddress: 2
	},
	{
		locationId: 0,
		vendorId: 5824,
		productId: 1155,
		deviceName: 'Teensy USB Serial (COM3)',
		manufacturer: 'PJRC.COM, LLC.',
		serialNumber: '',
		deviceAddress: 11
	}
]
*/

FAQ

The script/process is not exiting/quiting

var usbDetect = require('usb-detection');

// Do some detection
usbDetect.startMonitoring();

// After this call, the process will be able to quit
usbDetect.stopMonitoring();

usbDetect.find() always returns the same list of devices, even after removal.

Make sure you call usbDetect.startMonitoring() before any calls to usbDetect.find().

npm run rebuild -> The system cannot find the path specified.

If you are running into the The system cannot find the path specified. error when running npm run rebuild, make sure you have Python installed and on your PATH.

You can verify node-gyp is configured correctly by looking at the output of node-gyp configure --verbose.

To build a debug version with error outputs use:

$ npm run rebuild --debug

Development (compile from source)

This assumes you also have everything on your system necessary to compile ANY native module for Node.js using node-gyp. This may not be the case, though, so please ensure the following requirements are satisfied before filing an issue about "does not install".

If you are developing locally, you should use Node.js v14, but if you are just trying to install usb-detection, you should be able to compile from source using any supported version of Node.

Windows

See node-gyp's Windows installation instructions.

macOS

See node-gyp's macOS installation instructions.

Linux

You know what you need for you system, basically your appropriate analog of build-essential. Keep rocking! See node-gyp's Unix installation instructions for more details.

sudo apt-get install build-essential

You will also need to install libudev-dev.

sudo apt-get install libudev-dev

Testing

We have a suite of Mocha/Chai tests.

The tests require some manual interaction of plugging/unplugging a USB device. Follow the cyan background text instructions.

npm test

More Repositories

1

postcss-css-variables

PostCSS plugin to transform CSS Custom Properties(CSS variables) syntax into a static representation
JavaScript
541
star
2

postcss-increase-specificity

Why? Dealing with CSS you can't remove(mainly from a 3rd party). Increases specificity of selectors.
JavaScript
51
star
3

svg-curve-lib

Bezier Curve and Elliptical Arc implementations - SVG Path commands
C
46
star
4

gulp-css-spriter

Sprite Sheet Generation from CSS source files. The best and different approach to sprite sheets.
JavaScript
38
star
5

FP-V-GA-Text

A simple to use VHDL module to display text on VGA display.
VHDL
34
star
6

Radius

Complete Unity Reference Project - Multiplayer, UI
JavaScript
26
star
7

C4D-Py-Utils

Cinema 4D Python scripts designed for random purposes
Python
22
star
8

VHDL-Pong

Straightforward Pong Game written in VHDL. Scoring and Multiplayer
VHDL
20
star
9

vga-simulator

JavaScript
14
star
10

markdown-icons

Easily display icon fonts in markdown.
Python
13
star
11

emoji-unicode-version

Get the unicode version for a given emoji name
JavaScript
9
star
12

sdp-blob

Turn a WebRTC Offer/Answer into a compact blob/token
JavaScript
9
star
13

macchiato

Mocha/Chai inspired C++ test framework for desktop and Arduino (BDD)
C++
8
star
14

gulp-image-diff

Image diff'ing tool that compares pixel by pixel.
JavaScript
8
star
15

airflow-fluid-simulator

Create airflow heat or fluid animation/diagram by painting a vector-field
JavaScript
8
star
16

postcss-reverse-media

Reverse/Invert media query parameters
JavaScript
4
star
17

STM32F3-Mouse

A HID Mouse Project for the STM32F3 Discovery board.
4
star
18

zig-neural-networks

A from scratch neural network library in Zig
Zig
3
star
19

zig-ocr-neural-network

A from scratch neural network implementation in Zig, trained against the MNIST dataset to recognize handwritten digits.
Zig
3
star
20

Super-Bounce

Super Bouncing Game just like Halo 2 - GDSE Game Jam
JavaScript
3
star
21

postcss-raw

Protect nodes inside @raw at-rules from being touched by other plugins.
JavaScript
3
star
22

render-components-demo

Awesome Module Bundling/Packaging concept!
JavaScript
2
star
23

irccloud-layout-demo

Responsive CSS-only experiment to combine the IRCCloud website and native Android app
CSS
2
star
24

zig-ocr-mnist-k-nearest-neighbors

Basic OCR example written in Zig using K-nearest neighbor against the MNIST dataset
Zig
1
star
25

Traces

HTML5 Browser Game - GDSE Anniversary Game Jam 2014 Project
JavaScript
1
star
26

matrix-spam-scripts

Quick and dirty scripts to find and cleanup spam in Matrix rooms
JavaScript
1
star
27

emoji-add-unicode-version

DEPRECATED: See https://github.com/MadLittleMods/emoji-unicode-version
JavaScript
1
star
28

postcss-fallback

Provide fallback values for properties without having duplicate declarations with PostCSS
JavaScript
1
star
29

KmeansImage

Project to find the Dominant Color of any Image using K-means Clustering
PHP
1
star
30

jquery-carouselss

jQuery Carousel plugin for HTML content/images. Listens to CSS transitions/animations when switching frames. CSS class-based states.
JavaScript
1
star
31

crispin-mulberry-fe-nat

Crispin & Mulberry - Nerdery FE NAT
JavaScript
1
star
32

postcss-selector-scope-utility

WIP
JavaScript
1
star
33

linux-notes

1
star
34

fps-aim-analyzer

WIP
Zig
1
star
35

tensorflow-demo-in-browser

Tensorflow with custom model to detect my pet bunny/rabbit
JavaScript
1
star