Hello!

I am programming a windows form that will communicate with a microcontroller. After the winform is loaded, it should automatically detect the comport where the microcontroller is connected.

What I am doing is:
  1. Get the names of the available ports
  2. With a look through these names:
  3. assign the port name to the seiral port instance of the form
  4. send the detection command
  5. wait for some time
  6. check if some text has been received
  7. compare the received message to the odentication message
  8. if it the right one break the loop and return the reult, if not continue.


Below is the code of the function. I am using thread.sleep method. But the function is not working and I don't detect the board when it is there.

Can You tell me what is wrong with the function? Is the time delay blocking the reception? how should I ameliorate it?

This function is excuted at the beginning after loading the form. If I don't find the port, nothing can go forward. On the other side, there are no other threads I have to take into account at that stage of the excution. I thought about the DatarRceived Event, but it does not make sense at this stage, it will be activated after the right port has been detected. Please let me know what you think

thank you!

PS: Now I can connect manually from the GUI if the automatic connect does not workl´. But the main goal at this point is to do it automatically.



Code:
   ' FUNCTION TO FIND OUT THE COM PORT OF THE XMC
    ' SEND THE USER COMMAND TO ALL AVAILABLE COM PORTS TILL THE RETURN MESSAGE IS RECOGNIZED
    Public Function connect(testport As SerialPort, recognizeText As String, userCommand As String) As Boolean
        Dim intReturnASCII As Integer = 0
        Dim charReturnValue = Chr(intReturnASCII)
        Dim returnMessage As String = ""
        Dim count As Integer = 0

        Dim ports As String() = IO.Ports.SerialPort.GetPortNames
        If testport.IsOpen Then
            testport.Close()
        End If

        Try
            ' LOOP THROUGH THE PORTS 
            For Each newport As String In ports
                testport.PortName = newport

                testport.Open()
                'SEND COMMAND 
                testport.Write(STX & userCommand & ETX)
                Thread.Sleep(200) ' stop the userform and wait for the reception of the response 
                count = testport.BytesToRead
                While count > 0
                    intReturnASCII = testport.ReadByte
                    returnMessage = returnMessage + Convert.ToChar(intReturnASCII)
                    count -= 1
                End While

                testport.Close()
                XMCPort = newport ' Danach instantiate the serial port publicly with port name , is true instantiate 

                If returnMessage.Contains(recognizeText) Then
                    Return True
                End If
            Next
            Return False
        Catch ex As Exception
            Return False
        End Try
        count = 0
        returnMessage = ""
    End Function