【问题标题】:Is there an equivalent to the lsusb command using System.IO.Ports in .NET 6?在 .NET 6 中是否有与使用 System.IO.Ports 的 lsusb 命令等效的命令?
【发布时间】:2021-12-29 19:12:58
【问题描述】:

我正在 Visual Studio .NET 6.0 的 Raspberry Pi OS Lite 上开发应用程序,需要读取 USB 端口。

当我通过 SSH 连接到 Raspberry Pi 4.0B 目标并输入 lsusb 命令时,我看到以下列表:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 0c76:161e JMTek, LLC. USB PnP Audio Device Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

我想使用System.IO.Ports 找到插入JMTek, LLC. USB Audio Device Hub 的USB 端口。

我尝试了下面的代码:

   public void EnumPorts()
    {
        private string[]? portNames;

        portNames = SerialPort.GetPortNames();
        Console.WriteLine("Port Count {0}:", portNames.Length);

        foreach(var port in portNames) 
        { 
            Console.WriteLine(port);
        }

    }

但是,当System.IO.SerialPort.GetPortNames() 被执行时,会返回一个条目:

/dev/ttyAMA0

有没有办法通过 .NET 6.0 System.IO.Ports(或其他)命名空间获取 lsusb 命令返回的相同信息?

更新

为了枚举 Raspberry Pi 4 上的 USB 端口,我编写了以下函数并使用 GetFiles 代替 GetPortNames

   public void EnumUSBPorts()
    {
        
        string[] ttys = Directory.GetFiles("/dev/bus/usb/001", "*");

        foreach (string device in ttys)
        {
                usbNames.Add(device);
                Console.WriteLine(device);
        }
    }

在这种情况下,它返回 Bus 001 上所有 USB 端口的列表。

这似乎列出了 Bus 001 上的所有 USB。

【问题讨论】:

  • System.Management 通常用于此类信息,但未针对 Unix 实现。考虑使用 lsusb,使用 Process 类运行它并解析其输出。
  • 这是一个音频设备。你确定它显示为串口吗?
  • 如果我可以授予自己使用 USB 端口的权限,我想我就在那里......

标签: c# raspberry-pi


【解决方案1】:

这很简单,您必须使用ManagementObjectCollection 并发送查询Select * From Win32_USBHub。使用下面的代码并检查您需要的端口是否会显示,因为我无法测试。

using System;
using System.Collections.Generic;
using System.Management;

namespace USBLoader
{
    class Program
    {
        static void Main(string[] args)
        {
            var usbDevices = GetUSBDevices();

            foreach (var usbDevice in usbDevices)
            {
                Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                    usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
            }

            Console.Read();
        }
        static List<USBDeviceInfo> GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }

            collection.Dispose();
            return devices;
        }
    }
    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }

}

【讨论】:

  • Win32_USBHub 会在树莓派上工作吗?
  • @gunr2171 这就是我要求对其进行测试的原因,因为这是似乎适合在 USB 集线器中工作的唯一命令。
  • 我会测试它并回帖。
  • 注意对主要问题的修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多