How to programmatically find all available Baudrates in C # (serialPort class)

Is there a way to find out all the available baud rates that the system supports through C #? This is available through Device Manager β†’ Ports but I want to list them programmatically.

Thanks.

+12
source share
3 answers

I found several ways to do this. The following two documents were the starting point.

The key is in the next paragraph from the first document

The easiest way to determine which baud rates are available for a particular serial port is to call the GetCommProperties () API (API) and examine the COMMPROP.dwSettableBaud bitmask to determine which baud rates are supported on that serial port.

At this point in C # there are two options:

1.0 Use interop (P / Invoke) as follows:

Define the following data structure

[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; } 

Then define the following signatures

 [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); 

Now make the following calls (see 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); 

Where is portName something like COM ?? (COM1, COM2, etc.). commProp.dwSettableBaud should now contain the necessary information.

2.0 Use reflection C #

Reflection can be used to access the SerialPort base stream, and then the required data as follows:

  _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); 

Please note that in both of the above methods, the port must be opened at least once in order to receive this data.


+12
source

I do not think you can.

I recently ran into this problem and ended up with the hard-coding baud rate that I wanted to use.

MSDN simply states: "The baud rate must be supported by the serial user driver."

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

Obviously this does not help me. Here's what I currently rely on:

 using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; public static readonly List<string> SupportedBaudRates = new List<string> { "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; } 

Baud rates are in rows because they are for a combo box.

  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()); } } 

You can improve performance if you know the minimum transfer rate supported by all the serial ports you plan to open. For example, starting at 115,200 seems to be a reliable lower limit for serial ports released this century.

0
source

All Articles