[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Serial Port ReadTimeout Property

Posted on 2009-02-18
2
Medium Priority
?
1,603 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

0
Comment
Question by:cmdolcet
  • 2
2 Comments
 
LVL 3

Expert Comment

by:ajnt__
ID: 23787347
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
0
 
LVL 3

Accepted Solution

by:
ajnt__ earned 750 total points
ID: 23788274
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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

873 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