windows 在python中获取友好的设备名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7551546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:09:32  来源:igfitidea点击:

Getting friendly device names in python

pythonwindowslinuxmacos

提问by manneorama

I have an 2-port signal relay connected to my computer via a USB serial interface. Using the pyserial module I can control these relays with ease. However, this is based on the assumption that I know beforehand which COM-port (or /dev-node) the device is assigned to.

我有一个 2 端口信号继电器通过 USB 串行接口连接到我的计算机。使用 pyserial 模块,我可以轻松控制这些继电器。但是,这是基于我事先知道设备分配给哪个 COM 端口(或 /dev-node)的假设。

For the project I'm doing that's not enough since I don't want to assume that the device always gets assigned to for example COM7 in Windows. I need to be able to identify the device programatically across the possible platforms (Win, Linux, OSX (which I imagine would be similar to the Linux approach)), using python. Perhaps by, as the title suggests, enumerate USB-devices on the system and somehow get more friendly names for them. Windows and Linux being the most important platforms to support.

对于我正在做的项目,这还不够,因为我不想假设设备总是被分配给例如 Windows 中的 COM7。我需要能够使用 python 跨可能的平台(Win、Linux、OSX(我认为这类似于 Linux 方法))以编程方式识别设备。也许,正如标题所暗示的那样,枚举系统上的 USB 设备并以某种方式为它们获得更友好的名称。Windows 和 Linux 是最重要的支持平台。

Any help would be greatly appreciated!

任何帮助将不胜感激!

EDIT:
Seems like the pyudev-module would be a good fit for Linux-systems. Has anyone had any experience with that?

编辑:
似乎 pyudev-module 非常适合 Linux 系统。有没有人有这方面的经验?

采纳答案by abbot

Regarding Linux, if all you need is to enumerate devices, you can even skip pyudev dependency for your project, and simply parse the output of /sbin/udevadm info --export-dbcommand (does not require root privileges). It will dump all information about present devices and classes, including USB product IDs for USB devices, which should be more then enough to identify your USB-to-serial adapters. Of course, you can also do this with pyudev.

对于Linux,如果您只需要枚举设备,您甚至可以跳过项目的pyudev依赖项,只需解析/sbin/udevadm info --export-db命令的输出(不需要root权限)。它将转储有关当前设备和类的所有信息,包括 USB 设备的 USB 产品 ID,这应该足以识别您的 USB 到串行适配器。当然,您也可以使用 pyudev 执行此操作。

回答by Taylor Hanson

I know this is an older post, but I was struggling with it today. Ultimately I used the wmi library for python as I'm on a Windows machine (sorry, I know my answer only applies to Windows, but maybe it'll help someone).

我知道这是一个较旧的帖子,但我今天正在努力解决它。最终,我在 Windows 机器上使用了 python 的 wmi 库(抱歉,我知道我的答案仅适用于 Windows,但也许它会帮助某人)。

Install the package using pip first:

首先使用 pip 安装包:

pip install wmi

then

然后

import wmi
c = wmi.WMI()
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
    print item.Dependent.Caption

Should result with something like:

结果应该是这样的:

USB Root Hub
USB Root Hub
Prolific USB-to-Serial Comm Port (COM9) USB Root Hub
USB Root Hub
USB Composite Device
USB Video Device USB Audio Device
USB Root Hub
...snip...

USB Root Hub
USB Root Hub
Prolific USB-to-Serial Comm Port (COM9) USB Root Hub
USB Root Hub
USB Composite Device
USB Video Device USB Audio Device
USB Root Hub
...snip...

In this case, you'd have to string parse the Caption to find the COM port. You can also take a look at just the item. Dependent object to see other attributes of the USB device beside Caption that you may find relevant:

在这种情况下,您必须对 Caption 进行字符串解析才能找到 COM 端口。您也可以只查看项目。用于查看除 Caption 之外的 USB 设备其他属性的相关对象:

instance of Win32_PnPEntity
{
    Caption = "USB Root Hub";
    ClassGuid = "{36fc9e60-c465-11cf-8056-444553540000}";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_PnPEntity";
    Description = "USB Root Hub";
    DeviceID = "USB\ROOT_HUB\4&32F13EF0&1";
    HardwareID = {"USB\ROOT_HUB&VID8086&PID3A36&REV0000",     
                "USB\ROOT_HUB&VID8086&PID3A36", "USB\ROOT_HUB"};
    Manufacturer = "(Standard USB Host Controller)";
    Name = "USB Root Hub";
    PNPDeviceID = "USB\ROOT_HUB\4&32F13EF0&1";
    Service = "usbhub";
    Status = "OK";
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = "001fbc0934d1";
};

回答by Adrian Cox

Windows: you can pull USB information from WMI, but you need to be administrator. The examples are in .NET, but you should be able to use the Python WMI module. This will give you access to USB identification strings, which may contain useful information. For FTDI serial devices there is a short cut using FTDI's DLL, which does not require privileged access.

Windows:您可以从 WMI 中提取 USB 信息,但您需要是管理员。这些示例在 .NET 中,但您应该能够使用Python WMI 模块。这将使您能够访问 USB 标识字符串,其中可能包含有用的信息。对于 FTDI 串行设备,使用 FTDI 的 DLL 有一个捷径,它不需要特权访问。

Linux: all the available information is under /sys/bus/usb, and also available through udev. This looks like a good answer.

Linux:所有可用信息都在 下/sys/bus/usb,也可以通过 udev 获得。 这看起来是个不错的答案

回答by hymloth

At least for linux, you can use some dummy hacks to determine your /dev node, by inspecting for example the output of "ls /dev | grep ttyUSB" before and after you attach your device. This somehow must apply as well for the OSX case. A good idea is to inspect those commands using something like the subprocess.Popen() command. As for windows, thismight be helpful.

至少对于 linux,您可以使用一些虚拟技巧来确定您的 /dev 节点,例如在您连接设备之前和之后检查“ls /dev | grep ttyUSB”的输出。这在某种程度上也必须适用于 OSX 案例。一个好主意是使用类似于 subprocess.Popen() 命令的东西检查这些命令。至于窗户,可能会有所帮助。

回答by zeekay

As far as Windows goes, you could scan the registry:

就 Windows 而言,您可以扫描注册表:

import _winreg as reg
from itertools import count

key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, 'HARDWARE\DEVICEMAP\SERIALCOMM')
try:
    for i in count():
        device, port = reg.EnumValue(key, i)[:2]
        print device, port
except WindowsError:
    pass

回答by Hernan

It will be great if this is possible, but in my experience with commercial equipments using COM ports this is not the case. Most of the times you need to set manually in the software the COM port. This is a mess, specially in windows (at least XP) that tends to change the number of the com ports in certain cases. In some equipment there is an autodiscovery feature in the software that sends a small message to every COM port and waits for the right answer. This of course only works if the instrument implements some kind of identification command. Good luck.

如果可能的话,那就太好了,但根据我使用 COM 端口的商业设备的经验,情况并非如此。大多数情况下,您需要在软件中手动设置 COM 端口。这是一团糟,特别是在 Windows(至少是 XP)中,它在某些情况下往往会改变 com 端口的数量。在某些设备中,软件中有一个自动发现功能,可以向每个 COM 端口发送一条小消息并等待正确答案。这当然只有在仪器执行某种识别命令时才有效。祝你好运。