NodeJS 如何在USB设备中指定USB大容量存储?

csga3l58  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(216)

我正在为用户提供一个将数据传输到USB的功能,我正在使用'USB'包。如果我做const devices = getDeviceList();,然后我得到所有设备信息连接通过USB,如鼠标和键盘。但是,我应该只指定USB大容量存储。在这种情况下,我如何指定它?任何建议总是欢迎和提前感谢你!
仅供参考:我试图指定它,但console.log("usb device is: ", usbDevice);给了我null
下面是我的代码:

import {usb, getDeviceList} from "usb";
import fs from "fs";
import path from "path";

async function path2usb(selectedMeasures) {
  // Assuming selectedMeasures is an array of data to be exported
  try {
    usb.on("attach", function (device) {
      console.log("USB device attached:");
      console.log(device);
    });

    usb.on("detach", function (device) {
      console.log("USB device detached:");
      console.log(device);
    });

    // Get a list of USB devices
    const devices = getDeviceList();
    console.log("devices are: ", devices);
    // Find the first USB mass storage device (e.g., a USB stick)
    let usbDevice = null;
    for (const device of devices) {
      const descriptor = device.deviceDescriptor;
      if (
        descriptor &&
        descriptor.bDeviceClass === 0x08 && // USB mass storage class
        descriptor.bDeviceSubClass === 0x06 && // USB mass storage subclass
        (descriptor.bDeviceProtocol === 0x50 || descriptor.bDeviceProtocol === 0x01) // Bulk-only transport or SCSI transparent command set
      ) {
        usbDevice = device;
        break; // Stop iterating once the USB stick is found
      }
    }
    console.log("usb device is: ", usbDevice);
    if (usbDevice) {
      usbDevice.open();
      const usbInterface = usbDevice.interface(0);
      usbInterface.claim();

      const endpoint = usbInterface.endpoint(0x81); // Assuming the endpoint is 0x81, you may need to check the endpoint address for your specific USB device.

      if (endpoint) {
        const folderName = selectedMeasures.join("_"); // Combine the selected measures to form the folder name
        const targetFolderPath = path.join("/data", folderName); // Create the target folder path in the 'data' directory

        // Check if the folder exists
        if (!fs.existsSync(targetFolderPath)) {
          throw new Error("Folder not found in 'data' directory.");
        }

        const targetFilePath = path.join(
          `/dev/${endpoint.deviceDescriptor.iSerialNumber}`,
          `${folderName}.txt`
        ); // Create the target file path in the USB mass storage device

        // Write the exported data to the file
        fs.writeFileSync(targetFilePath, selectedMeasures.join("\n"));

        console.log(`Data exported to USB-drive: ${targetFilePath}`);
        return targetFilePath;
      } else {
        // If the endpoint is not found, throw an error
        throw new Error("USB endpoint not found!");
      }
    } else {
      // If no USB mass storage device is found, throw an error
      throw new Error("No USB Mass Storage device found!");
    }
  } catch (error) {
    console.error("Error exporting data to USB drive:", error.message);
    throw error;
  }
}

export { path2usb };

字符串
这是我从console.log得到的:x1c 0d1x的数据


w6mmgewl

w6mmgewl1#

由于您似乎正在使用Linux,我建议您查看/dev/disk/by-label中的符号链接。然后,您可以使用libudev查看这些设备的父设备,并查看是否有任何父设备是USB设备。libudev库是Linux上广泛使用的C库,可能有一个node.js绑定。
作为概念证明,在编写任何代码之前,只需运行ls -l /dev/disk/by-label,然后运行udevadm info --attribute-walk /dev/disk/by-label/NAME(其中NAME是您在目录列表中看到的名称)。在该命令的输出中查找usb

相关问题