Serial Port ReadTimeout Property

Posted on 2009-02-18
Last Modified: 2013-11-26
I have an application that will read the entire line of a string then parse it out accordingly, what im seeing is that is I set me ReadTimeout to a different number i think the serial port is closing both it gets all the data.... How can I Close the port or control the Readouttime better by just looking for a CRLF rather then an actual variable time.
Public Sub GetDSIInput(ByVal serialPort As IO.Ports.SerialPort, ByVal pfile As PartfileLibrary.Gages)

        Dim bytes(4096) As Byte


            serialPort.ReadTimeout = 5000


            Dim strData As String = String.Empty

            If Not serialPort.IsOpen Then

                strData = String.Empty


                strData = serialPort.ReadLine

            End If

            If strData.Length > 0 Then

                Dim tempArray() As String

                strData = strData.Replace(vbCrLf, vbTab).Replace(Chr(26), "").Replace(Chr(12), "").Replace(Chr(13), "")

                'parses out the information gathered from the COM port to a temp location

                tempArray = Split(strData, vbTab)

                If serialPort.BaudRate = 9600 Then

                    DSICollector = "585 Plus"



                    DSICollector = "501/440"


                End If

            End If

        Catch tex As TimeoutException

            'do nothing as this will happen while waiting

        Catch ioError As System.IO.IOException

            'do nothing as this will occur sometimes when closing the thread

        Catch ex As Exception


        End Try

    End Sub

Open in new window

Question by:cmdolcet
    LVL 3

    Expert Comment

    Hi cmdolcet,

    I have written some software to do exactly this, I'm just booting up the old system to get the source code for you...

    LVL 3

    Accepted Solution

    Below is just part of the solution as I can't release the whole lot.
    All that happens is we keep calling comPort.ReadExisting inside a loop and then when we receive "EOF" we stop listening and save all the data.

    So you should be able to change EOF to VbCrLf and get rid of the EndsWith.

    Sorry it's in C#, let me know if you need it in VB.Net, the concept is quite simple.

    while (true)
    				if (((BackgroundWorker)sender).CancellationPending)
                        string message = comPort.ReadExisting();
                        gloMessage = gloMessage + message;
    					if (!string.IsNullOrEmpty(gloMessage))
                            // find out if the right most 3 characters on the message are "EOF"
                            if (gloMessage.EndsWith("EOF"))
                                string filePath = GetFileFullPath();
    gloMessage.Substring(int2DigTo+1, gloMessage.Length - (int2DigTo+1));
                                System.IO.File.WriteAllText(filePath, gloMessage + Environment.NewLine);
                                if (!UploadFile(filePath, Settings.Default.FTPFileName))
                                    MessageBox.Show("FTP Error", "Unable to upload file to the FTP server. See logs for details.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    gloMessage = "";
                                gloMessage = ""; 
    				catch (TimeoutException) { }
    				catch (Exception ex)
    					MessageBox.Show("COM error", "COM listening failed. See logs for details.", MessageBoxButtons.OK, MessageBoxIcon.Error);

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now