Click to See Complete Forum and Search --> : Timer


bryan822
June 9th, 2009, 08:59 AM
I am writing a program with Visual Basic 2005 that talks back and forth with an external machine using UDP/IP. One of the sets of code I need to send to the machine is more or less a random set of numbers. Since I have no way of knowing exactly what that code is going to be when I send it I would like to set up my program so that it tries several different codes by trial and error. So basically it will send one code, try to read from the machine, and if nothing is being sent back from the machine then it has failed and will try a different set of code. I had considered a Try Catch. The problem is that the computer program just continues to try to read from the machine and gets locked up. It never actually fails. Is there a way I can set it so that it only tries to read for say 2 seconds and then if it's still on that step of the code it cancels and tries to move on to something else?


Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(112) & Chr(0) & Chr(0) & Chr(0) & Chr(4) _
& Chr(0) & Chr(23) & Chr(0) & Chr(11) & Chr(1) & Chr(1) & Chr(0) & Chr(6) & Chr(0) & Chr(2) & Chr(1) _
& Chr(6) & Chr(0) & Chr(11) & Chr(180) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) _
& Chr(10) & Chr(0) & Chr(0) & Chr(0))
udpClient.Send(sendBytes, sendBytes.Length)

Dim RemoteIpEndPoint7 As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes7 As [Byte]() = udpClient.Receive(RemoteIpEndPoint7)
Dim returnData As String = Encoding.Default.GetString(receiveBytes7)
Dim MyString As String = returnData
Dim TmpString As String = ""
Dim HexString As String = ""
Dim OutputString As String = ""
Dim X As Integer
For X = 0 To MyString.Length - 1
HexString = Asc(MyString.Substring(X, 1))
TmpString = Hex(HexString).ToString.PadLeft(2, "0")
OutputString = OutputString & TmpString
OutputLength = OutputString.Length
Next

DataMiser
June 9th, 2009, 09:22 AM
Check to see if there is data at the socket before you issue a read.

bTimeOut = TimeOfDay
Do While wSocket.Poll(1, SelectMode.SelectRead) = False
bTimeTaken = DateDiff(DateInterval.Second, bTimeOut, TimeOfDay)
If bTimeTaken > tTimeout Then
Return False
End If
Application.DoEvents()
Loop

Place your read after the loop and it will only read when the data is there and will exit if the data does not get there before the timeout expires.

bryan822
June 9th, 2009, 01:46 PM
I have tried your method of polling. I'm not having much luck with it though. Will this method only return true if it's readable or only when there's actually something to read? If that makes any sense to you. I guess what I'm saying it that the device is readable. The thing is that it's not actually sending information back to my computer and so there is nothing to read. Let me know if that makes any sense and I will try harder to make my self clear. Thanks.

dglienna
June 9th, 2009, 06:59 PM
You could search for SelectMode.SelectRead if it doesn't make immediate sense. I just did, and found this on MSDN

'Creates the Socket for sending data over TCP.
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' Connects to host using IPEndPoint.
s.Connect(EPhost)
If Not s.Connected Then
strRetPage = "Unable to connect to host"
End If
' Use the SelectWrite enumeration to obtain Socket status.
If s.Poll(- 1, SelectMode.SelectWrite) Then
Console.WriteLine("This Socket is writable.")
Else
If s.Poll(- 1, SelectMode.SelectRead) Then
Console.WriteLine(("This Socket is readable. "))
Else
If s.Poll(- 1, SelectMode.SelectError) Then
Console.WriteLine("This Socket has an error.")
End If
End If
End If



http://msdn.microsoft.com/en-us/library/y9x13hh4.aspx

DataMiser
June 9th, 2009, 09:30 PM
The liitle piece of code I posted above will loop until data is present to read or until it reaches the timeout.

If selectread is true then there is data available to be read and you can safely issue a read without causing yoru program to hang. If no data within timeout period it exits the function with a return value of false.