How to clear a COM buffer using VB 2005

I am trying to shut off a piece of hardware when I exit my program. I may have a infinate amount of Hardware connected to my PC USB port so I need to loop thru all active serial ports in my array.

The problem I am having is when I look at my shutoff_ response variable. I will send a command ("[STRM] ") to shut off the hardware device. I look at my COM posrt and see if I can read anything accross it. It should be empty. However I am still getting data this is why I added the do until loop. Even with the do until loop I still get data in the port buffer. How can I make sure to clear this port buffer completely before reading it again.

'Gets and sets all active Digital Indicator COM Ports for use of the [STRM] command
    Public Sub Close_Active_Indicator_Streams()
        Try
            Dim intloop As Integer
            If masterSettings.masterRuntime.masterSingleIndicatorRead = True Then
            Else
                If ActiveSerialPorts Is Nothing Then
                Else
                    For intloop = 0 To ActiveSerialPorts.Count - 1
                        Dim serialPort As New SerialPort()
                        serialPort = ActiveSerialPorts(intloop)
                        Shut_Off_Indicators(serialPort)
                        System.Threading.Thread.Sleep(500)
                    Next
                    StreamCommand_Sent = True
                    ProbesZero = True
                End If
            End If
        Catch ex As Exception
            tListener.AddMethodError(ex)
        End Try
    End Sub

  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        Dim shutoff_response As String = String.Empty
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        System.Threading.Thread.Sleep(1000)
        shutoff_response = serialPort.ReadExisting
        If Not shutoff_response = "" Then

            Do Until String.IsNullOrEmpty(shutoff_response) 'shutoff_response = ""
                serialPort.Write("[STRM]")
                System.Threading.Thread.Sleep(100)
                shutoff_response = String.Empty
                shutoff_response = serialPort.ReadExisting
            Loop
        End If
    End Sub

Open in new window

cmdolcetAsked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
0
 
cmdolcetAuthor Commented:
If I use the DiscardIn Buffer method I still read values in my buffer.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Please try with this:
  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        Dim shutoff_response As String = String.Empty
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        'System.Threading.Thread.Sleep(1000)
        shutoff_response = serialPort.ReadLine()
        If Not String.IsNullOrEmpty(shutoff_response) Then
            serialPort.DiscardIn()
            Do Until String.IsNullOrEmpty(shutoff_response) 'shutoff_response = ""
                serialPort.Write("[STRM]")
                'System.Threading.Thread.Sleep(100)
                shutoff_response = serialPort.ReadLine()
            Loop
        End If
    End Sub

Open in new window


Maybe the loop is not required.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
cmdolcetAuthor Commented:
yv989c: this code once it turns off my first device in 6 it will just lock my PC up
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
To avoid lock your pc, please set the SerialPort.ReadTimeout property to a reasonable value (maybe 5000 = 5 seg)
mmm, please debug that method, what is happen there? is entering into the loop? serialPort.ReadLine() always return a value? or in what line it is freezing?
0
 
cmdolcetAuthor Commented:
It breaks on the ReadLine (). If I set a timeout to 5 secs then it will jsut time out and error out.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Well, is hard to me do this without know your device response, but I will try to give you a solution, try this:
  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        'System.Threading.Thread.Sleep(1000)
         Do Until serialPort.ReadByte() >= 0
             serialPort.Write("[STRM]")
             System.Threading.Thread.Sleep(100)
         Loop
    End Sub

Open in new window

I dont know your device behavor, but I have a question, if you are sending a command that turn off it, why you dont simply:
  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        serialPort.Close()
    End Sub

Open in new window

0
 
cmdolcetAuthor Commented:
I need to check that its off. It wont always turn the device off if I send the command [STRM] too fast.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, and this dont works?
  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        System.Threading.Thread.Sleep(1000)
         Do Until serialPort.ReadByte() >= 0
             System.Threading.Thread.Sleep(100)
         Loop
         serialPort.Close()
    End Sub

Open in new window


The purpose of this code is to wait until you dont get more data from your dvice?
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Oh my last code is wrong, try this instead:
  Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        System.Threading.Thread.Sleep(1000)
         Do Until serialPort.ReadByte() < 0
             System.Threading.Thread.Sleep(100)
         Loop
         serialPort.Close()
    End Sub

Open in new window

This will wait until your device stop sending data.
0
 
cmdolcetAuthor Commented:
It works but it will always leave one device one. Is there anything else I can check?
0
 
cmdolcetAuthor Commented:
Sorry but this last code does nothing. It hangs my system. The previous code snippet you had worked better but it always left a device on.
Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If
        serialPort.Write("[STRM]")
        System.Threading.Thread.Sleep(1000)
         Do Until serialPort.ReadByte() < 0
             System.Threading.Thread.Sleep(100)
         Loop
         serialPort.Close()
    End Sub

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Try this code:
Public Sub Shut_Off_Indicators(ByVal serialPort As SerialPort)
	If Not serialPort.IsOpen() Then
	    serialPort.Open()
	End If
	Try
	    serialPort.NewLine = vbCr
	    serialPort.ReadTimeout = 1000
	    serialPort.Write("[STRM]")
	    System.Threading.Thread.Sleep(1000)
	    Do Until String.IsNullOrEmpty(serialPort.ReadLine())
		System.Threading.Thread.Sleep(500)
	    Loop
	Catch ex As System.TimeoutException
	Finally
	    serialPort.Close()
	End Try
End Sub

Open in new window


Also, are you sure that ActiveSerialPorts contains all the active ports?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.