Public time As New Stopwatch
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(SerialPort.GetPortNames().Length)
Try
SerialPort1.Open()
Catch ex As Exception
If SerialPort.GetPortNames().Length <= 0 Then
MessageBox.Show("No ports were found.", "No ports to open.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
MessageBox.Show(ex.Message, "Couldn't open port.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sub Input(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
SerialPort1.DtrEnable = False
time.Start()
BackgroundWorker1.RunWorkerAsync()
End Sub
Sub WaitForSignal() Handles BackgroundWorker1.DoWork
While SerialPort1.DsrHolding = True And time.ElapsedMilliseconds < 1501
Thread.Sleep(100)
End While
End Sub
Sub SignalFound() Handles BackgroundWorker1.RunWorkerCompleted
time.Stop()
Label1.Text = time.ElapsedMilliseconds.ToString
SerialPort1.DtrEnable = True
End Sub