I have been trying to find a way to break a loop that is continually polling a modem with a keystroke via a button control or just a ESC key. I'm trying to break the loop 'Do While x<4'. I would normally set it to run until a keystroke or button press by changing it to "Do while True".
Any help would be greatly appreciated.
ps. port1 is declared public in a module for storing the commport name ex:"COM1"
vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim x As Integer = 1
Do While x < 4
Dim n As Integer = 0
Dim strng2 As String = ""
Using serialport1 As SerialPort = My.Computer.Ports.OpenSerialPort(port1)
serialport1.DtrEnable = True
serialport1.RtsEnable = True
'Dim start, finish, totalTime As Double
Do While n < 3 '4 loops then exit and come back on timer
If TextBox1.Text = "" Then
Else
Dim strng As String = TextBox1.Text
serialport1.Write(strng + vbCrLf) ' All data transfer code goes here.
taketime(0.2)
TextBox1.Text = ""
End If
taketime(0.2)
If serialport1.BytesToRead = 0 Then
Else
strng2 = serialport1.ReadExisting ' All data transfer code goes here.
taketime(0.2)
TextBox2.Select()
TextBox2.Text = strng2
Exit Do
End If
n = n + 1
Loop
End Using
taketime(1) 'poll modem every 1 seconds
x = x + 1
'Dim ee As System.EventHandler
'If ee. = Chr(27) Then
' exit try
'End If
' my attempt above to catch esc key^
'
Loop
SerialPort1.DtrEnable = False
SerialPort1.RtsEnable = False
SerialPort1.Close()
Catch ex As Exception
MsgBox("The port is in use, close the port and try again.")
Exit Try
End Try
SerialPort1.DtrEnable = False
SerialPort1.RtsEnable = False
SerialPort1.Close()
ProgressBar2.Value = 0
End Sub
This post has been edited by PsychoCoder: 1 May, 2008 - 02:52 PM