如何以编程方式在C#中查找所有可用的波特率(serialPort类)

有没有办法找出特定系统通过C#支持的所有可用波特率? 这可以通过设备管理器 – >端口获得,但我想以编程方式列出这些。

谢谢。

我找到了几种方法来做到这一点。 以下两个文件是一个起点

线索在第一份文件的以下段落中

确定特定串行端口上可用波特率的最简单方法是调用GetCommProperties()应用程序编程接口(API)并检查COMMPROP.dwSettableBaud位掩码以确定该串行端口支持的波特率。

在这个阶段,在C#中有两种选择:

1.0使用interop(P / Invoke)如下:

定义以下数据结构

[StructLayout(LayoutKind.Sequential)] struct COMMPROP { short wPacketLength; short wPacketVersion; int dwServiceMask; int dwReserved1; int dwMaxTxQueue; int dwMaxRxQueue; int dwMaxBaud; int dwProvSubType; int dwProvCapabilities; int dwSettableParams; int dwSettableBaud; short wSettableData; short wSettableStopParity; int dwCurrentTxQueue; int dwCurrentRxQueue; int dwProvSpec1; int dwProvSpec2; string wcProvChar; } 

然后定义以下签名

 [DllImport("kernel32.dll")] static extern bool GetCommProperties(IntPtr hFile, ref COMMPROP lpCommProp); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttrs, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); 

现在进行以下调用(参考http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx )

  COMMPROP _commProp = new COMMPROP(); IntPtr hFile = CreateFile(@"\\.\" + portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero); GetCommProperties(hFile, ref commProp); 

哪里的portName就像COM? (COM1,COM2等)。 commProp.dwSettableBaud现在应该包含所需的信息。

2.0使用C#reflection

reflection可用于访问SerialPort BaseStream以及所需的数据,如下所示:

  _port = new SerialPort(portName); _port.Open(); object p = _port.BaseStream.GetType().GetField("commProp", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_port.BaseStream); Int32 bv = (Int32)p.GetType().GetField("dwSettableBaud", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(p); 

请注意,在上述两种方法中,必须至少打开一次端口才能​​获取此数据。


我认为你不能。

我最近遇到了这个问题,并最终硬编码了我想要使用的波特率。

MSDN简单地说,“波特率必须由用户的串行驱动程序支持”。

 dwSettableBaud gives 268894207 int (0x1006ffff) while dwMaxBaud gives 268435456 int (0x10000000) 

显然,这对我没有帮助。 所以这就是我目前所依赖的:

 using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; public static readonly List SupportedBaudRates = new List { "300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "921600" }; public static int MaxBaudRate(string portName) { var maxBaudRate = 0; try { //SupportedBaudRates has the commonly used baudRate rates in it //flavor to taste foreach (var baudRate in ConstantsType.SupportedBaudRates) { var intBaud = Convert.ToInt32(baudRate); using (var port = new SerialPort(portName)) { port.BaudRate = intBaud; port.Open(); } maxBaudRate = intBaud; } } catch { //ignored - traps exception generated by //baudRate rate not supported } return maxBaudRate; } 

波特率是字符串,因为它们的目的地是combobox。

  private void CommPorts_SelectedIndexChanged(object sender, EventArgs e) { var combo = sender as ComboBox; if (combo != null) { var port = combo.Items[combo.SelectedIndex].ToString(); var maxBaud = AsyncSerialPortType.MaxBaudRate(port); var baudRates = ConstantsType.SupportedBaudRates; var f = (SerialPortOpenFormType)(combo.Parent); f.Baud.Items.Clear(); f.Baud.Items.AddRange(baudRates.Where(baud => Convert.ToInt32(baud) <= maxBaud).ToArray()); } } 

如果您知道计划打开的所有串行端口支持的最小波特率,则可以提高性能。 例如,以115,200开头似乎是本世纪制造的串口的安全下限。