1

So far I have not been able to find an answer, but I wanted to know if there is a way, specifically with C#, to detect what the highest baudrate a serial port is capable of, without just having to test it.

Is there something in it's hardware profile or something that stores the available baudrates for a certain port, that C# can query?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Xantham
  • 1,829
  • 7
  • 24
  • 42

1 Answers1

2

Using reflection you can/should do it like this:

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

Note: The port will be opened in order to check it's settable baudrate! :)

See this post for more information on how you can check the baudrate: How to programatically find all available Baudrates in C# (serialPort class)

Community
  • 1
  • 1
Faraday
  • 2,904
  • 3
  • 23
  • 46
  • Thank you for the answer. Unfortunately, the answer gives out is almost a quarter of a gigabaud (I never thought I'd write that term about normal RS232). So it states a number well above what the serial driver appears to be capable of (on a port that it stated could handle 268,894,207 (number stored in bv), setting it to 230,400 baud caused an exception). Is there some filtering or formula I have to put bv through to get the true max baudrate that the serial chip can handle, or is this just a limit of what windows will attempt to write to it? – Xantham Jun 08 '12 at 17:18
  • RS232 is an asynchronous data bus without any kind of built in synchronization or error detection/correction. So the practical limit of baud rate depends on several factors, including the quality / length of the serial connection, and the baud rate accuracy specs of both sides of the link (how accurate is baud rate generated for Tx, and how much error in Rx baud can be tolerated via Rx oversampling). If you're going to push the speed limits, better add some protocol with CRC's and retransmissions to handle occasional bit errors. – TJD Jun 08 '12 at 19:17
  • 1
    @Xantham yes this is almost 8 years later but for anyone looking at this confused about the values returned from dwSettableBaud they are constants corresponding to a specific baud value. The reference can be found here: https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commprop – Andy Dec 10 '19 at 04:41