javascript - 如何检测使用 Node.js 连接的 USB 设备

标签 javascript node.js meteor serial-port

我是 Node.js 的新手。我想检测是否有任何 USB/大容量存储设备连接到系统。

因为我们在 C# 中有一些事情要做,比如

// Add USB plugged event watching
        watcherAttach = new ManagementEventWatcher();
        watcherAttach.EventArrived += watcherAttach_EventArrived;
        watcherAttach.Query = new WqlEventQuery("SELECT * FROM  Win32_DeviceChangeEvent WHERE EventType = 2");
        watcherAttach.Start();

        // Add USB unplugged event watching
        watcherDetach = new ManagementEventWatcher();
        watcherDetach.EventArrived += watcherDetach_EventArrived;
        watcherDetach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
        watcherDetach.Start();

请建议我们如何在 Node.js 中做类似 C# 的事情。

最佳答案

更好的解决方案是使用“usb-detection”https://www.npmjs.com/package/usb-detection

您可以通过 productId 或 vendorId 过滤监听特定的 USB 设备:

// 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); });

关于javascript - 如何检测使用 Node.js 连接的 USB 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843946/

相关文章:

javascript - 数据完全渲染时的 React/Meteor 初始化函数

javascript - IronRouter waitOn、数据和 onAfterAction 困惑

javascript - 如何在 jquery 中创建间隔一小时的时间数组?

javascript - 从soundcloud获取歌曲信息,有歌曲id

javascript - 格式化text()的字符串结果

mysql - node.js/express/mysql/serialport/socket.io

javascript - 首先返回函数的一部分,然后执行其余部分

javascript - 是否可以在不初始化类的情况下访问类属性?

node.js - request() 之后 .then() 的替代方法

node.js - 从 mongoose findOne 查询中获取子文档的 _id