none
Is SerialPort.ReadTimeout working on C#??!

    Question

  • Hello,

    I am creating a program on C# and I am using the function "string SerialPort.ReadTo(string value)" to read data from the serialport.
    This is supposed to read until a character or string that you provide.
    If it is not able to find this string it will return a Timeout Exception after the value in ms of SerialPort.ReadTimeout.

    My problem is that my code works fine only on pc`s that has the Visual Studio environment with C# installed.
    In every other PC I tried it, I am always instantly having the "System.TimeoutException" without reading any data at all and without waiting 2500ms for the TimeoutException to come up..

    My code is the following:


    Code Snippet

    public string ReceiveSerialData(SerialPort sp)

    {
                string Result = "";
                if (sp.IsOpen)
                {
                    try
                    {

    sp.ReadTimeout = 2500;
    sp.WriteTimeout = 2500;
              Result = sp.ReadTo("\r\n");
                    }
                    catch (System.TimeoutException ex)
                    {
               MessageBox.Show(ex.Message, "Receive Data Error. Read Timeout", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch { }
                }

    return Result;

    }



    Is there a way to implement proper ReadTimeout waiting times for Serial ports on C#?
    Friday, February 01, 2008 1:28 PM

All replies

  • It seems that the readtimeout problem exists only when I am using a Virtual COM Port from a Serial Ethernet Server I have!
    When I connect the device direclty on a physical port or a high quality USB to RS232 (I am saying high quality because on cheap USB to RS232 Converters I am always receiving *** data) everything is working perfectly!!

    Anyway, it is weird that I am encountering this problem only on computers without Visual Studion Installed...
    In the computer I am developing the program I do not have any readtimeout problems at all and I am using this Virtual Port for my development...
    Everything is working as it should be!
    Also on two more computers I tried just the executable there was no readtimeout problem! These two computers had also Visual Studio with C# environment installed..

    On two windows XP and one Windows Vista machine without Visual Studio Installed I am encountering this readtimeout problem..

    Maybe it was coincidence that it was working on the pc`s with Visual Studio installed?!?
    I don`t think so...
    I am installing now visual studio 2008 express on one of the machines I had the problems to do some more testing.
    Monday, February 04, 2008 9:25 AM
  • I know this reply is 2 years too late to help, but I must say that I have this same problem, and have had it for years.  I'm on VS2010, and we had it on VS2008.  The symptoms are exactly as you say:

    - No amount of increasing the ReadTimeout solves the problem.  I can set it to 10 seconds, but I get a timeout exception immediately upon the call to ReadChar().

    - Only happens on virtual com port devices.

    - Only happens on machines that do not have Visual Studio installed.

    My solution is to do Sleep(10) after writing data, before reading anything back.  I wonder if this is a problem with the SerialPort class implementation, or if the USB to com port drivers are responsible for the timeout handling, and they aren't doing it properly.  In my case, this is using the standard Microsoft USBSer.sys.

    • Proposed as answer by Moby Disk Wednesday, December 08, 2010 6:28 PM
    Thursday, September 30, 2010 9:23 PM
  • Prolific USB=>RS232 adapters with Prolific chips are ____.  Get one with an FTDI chip and I bet it'll work.
    Wednesday, October 27, 2010 9:49 PM
  • I've got the same problem, except it happens on my machine with VS2008 installed (Windows 7, 64 bit). I'm using a VLinx Ethernet Serial Server, and it times-out immediately even though ReadTimeout is set to 1000 milliseconds.  It must be a bug in the virtual com port driver, probably because it is using worker threads that are not synchronized.

    My only work around is to wait after a Write. In my testing 10 milliseconds doesn't work, 11 works a lot better, but still fails after a few iterations.

     

    I had to put in a conditional delay, based on BytesToWrite. I experimentally found that waiting 2 ms after BytesToWrite goes to 0 is still necessary.

     

     

     

    device.Write(...);
    
    while (device.BytesToWrite > 0) 
    {
    Thread.Sleep(2);
    }
    Thread.Sleep(2); device.Read(...)

     

     

     

    This is a "B&B Electronics" VLINX SP driver, version 3.1.0.0

     



    Monday, May 09, 2011 9:08 PM
  • Very known issues with SerialPort . It behaves unexpected with some serial ports and not all workarounds are perfect. Timing should be accurate and using "trial and error" to get a working delay on the actual PC is not the way to get a stable software. Different PC will return other values. Checking the SuperCom library one finds the answer to timing issues and serial hardware problems. The tech support is very fast & excelent, as one would expect for a commercial library. 

    Tuesday, November 08, 2011 11:03 AM