我怎样才能得到“友好”的朋友? Mono 中串口的名称并保持跨平台

发布于 2024-07-11 21:04:11 字数 834 浏览 6 评论 0原文

我问了这个问题:
Mono 中适用于多个平台的串行端口 (rs232)

以及此一个是相关的:
如何获得友好Windows 中 COM 端口的名称?

但我希望能够在 Windows 上获得“友好”名称 - 如果有这样的东西,也可能在 linux /mac 上获得。

有没有跨平台的方法可以做到这一点,还是我运气不好?

这是我在当前应用程序中所做的事情 - 它非常适合本机 C++ win32。

http://www.naughter.com/enumser.html

无论如何,它看起来都不是就像跨平台分发的一个漂亮的解决方案。 有没有人有什么建议?

编辑 - 因为人们很难理解我的要求: 例如 - COM9 不是一个友好的名称。 我想要一些写着“COM9 -​​ USB 连接器”或类似内容的东西。 在 Win32 中可以通过上面的链接实现这一点。 这是令人讨厌和黑客的,但很多时候最终用户不知道他们需要在我的程序中打开什么 COM 端口,除非有一个有用的名称 - 比“COMn”更有用。

I asked this question:
Serial Port (rs232) in Mono for multiple platforms

and this one is related:
How do I get the friendly name of a COM port in Windows?

But I want to be able to get the "friendly" name on Windows- and possibly also on linux /mac if there is such a thing.

Is there any cross platform way to do it, or am I out of luck?

Here is what I am doing in my current app - and it works great for native C++ win32.

http://www.naughter.com/enumser.html

In any case it does not look like a pretty solution for cross-platform distribution. Does anyone have any suggestions?

EDIT - since people are having trouble understanding what I am asking for:
as an example - COM9 is not a friendly name. I want something that says "COM9 - USB connector" or something like that. This is possible with the link above in Win32. It is nasty and hacky, but many times end users have no idea what COM port they need to open in my program unless there is a useful name - more useful than "COMn."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一页 2024-07-18 21:04:11

据我所知,Linux 中的 COMM 设备没有“友好”的名称。 我建议您在 Linux 环境中使用 /dev/ttyS# 作为设备名称,并在 Windows 中将它们列为 COMM#。

Linux 用户会理解这些术语,所以不必担心。

AFAIK there is no "friendly" name for the COMM devices in linux. What I suggest you do is use the /dev/ttyS# as your device name in a linux environment and list them as COMM# in windows.

The linux users will understand the terminology so there shouldn't be a worry there.

祁梦 2024-07-18 21:04:11

考虑查看 SerialPort.GetPortNames() 静态方法。 它在 .NET 2.0 中可用,而且看起来它也在 Mono 中实现。 根据http://www.go-mono.com/docs/>单声道文档页面,GetPortNames存在于Mono串行端口对象上,所以我会尝试一下。

如果实现了,它应该返回一个 C# 字符串数组,其中包含计算机上可用的端口名称。 这些对于您拥有的任何底层操作系统都应该有意义。 例如,在 Windows 中,它将返回 COM1、COM2、COM4 等。 它应该返回 PortName 属性所需的字符串。

更新:

查看 来自mono-dev 邮件列表 看起来它确实可以在 *nix 环境下工作。

Consider looking at the SerialPort.GetPortNames() static method. It's available in .NET 2.0, and it looks like it's implemented in Mono as well. According to http://www.go-mono.com/docs/>the mono docs page, GetPortNames exists on the Mono serial port object, so I'd give it a shot.

If it's implemented, it should return you a C# array of strings containing port names available on your computer. These should make sense for whatever underlying OS you have. For example, in windows it'll return COM1, COM2, COM4, and so on. It should return the string needed for the PortName property.

Update:

Taking a look at a post from the mono-dev mailing list it looks like it does work under *nix environments.

秋凉 2024-07-18 21:04:11

这是通过 com.fazecast.jSerialComm.SerialPort 的 getDescriptivePortName() 方法完成的。 我在下拉列表中显示端口名称及其友好名称时遇到了同样的问题。 因此,我必须制作一个哈希映射,其中“COM8”等 SystemPortNames 是一个键,“Prolific USB-to-Serial Comm Port (COM8)”是一个值。 这段代码对我有用:

import com.fazecast.jSerialComm.SerialPort;
import java.util.Arrays;
import java.util.HashMap;

public class PortService {

    public HashMap getAllPorts() {

        SerialPort[] ports = SerialPort.getCommPorts();
        HashMap result = Arrays.stream(ports).collect(HashMap::new,
            (map, port) -> map.put(port.getSystemPortName(), port.getDescriptivePortName()),
            HashMap::putAll);

        return result;
    }
}

It's done by getDescriptivePortName() method of com.fazecast.jSerialComm.SerialPort. I had the same issue to show ports' names and their friendly names in a dropdownList. So I had to make a Hashmap in which SystemPortNames like 'COM8' was a key and the 'Prolific USB-to-Serial Comm Port (COM8)' was a value. This piece of code worked for me:

import com.fazecast.jSerialComm.SerialPort;
import java.util.Arrays;
import java.util.HashMap;

public class PortService {

    public HashMap getAllPorts() {

        SerialPort[] ports = SerialPort.getCommPorts();
        HashMap result = Arrays.stream(ports).collect(HashMap::new,
            (map, port) -> map.put(port.getSystemPortName(), port.getDescriptivePortName()),
            HashMap::putAll);

        return result;
    }
}
栀子花开つ 2024-07-18 21:04:11

在 WMI 中尝试以下查询:

“Select Name From Win32_PnPEntity”并搜索包含“COM”的对象,例如,我的计算机上安装了 USB 串行转换器设备:

USB60FPW USB 串行转换器 (COM6)

Try the following query in WMI:

"Select Name From Win32_PnPEntity" and search for objects that contain "COM", for example, I have a USB-Serial Converter device installed on my computer:

USB60FPW USB-Serial Converter (COM6)

踏雪无痕 2024-07-18 21:04:11

我建立了一个用于串行端口控制的库。 它可以从注册表中搜索友好名称。

https://github.com/kcwongjoe/serial_port

std::vector<SerialPortInfo> comPorts = SerialPort::getSerialPortList();
std::cout << comPorts[0].friendlyName << std::endl;

I built a library for serial port control. It can search the friendly name from registry.

https://github.com/kcwongjoe/serial_port

std::vector<SerialPortInfo> comPorts = SerialPort::getSerialPortList();
std::cout << comPorts[0].friendlyName << std::endl;
月棠 2024-07-18 21:04:11

代码:

    public static ArrayList GetComFriendlyNames()
    {
        ArrayList names = new ArrayList();
        try
        {
            ManagementObjectSearcher searcher =
              new ManagementObjectSearcher("root\\WMI",
              "SELECT InstanceName, PortName FROM MSSerial_PortName");

            foreach (ManagementObject port in searcher.Get())
            {
                names.Add(port["PortName"] + " - " + port["InstanceName"]);
            }
        }
        catch (ManagementException)
        {
        }
        return names;
    }

用法:

        ArrayList ports = GetComFriendlyNames();
        foreach (string name in ports)
        {
            Console.WriteLine(name);
        }

示例输出:

COM1 - ACPI\PNP0501\1_0

COM2 - ACPI\PNP0501\2_0

COM3 - FTDIBUSVID_0000+PID_0000+0&0000000&0&0?000_0

Code:

    public static ArrayList GetComFriendlyNames()
    {
        ArrayList names = new ArrayList();
        try
        {
            ManagementObjectSearcher searcher =
              new ManagementObjectSearcher("root\\WMI",
              "SELECT InstanceName, PortName FROM MSSerial_PortName");

            foreach (ManagementObject port in searcher.Get())
            {
                names.Add(port["PortName"] + " - " + port["InstanceName"]);
            }
        }
        catch (ManagementException)
        {
        }
        return names;
    }

usage:

        ArrayList ports = GetComFriendlyNames();
        foreach (string name in ports)
        {
            Console.WriteLine(name);
        }

example output:

COM1 - ACPI\PNP0501\1_0

COM2 - ACPI\PNP0501\2_0

COM3 - FTDIBUSVID_0000+PID_0000+0&0000000&0&0?000_0

孤城病女 2024-07-18 21:04:11

您需要考虑执行 WMI。 我自己还无法运行这个,但是如果你结合 这个基本框架 如何使用 this 检索 WMI 对象Win32_SerialPort 类的文档,我认为您可以解决一些问题。

基本上,您想要获取系统上所有 Win32_SerialPort 的集合,然后迭代它们。 您可能需要“标题”、“说明”,或者只是“名称”。 我最好的建议是设置一个断点并在调试模式下检查对象的属性,以便您可以准确地找出填充的内容。

You need to look into doing WMI. I haven't been able to run this myself, but if you combine this basic framework of how to retrieve a WMI object with this documentation of the Win32_SerialPort class, I think you can work something out.

Basically, you want to get a collection of all the Win32_SerialPorts on the system, then iterate through them. You may want the "Caption", or "Description", or maybe just "Name". My best advice is to just set a breakpoint and check the object's properties in debug mode so you can figure out exactly what gets populated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文