cmdolcet
asked on
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please try with this:
Maybe the loop is not required.
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
Maybe the loop is not required.
ASKER
yv989c: this code once it turns off my first device in 6 it will just lock my PC up
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?
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?
ASKER
It breaks on the ReadLine (). If I set a timeout to 5 secs then it will jsut time out and error out.
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
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
ASKER
I need to check that its off. It wont always turn the device off if I send the command [STRM] too fast.
Hi, and this dont works?
The purpose of this code is to wait until you dont get more data from your dvice?
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
The purpose of this code is to wait until you dont get more data from your dvice?
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
This will wait until your device stop sending data.
ASKER
It works but it will always leave one device one. Is there anything else I can check?
ASKER
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
Try this code:
Also, are you sure that ActiveSerialPorts contains all the active ports?
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
Also, are you sure that ActiveSerialPorts contains all the active ports?
ASKER