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 (show other bugs)
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)

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


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…

Note You need to log in before you can comment on or make changes to this bug.