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
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