问题描述
我正计划在 .NET 中重新编写一个 Win32 应用程序(本机 C++)——很可能使用单声道,这样我就可以在 Win32、Linux 和 mac 上运行它.我试图解决的问题(仅真正为 Win32 开发)是串行端口定义的问题.当只有一个可执行文件时,通常如何识别平台差异.具体来说,COM 端口在 Windows 中被标识为 COM1 或类似的东西 (.COM),但在 linux 上它们被指定为/dev/ttyS0 之类的东西.
I am planning on re-writing a Win32 application (native C++) in .NET - most likely using mono so I can run it on Win32, Linux and mac. The problem I am trying to solve (having only really developed for Win32) is an issue with the serial port definition. How does one typically identify differences in platform when there is only supposed to be one executable. Specifically, the COM port is identified in windows as COM1 or something like that (.COM) but on linux they are specified as something like /dev/ttyS0.
是否在运行时检查平台以获取此信息?
Does one check the platform at runtime for this information?
我认为唯一的区别在于港口的开放和关闭.读写是一样的.
I think the only difference would be in the opening and closing of the port. The reading and writing is the same.
也许这是一个更通用的问题,因为它适用于 mono/.NET 中任何特定于平台的内容.
Perhaps this is a more generic question in that it applies to any platform-specific stuff in mono/.NET.
你如何处理这个问题?在某些字符串资源或配置文件中,还是基于运行时平台硬编码和切换?
How do you handle this? In some string resource or config file, or hard-coded and switch based on runtime platform?
有任何代码示例吗?请注意,我是一名 C++ 开发人员,并不熟悉 .NET 中可用的所有类.有没有办法从 CLR 获取串行端口命名方案,或者有没有办法从 CLR 获取操作系统/平台?
Any code sample for this? Note am a C++ developer and not familiar with all the classes available in .NET. is there a way to get either the serial port naming scheme from the CLR or is there a way to get the OS/Platform from the CLR?
如果 CLR 能够以更独立的方式呈现串行端口,那就太好了.也许那是不可能的.
it would have been nice for the CLR to have been able to present the serial ports in a more independent manner. Maybe that is not possible.
谢谢,蒂姆
编辑
鉴于到目前为止的一个响应,我想我应该尝试一下
SerialPort.GetPortNames() 先枚举看看是否有效.(在两个平台上)
Given the one response so far I guess I should just try the
SerialPort.GetPortNames() enumeration first to see if it works. (on both platforms)
在 win32 中用于更高的端口号和 USB 加密狗 - 它不像基本的 COM 端口枚举那么简单.
In win32 for the higher port numbers and ofr the USB dongle - it is not as simple as the basic COM port enumeration.
我将在这里报告调查结果.
I will report the findings here.
推荐答案
刚发现这个帖子,我想我应该添加我的发现:随机,我也担心在 mac 上这个问题.在 Windows 和 Linux 中(在 VS 和 Mono 中)SerialPort.GetPortNames() 返回一个列表,具有以下规则:
Just discovered this thread, and thought I aught to just add my discoveries: randomly, I am also worried about this on a mac. In Windows and Linux (in VS and Mono alike) SerialPort.GetPortNames() return a list, with the following rules:
1) Windows 返回一个字符串列表,如 Com1、Com4,遗漏了任何不存在的字符串(USB 串行适配器似乎始终根据插入的插头获取 COM 编号)来自我的串行端口扫描仪:
1) Windows returns a list of strings like Com1, Com4, missing out any which don't exist (USB Serial adapters seem to take a COM number based on the plug they are plugged into, consistantly) From my serial port scanner:
扫描 COM1扫描 COM4扫描完成
2) Linux 返回 linux 发行版的编译器已启用的所有可能的 tty 串行端口.这似乎是大约 8 个端口,如果您尝试打开,将引发异常(来自我的串行端口扫描仪:
2) Linux returns all possibly tty serial ports that the compiler of the linux distro has enabled. This seems to be about 8 ports, which if you try to open, will throw an exception (from my serial port scanner:
扫描/dev/ttyS0扫描/dev/ttyS1 端口 FailedSystem.IO.IOException: I/O 错误在 System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize)[0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)在 System.IO.Ports.SerialPort.Open () [0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()在 HSMScanner.Program.Main (System.String[] args) [0x00000]扫描/dev/ttyS2 端口 FailedSystem.IO.IOException: I/O 错误在 System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize)[0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)在 System.IO.Ports.SerialPort.Open () [0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()在 HSMScanner.Program.Main (System.String[] args) [0x00000]扫描/dev/ttyS3 端口 FailedSystem.IO.IOException: I/O 错误在 System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize)[0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)在 System.IO.Ports.SerialPort.Open () [0x00000]在 (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()在 HSMScanner.Program.Main (System.String[] args) [0x00000]
3) Mac...
天哪,天哪.Mac(当插入 USB 串行端口并且驱动程序和一切正常时)不会在 GetPortNames() 上返回 任何内容.纳达.在/dev/tty 中查看,额外的设备仅在插入设备并具有/dev/tty.usbserial-A7006Ro7 之类的名称时才会出现,不幸的是,使用此名称作为程序的参数后跟 serial.open dosnt 似乎有什么影响.
Oh dear oh dear. Macs (when a usb serial port is plugged in and drivers and everything are ok) don't return anything on the GetPortNames(). Nada. Looking in the /dev/tty, the extra devices appear only when the device is plugged in and has names like /dev/tty.usbserial-A7006Ro7 unfortunatly, using this name as an argument to the program followed by serial.open dosnt seem to have any effect.
进一步研究.
这篇关于用于多个平台的 Mono 中的串行端口 (rs232)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!