串行端口(RS232)在单多平台 [英] Serial Port (rs232) in Mono for multiple platforms

查看:161
本文介绍了串行端口(RS232)在单多平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算重新编写一个Win32应用程序(原生C ++)在.NET - 最有可能使用单这样我就可以在Win32,Linux和Mac上运行它。我试图解决(有只有真正为Win32开发的)问题是一个问题,串行端口的定义。怎样才能典型的识别平台的差异时,有只应该是一个可执行。具体来说,COM口被确定在窗口为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.

也许这是因为它适用于任何特定平台的东西,在单声道/ .NET一个更通用的问题。

Perhaps this is a more generic question in that it applies to any platform-specific stuff in mono/.NET.

你是如何处理的?在一些字符串资源或配置文件,或者硬codeD和交换机的基础上运行平台?

How do you handle this? In some string resource or config file, or hard-coded and switch based on runtime platform?

此Any code样?注意是一个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已经能够以present串行端口以更独立的方式。也许这是不可能的。

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中的更高的端口号和OFR的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基于其插入,consistantly插头)从我的串行端口扫描器

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 扫描完成

Scanning COM1 Scanning COM4 Scanning Complete

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波特率,数据位的Int32,奇偶校验,停止位停止位,布尔dtrEnable,布尔rtsEnable,握手握手,的Int32 readTimeout,的Int32 writeTimeout,的Int32 readBufferSize,的Int32 writeBufferSize) [0x00000]   在(包装远程处理调用,以检查)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]   在(包装远程处理调用,以检查)System.IO.Ports.SerialPort:打开()   在HSMScanner.Program.Main(System.String []参数)[0x00000] 扫描的/ dev / ttyS2端口FailedSystem.IO.IOException:I / O错误   在System.IO.Ports.SerialPortStream..ctor(System.String PORTNAME,的Int32波特率,数据位的Int32,奇偶校验,停止位停止位,布尔dtrEnable,布尔rtsEnable,握手握手,的Int32 readTimeout,的Int32 writeTimeout,的Int32 readBufferSize,的Int32 writeBufferSize) [0x00000]   在(包装远程处理调用,以检查)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]   在(包装远程处理调用,以检查)System.IO.Ports.SerialPort:打开()   在HSMScanner.Program.Main(System.String []参数)[0x00000] 扫描的/ dev / ttyS3端口FailedSystem.IO.IOException:I / O错误   在System.IO.Ports.SerialPortStream..ctor(System.String PORTNAME,的Int32波特率,数据位的Int32,奇偶校验,停止位停止位,布尔dtrEnable,布尔rtsEnable,握手握手,的Int32 readTimeout,的Int32 writeTimeout,的Int32 readBufferSize,的Int32 writeBufferSize) [0x00000]   在(包装远程处理调用,以检查)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]   在(包装远程处理调用,以检查)System.IO.Ports.SerialPort:打开()   在HSMScanner.Program.Main(System.String []参数)[0x00000]

Scanning /dev/ttyS0 Scanning /dev/ttyS1 Port FailedSystem.IO.IOException: I/O Error at 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] at (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) at System.IO.Ports.SerialPort.Open () [0x00000] at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open () at HSMScanner.Program.Main (System.String[] args) [0x00000] Scanning /dev/ttyS2 Port FailedSystem.IO.IOException: I/O Error at 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] at (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) at System.IO.Ports.SerialPort.Open () [0x00000] at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open () at HSMScanner.Program.Main (System.String[] args) [0x00000] Scanning /dev/ttyS3 Port FailedSystem.IO.IOException: I/O Error at 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] at (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) at System.IO.Ports.SerialPort.Open () [0x00000] at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open () at HSMScanner.Program.Main (System.String[] args) [0x00000]

3)苹果电脑...

3) Macs...

哦,亲爱的哦,亲爱。苹果电脑(当USB串行端口插入和驱动程序,一切都OK),不回的什么的上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.

寻找更多的内容。

这篇关于串行端口(RS232)在单多平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆