[ACCEPTED]-How to programmatically find all available Baudrates in C# (serialPort class)-baud-rate

Accepted answer
Score: 15

I have found a couple of ways to do this. The 19 following two documents were a starting 18 point

The clue is in the following paragraph 17 from the first document

The simplest way 16 to determine what baud rates are available 15 on a particular serial port is to call the 14 GetCommProperties() application programming 13 interface (API) and examine the COMMPROP.dwSettableBaud 12 bitmask to determine what baud rates are 11 supported on that serial port.

At this stage 10 there are two choices to do this in C#:

1.0 Use interop (P/Invoke) as follows:

Define 9 the following data structure

    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 8 the following signatures

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 7 calls (refer to 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 portName is something like 6 COM?? (COM1, COM2, etc). commProp.dwSettableBaud should now contain 5 the desired information.

2.0 Use C# reflection

Reflection can 4 be used to access the SerialPort BaseStream 3 and thence the required data as follows:

   _port = new SerialPort(portName);
   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);

Note 2 that in both the methods above the port(s) has 1 to be opened at least once to get this data.

Score: 0

I don't think you can.

I recently had this 4 problem, and ended up hard coding the baud 3 rates I wanted to use.

MSDN simply states, "The 2 baud rate must be supported by the user's 1 serial driver".

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

Obviously, this doesn't help me. So this 8 is what I am currently relying upon:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;

  public static readonly List<string> SupportedBaudRates = new List<string>

    public static int MaxBaudRate(string portName)
        var maxBaudRate = 0;
            //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;
                maxBaudRate = intBaud;
            //ignored - traps exception generated by
            //baudRate rate not supported

        return maxBaudRate;

The 7 baud rates are in strings because they are 6 destined 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.AddRange(baudRates.Where(baud => Convert.ToInt32(baud) <= maxBaud).ToArray());

You can improve 5 on performance if you know the minimum baud 4 rate supported by all of the serial ports 3 you plan to open. For instance, starting 2 with 115,200 seems like a safe lower limit 1 for serial ports manufactured in this century.

More Related questions