Working with USB to Serial port adapter on .NET

You can't solve a problem with the same kind of thinking that created it. — Albert Einstein (1879-1955)

Yesterday I spent a lot of time trying to get a speed radar to work on my .NET application. Every test was giving us diferent results. We made hundreds of tests, but every time we thought we found the problem, then the results of the test changed again.

So If you're working with USB to Serial ports using some devices (like a Decatur speed radar in my case), and you're getting some difficulties, there are some things that you can do to isolate the problem

FIRST OF ALL: Test the application on a computer that has a serial port, and connect the device directly without the USB to SERIAL adapter.

If the problem is sitll in there, well, you should check the power of the device, or some kind of bug in the program.

BUT if the problem is gone, there are two things that you should know.

  1) For some reason, some adapters can add some caracters like "\r" right before or after the content that is sent from the device.

So, as a rule, every time you make a ReadLine through a Serial port connection you should trim the data received.


    String data = sp.ReadLine().Trim();       // (Where sp is the serial port connection)


2) Some devices can connect with different baud speeds, but after the tests, we came to the conclusion that In spite of using the same model of devices, some of them should be connected with different baud Speeds. So, How do we know which speed we should use? we just keep testing! :).

    When you connect to a serial port with a baud speed that is not the one that likes to the device, probably it will let you open the port without any error, warning or exception, but once you're connected, if you try to make a sp.ReadLine() it will throw an Exception like System.TimeOutException (then you know you're probably working on a wrong baud speed).

    SO, What do we do? ...well, you can try something like this

                String data = sp.ReadLine().Trim();

                // Here you process the information received.
    catch (TimeoutException tex)
          String p = tex.Message;
                  // here you close the connection, and open it again with a different baud speed

                  if (sp.IsOpen)
                  if (sp.BaudRate == 9600)
                       sp.BaudRate = 115200;
                       sp.BaudRate = 9600;

    catch (Exception exc)


Any comments, questions and suggestions, fell free to contact me at: paterlinimatias (at) gmail (dot) com

Hope That Helps A Little!!!

Matias Paterlini



Comments have been disabled for this content.