Re: Trapping for error and displaying receive buff - WeOnlyDo Discussion board

Re: Trapping for error and displaying receive buff (General questions)

by Eric, Monday, December 10, 2007, 16:05 (6194 days ago) @ Eric

[size=2]Method 2 - Cleaner Code with WaitFor command[/size]

I have reworked the script to work with WiatFor commands. This is a little cleaner code as it allows you to follow the coding better.

Prompt commands need to be defined first before the Send command. This means that you have to define the text desired to be received before the command to answer the current screen. This can get confusing and you have to make sure you debug often. The reason is that this method requires a PromptReceived event handler to be set up to trap for the prompt events and then you also need a counter to make sure you get to the proper itteration code to hadle the event. This is a little more difficult to follow than the WaitFor method.


WaitFor commands are great as you can itterate through the code directly. You do not need an event handler because the program pauses and waites for the desired string to be found. Once the string is found the program continues with the next line of code . Ths only thing to remember here is that there is a timeout that you will have to account for to handle the errors properly or else your code will fail inadvertantly.

Solution:
[code]
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Dim myError As System.Exception

Telnet1 = New WODTELNETDLXCOMLIB.wodTelnetDLXComClass

Telnet1.Timeout = 4

Telnet1.Hostname = textBox2.Text
Telnet1.Login = textBox3.Text
Telnet1.Password = textBox4.Text
Telnet1.Protocol = WODTELNETDLXCOMLIB.ProtocolsEnum.Telnet
Telnet1.Blocking = 1
Telnet1.StripANSI = True

Telnet1.Connect()

On Error GoTo ErrorHandler
Telnet1.WaitFor( p5104 - Select: )
AddToList( Menu 1: T - Training QAD and Eagle Database Menu & vbCrLf)
Telnet1.Send( T & vbLf)
Telnet1.WaitFor( p5104 - Select: )
AddToList( Menu 2: e51 - Eagle & vbCrLf)
Telnet1.Send( e51 & vbCrLf)
Telnet1.WaitFor( Dev )
AddToList( Menu 3: CrLf - Eagle Header Screen & vbCrLf)
Telnet1.Send(vbCrLf)
Telnet1.WaitFor( Exit )
AddToList( Menu 4: s51 - Login User Name? & vbCrLf)
Telnet1.Send( s51 & vbCrLf)
Telnet1.WaitFor( Password )
AddToList( Menu 5: s51eagle - Login Password? & vbCrLf)
Telnet1.Send( s51eagle & vbCrLf)
Telnet1.WaitFor( Exit )
AddToList( Menu 6: 51 - Login Site? & vbCrLf)
Telnet1.Send( 51 & vbCrLf)
Telnet1.WaitFor( Select )
AddToList( Menu 7: 3 - Prod Menu......3 & vbCrLf)
Telnet1.Send( 3 & vbCrLf)
Telnet1.WaitFor( Select )
AddToList( Menu 8: 3 - Rep Menu 1.....3 & vbCrLf)
Telnet1.Send( 3 & vbCrLf)
Telnet1.WaitFor( Select )
AddToList( Menu 9: bkn - Kanban BKF...BKN & vbCrLf)
Telnet1.Send( bkn & vbCrLf)
Telnet1.WaitFor( Employee )
AddToList( Menu 10: wrkrb - Employee Id? & vbCrLf)
Telnet1.Send( wrkrb & vbCrLf)

Exit Sub

ErrorHandler:
AddToList( Error: & Err.Number & vbLf)
AddToList(Err.Source & vbLf)
' Assigns the exception from the Err object to myError.
myError = Err.GetException()
' Displays the message associated with the exception.
AddToList(myError.Message & vbLf)

Resume Next
End Sub
[/code]

I hope this helps others in how these commands work together.

Regards,
Eric


Complete thread: