Open In App
Related Articles

How to access USB devices from node-webkit ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Node-webkit is a popular tool for creating desktop applications using web technologies such as HTML, CSS, and JavaScript. However, it can be challenging to access USB devices from a node-webkit application. In this context, we will explore different approaches to accessing USB devices from a node-webkit application. 

To access USB devices from node-webkit, you can use the Node.js `usb` package, which provides a Node.js API for communicating with USB devices.

Approach 1: Node.js usb package: To use the Node.js usb package, which provides a Node.js API for communicating with USB devices.

To use the usb package, you need to install it first by running the following command:

npm install usb

To access the USB device the syntax is:

const usb = require('usb');
const device = usb.findByIds(vid, pid);

device.open();
device.controlTransfer(bmRequestType, 
        bRequest, wValue, wIndex, data, callback);
device.close();

Accessing USB devices requires special permissions on some operating systems, such as macOS and Linux. You may need to add a udev rule on Linux or create a custom Info.plist file on macOS to grant your node-webkit application access to USB devices.

A detailed example is given to properly understand the use of this method.

Example 1: List all connected USB devices.

Javascript

const usb = require('usb');
  
const devices = usb.getDeviceList();
  
devices.forEach((device) => {
    console.log(`Device: ${device.deviceDescriptor.idVendor}:
                         ${device.deviceDescriptor.idProduct}`);
    console.log(`  Type: ${device.deviceDescriptor.iProduct}`);
    console.log(`  Bus: ${device.busNumber}`);
    console.log(`  Address: ${device.deviceAddress}`);
    console.log(`  Ports: ${device.portNumbers}`);
    console.log(`  Manufacturer: 
        ${device.deviceDescriptor.iManufacturer}`);
    console.log(`  Serial Number: 
        ${device.deviceDescriptor.iSerialNumber}`);
});

                    

Explanation: This code uses the usb package to get a list of all connected USB devices using the getDeviceList() method. Then, it iterates over each device in the list and logs information about each device to the console, including the vendor ID, product ID, device type, bus number, device address, port numbers, manufacturer, and serial number.

Output:

 

This output shows information about three connected USB devices: an OpenAI device, a wireless mouse, and a USB3.0 hub.

Example 2: Send a control transfer request to a USB device

Javascript

const usb = require('usb');
  
const VENDOR_ID = 0x1234;
const PRODUCT_ID = 0x5678;
const bmRequestType = 0x40;
const bRequest = 0x01;
const wValue = 0x0100;
const wIndex = 0x0000;
const data = Buffer.from([0x01, 0x02]);
  
const device = usb.findByIds(VENDOR_ID, PRODUCT_ID);
  
device.open();
  
device.controlTransfer(bmRequestType, bRequest, 
                       wValue, wIndex, data, 
                       (error, response) => {
    if (error) {
        console.log(`Error: ${error}`);
    } else {
        console.log(`Response: ${response}`);
    }
  
    device.close();
});

                    

Explanation: This code uses the usb package to find a USB device with a specific vendor ID and product ID using the findByIds() method. Then, it opens the device using the open() method and sends a control transfer request to the device using the controlTransfer() method. The control transfer request has a request type of 0x40, a request code of 0x01, a value of 0x0100, and a data buffer containing the bytes [0x01, 0x02]. The response from the control transfer request is logged to the console. Finally, the device is closed using the close() method.

Output:

 

This output shows the response from the control transfer request, which is a buffer containing the bytes [0x01, 0x02].

Approach 2: Using a Javascript Library

To use a JavaScript library like usb.js or webusb.js. usb.js is a JavaScript library that provides an API for accessing USB devices from a web application. It is a wrapper around the WebUSB API and provides a simple and consistent interface for interacting with USB devices.

Use this to include the js library.

<script src="https://cdnjs.cloudflare.com/ajax/libs/usb/1.4.0/usb.js"></script>

The code to list all the connected usb devices.

Javascript

usb.getDeviceList().then((devices) => {
    const deviceList = document.getElementById('devices');
    devices.forEach((device) => {
        const deviceItem = document.createElement('li');
        deviceItem.textContent = `${device.deviceDescriptor.idVendor}:
                                  ${device.deviceDescriptor.idProduct}`;
        deviceList.appendChild(deviceItem);
    });
}).catch((error) => {
    console.log(`Error: ${error}`);
});

                    

Example: Now, add this code in your HTML file 

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>List USB Devices</title>
    <script src=
    </script>
</head>
  
<body>
    <h1>List USB Devices</h1>
    <ul id="devices"></ul>
    <script>
        usb.getDeviceList().then((devices) => {
            const deviceList = document.getElementById('devices');
            devices.forEach((device) => {
                const deviceItem = document.createElement('li');
                deviceItem.textContent = `${device.deviceDescriptor.idVendor}:
                                          ${device.deviceDescriptor.idProduct}`;
                deviceList.appendChild(deviceItem);
            });
        }).catch((error) => {
            console.log(`Error: ${error}`);
        });
    </script>
</body>
</html>

                    

Output:

 

Conclusion: usb.js provides a simple and consistent API for accessing USB devices from a web application. It makes it easy to find devices, open them, claim interfaces, and send and receive data using endpoints. 



Last Updated : 07 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads