我怎样才能得到“友好”的朋友? Mono 中串口的名称并保持跨平台
我问了这个问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
据我所知,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.
考虑查看
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.
这是通过 com.fazecast.jSerialComm.SerialPort 的 getDescriptivePortName() 方法完成的。 我在下拉列表中显示端口名称及其友好名称时遇到了同样的问题。 因此,我必须制作一个哈希映射,其中“COM8”等 SystemPortNames 是一个键,“Prolific USB-to-Serial Comm Port (COM8)”是一个值。 这段代码对我有用:
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:
在 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)
我建立了一个用于串行端口控制的库。 它可以从注册表中搜索友好名称。
https://github.com/kcwongjoe/serial_port
I built a library for serial port control. It can search the friendly name from registry.
https://github.com/kcwongjoe/serial_port
代码:
用法:
示例输出:
COM1 - ACPI\PNP0501\1_0
COM2 - ACPI\PNP0501\2_0
COM3 - FTDIBUSVID_0000+PID_0000+0&0000000&0&0?000_0
Code:
usage:
example output:
COM1 - ACPI\PNP0501\1_0
COM2 - ACPI\PNP0501\2_0
COM3 - FTDIBUSVID_0000+PID_0000+0&0000000&0&0?000_0
您需要考虑执行 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_SerialPort
s 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.