Bug 9525 - SerialPort only accepts a few hardcoded baudrates
Summary: SerialPort only accepts a few hardcoded baudrates
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-01-13 18:51 UTC by Ariel Rocholl
Modified: 2015-04-22 08:51 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 9525 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Ariel Rocholl 2013-01-13 18:51:18 UTC
This is a more generic case of the bug reported #8207.

Microsoft .NET accepts any baudrate but zero or lower than zero.

I tested several custom baudrate values and, as far as I can see, all them are accepted by .NET SerialPort. 

Perhaps the Microsoft approach is to ask the underlying driver to validate baudrates, if at all. In the case of the Windows CP2102 driver I am using, it will accept any baudrate including ones that are not physically supported. For instance I tested 1,500,000bps which is not really going to work on the physical CP2102 hardware (max available speed is 920,000bps on the CP2102 device) and still the SerialPort accepts the 1,500,000 bps, connects to it and if you read the baudrate property, it returns 1,500,000. 

So probably the Mono approach is too limited, because it really depends on a switch/case with a set of hardcoded, predefined values. 

See a fraction of Serial.c below

            switch (baud_rate)
            case 115200: 
                baud_rate = B115200;
            case 57600:
                baud_rate = B57600;
            case 50:
            case 0:
                baud_rate = -1;

In that code, anything that is unknown goes to <default> which in the end returns an unsupported baudrate exception to the caller. 

However, that doesn’t seem the way the Windows .NET SerialPort class works and, in today’s world of interconnected USB COM custom devices, it really puts a hard limit on baudrates you can use. Interestingly, the documentation of SerialPort::Open doesn’t explicitly document any baudrate value being invalid, except if it is equal or lower than zero. From MSDN SerialPort::Open() :


One or more of the properties for this instance are invalid. For example, the Parity, DataBits, or Handshake properties are not valid values; the BaudRate is less than or equal to zero; the ReadTimeout or WriteTimeout property is less than zero and is not InfiniteTimeout. 

The right code would be to return -1 only for (baud_rate <= 0) and consider ok anything else.

So except anyone disagree, baudrate values should not be checked for anything different than that. I will then fill in a bugzilla entry and pull a request in a few days. I am a Mercurial guy so have to get used to GIT in the first place…