We help IT Professionals succeed at work.

Serial Port ReadTimeout Property

Medium Priority
2,346 Views
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
        Try
            serialPort.ReadTimeout = 5000
            serialPort.DiscardInBuffer()
 
            Dim strData As String = String.Empty
 
            If Not serialPort.IsOpen Then
                strData = String.Empty
            Else
                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"
                    Get585PlusData(tempArray)
                Else
                    DSICollector = "501/440"
                    Get501Data(tempArray)
                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
            tListener.AddMethodError(ex)
        End Try
    End Sub

Open in new window

Comment
Watch Question

Commented:
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...

Ajnt
Commented:
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)
				{
					break;
				}
 
				try
				{
 
                    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);
                            filesReceived++;
 
 
                            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 = "";
                                break;
                            }
                               
                            gloMessage = ""; 
                            
                        }
                        						
					}
				}
				catch (TimeoutException) { }
				catch (Exception ex)
				{
					CommonLib.Debugging.Trace.Log(ex);
					MessageBox.Show("COM error", "COM listening failed. See logs for details.", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
					break;
				}
			}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.