C#: How to determine settable baud rate for a serial port device?
<p>I've just encountered a problem with the serial port but so far could not find an answer to it. Please let me know or give me some clues if I'm doing it wrong, Thanks. I'll try to explain the whole situation as detail as possible and ask my question in the end. Here goes.</p>
<p>I bought a USB to TTL converter using FTDI FT232RL chip and was curious about how serial network would work and how I could write my own program in C# instead of using hyper terminal. I started writing the program by reading some tutorials to give myself a quick start. After going through tutorials, I've noticed that all the tutorials I've read about uses similar methods when it comes to baud rate settings. They hard code the typical baud rates into their code instead of asking if the device would support that. I always try to write program as generic as possible, because of this, I started looking into how I can get retrieve information from the device. After searching, I've found <a href="https://stackoverflow.com/questions/1165692/how-to-programatically-find-all-available-baudrates-in-c-sharp-serialport-class">this post answered by HiteshP</a> very useful and have gone on using the reflection method suggested in the post. So here's what my code looks like:</p>
<pre><code>private void UpdateBaudRateCollection()
{
    mySerialPort.PortName = cboAllPortNames.SelectedItem.ToString();
    mySerialPort.Open();
    object p = mySerialPort.BaseStream.GetType().GetField(&quot;commProp&quot;, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(mySerialPort.BaseStream);
    int dwSettableBaud = (int)p.GetType().GetField(&quot;dwSettableBaud&quot;, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(p);
    mySerialPort.Close();
    MessageBox.Show(dwSettableBaud.ToString(&quot;X&quot;));
}
</code></pre>
<p>The result that I get from the MessageBox is 10066B70 which I have no idea what it represents as it is <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363189(v=vs.85).aspx" rel="nofollow noreferrer">not shown in Microsoft's COMMPROP structure</a>.</p>
<p>I did more search trying to figure out what 10066B70 meant but could not find any answer until I came across <a href="http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application" rel="nofollow noreferrer">Basic serial port listening application</a> by Amund Gjersøe. Maybe I had to perform AND operations with 10066B70 and the all the value given by Microsoft's COMMPROP structure. So I threw the value into the function.</p>
<pre><code>private void SettableBaudRateOfDevice(int settableBaudRate)
{
    const int BAUD_075 = 0x00000001;
    const int BAUD_110 = 0x00000002;
    const int BAUD_150 = 0x00000008;
    const int BAUD_300 = 0x00000010;
    const int BAUD_600 = 0x00000020;
    const int BAUD_1200 = 0x00000040;
    const int BAUD_1800 = 0x00000080;
    const int BAUD_2400 = 0x00000100;
    const int BAUD_4800 = 0x00000200;
    const int BAUD_7200 = 0x00000400;
    const int BAUD_9600 = 0x00000800;
    const int BAUD_14400 = 0x00001000;
    const int BAUD_19200 = 0x00002000;
    const int BAUD_38400 = 0x00004000;
    const int BAUD_56K = 0x00008000;
    const int BAUD_57600 = 0x00040000;
    const int BAUD_115200 = 0x00020000;
    const int BAUD_128K = 0x00010000;
    const int BAUD_USER = 0x10000000;

    cboBaudRate.Items.Clear();

    if ((settableBaudRate &amp; BAUD_075) &gt; 0)
        cboBaudRate.Items.Add(75);
    if ((settableBaudRate &amp; BAUD_110) &gt; 0)
        cboBaudRate.Items.Add(110);
    if ((settableBaudRate &amp; BAUD_150) &gt; 0)
        cboBaudRate.Items.Add(150);
    if ((settableBaudRate &amp; BAUD_300) &gt; 0)
        cboBaudRate.Items.Add(300);
    if ((settableBaudRate &amp; BAUD_600) &gt; 0)
        cboBaudRate.Items.Add(600);
    if ((settableBaudRate &amp; BAUD_1200) &gt; 0)
        cboBaudRate.Items.Add(1200);
    if ((settableBaudRate &amp; BAUD_1800) &gt; 0)
        cboBaudRate.Items.Add(1800);
    if ((settableBaudRate &amp; BAUD_2400) &gt; 0)
        cboBaudRate.Items.Add(2400);
    if ((settableBaudRate &amp; BAUD_4800) &gt; 0)
        cboBaudRate.Items.Add(4800);
    if ((settableBaudRate &amp; BAUD_7200) &gt; 0)
        cboBaudRate.Items.Add(7200);
    if ((settableBaudRate &amp; BAUD_9600) &gt; 0)
        cboBaudRate.Items.Add(9600);
    if ((settableBaudRate &amp; BAUD_14400) &gt; 0)
        cboBaudRate.Items.Add(14400);
    if ((settableBaudRate &amp; BAUD_19200) &gt; 0)
        cboBaudRate.Items.Add(19200);
    if ((settableBaudRate &amp; BAUD_38400) &gt; 0)
        cboBaudRate.Items.Add(38400);
    if ((settableBaudRate &amp; BAUD_56K) &gt; 0)
        cboBaudRate.Items.Add(56000);
    if ((settableBaudRate &amp; BAUD_57600) &gt; 0)
        cboBaudRate.Items.Add(57600);
    if ((settableBaudRate &amp; BAUD_115200) &gt; 0)
        cboBaudRate.Items.Add(115200);
    if ((settableBaudRate &amp; BAUD_128K) &gt; 0)
        cboBaudRate.Items.Add(128000);
    if ((settableBaudRate &amp; BAUD_USER) &gt; 0)
        cboBaudRate.Items.Add(3000000);
}
</code></pre>
<p>After running the program, I get the following baud rates:</p>
<pre><code>300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 3000000
</code></pre>
<p>Was happy that it worked until I compared it with the values shown in Windows's Device Manager for the device. The baud rates from the Device Manager was:</p>
<pre><code>300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
</code></pre>
<p>I can understand that baud rate: 3000000 is not shown because I defined it myself but where did the rest of the values come from? I started wondering maybe it was defined in the driver. I started digging deeper and found FTDI's document
<a href="http://www.ftdichip.com/Support/Documents/AppNotes/AN232B-05_BaudRates.pdf" rel="nofollow noreferrer">AN232B-05 Configuring FT232R, FT2232 and FT232B Baud Rates</a>. Inside the document, it says about FTDI's driver configuration file (ftdiport.inf). I searched for it and found it under windows system folder. The entry looks like this:</p>
<pre><code>[FtdiPort.NT.HW.AddReg]
HKR,,&quot;ConfigData&quot;,1,11,00,3F,3F,10,27,00,00,88,13,00,00,C4,09,00,00,E2,04,00,00,71,02,00,00,38,41,00,00,9C,80,00,00,4E,C0,00,00,34,00,00,00,1A,00,00,00,0D,00,00,00,06,40,00,00,03,80,00,00,00,00,00,00,D0,80,00,00
</code></pre>
<p>It looks the same with the one shown in FTDI's document section 2.3, Aliasing Using the Additional FT232B Sub-Integer Divisors. Following the instruction in section 2.3 and translating the ConfigData from ftdiport.inf, I get the following baud rates:</p>
<pre><code>300, 600, 1200, 2400, 4800, 9600, 19230, 38461, 57692, 115384, 230769, 461538, 923076, 14406
</code></pre>
<p>Again, it is not the same as what the device manager is showing. Makes me wonder if the baud rate in the device manager is also hard coded.</p>
<p>I also tried counting how baud rate: 1800 from the device manager might look like in the ftdiport.inf file:</p>
<pre><code>Required divisor = 3000000/1800 = 1666.666
Divisor = 1666
Sub-integer divisors = 0.6666
Closest Sub-integer divisors = 0.625
Closest achievable baud rate = 3000000/1666.625 = 1800.045  
Error = (1800.045-1800)/1800*100 = 0.0025%
</code></pre>
<p>Error is well within the allowed +/-3% margin of error as stated in the document, so the data entry in ftdiport.inf should have something that looks like this:</p>
<pre><code>1666.625 Dec = 00014682 Hex
Data entry after re-order: 00014682 Hex =&gt; 82,46,01,00
</code></pre>
<p>Instead, [82,46,01,00] is nowhere to be found in ftdiport.inf file. I also performed dwMaxBaud from COMMPROP and got the result 10000000 which refers to:</p>
<pre><code>BAUD_USER (0x10000000): Programmable baud rate.
</code></pre>
<p>So this means that the user can use any baud rate they like as long as it fulfills the baud rate of both transmitter and receiver and are within margin of error, right? (Just making sure I'm not getting it wrong as my brain is starting to get a bit fuzzy)</p>
<h1>So by now, I'm very confused as I have 3 different results in my brain:</h1>
<ol>
<li>What does 10066B70 from my program mean?</li>
<li>Different results compared to the device manager's baud rate values with 10066B70 performing AND operations. Is this how it was meant to be done?</li>
<li>Different results from the device manager's baud rate values and the document from FTDI. I have no idea which one to trust as both are official?</li>
</ol>
<p>Thanks again for reading everything that I just wrote, I know it's quite long. Hopefully someone would be able to provide me with some answer to my questions.</p>
<p>*Note: I have not reach 10 reputation so I can't post more than 2 links, therefore I've put the links in the comment. Thanks again for your understanding.</p>