2 Replies - 179 Views - Last Post: 31 January 2018 - 04:12 AM Rate Topic: -----

#1 NesTeckie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-June 17

How detect/identify the serial port my device is connected to?

Posted 31 January 2018 - 03:16 AM

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: Get the names of the available ports
  • With a loop through these names:
  • assign the port name to the serial port instance of the form
  • send the detection command
  • wait for some time
  • check if some text has been received
  • compare the received message to the odentication message, 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.

' 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


Is This A Good Question/Topic? 0
  • +

Replies To: How detect/identify the serial port my device is connected to?

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: How detect/identify the serial port my device is connected to?

Posted 31 January 2018 - 03:54 AM

In what way does it not work? What happens or doesn't happen? Have you stepped through the code?

It is a poor approach to catch an exception but do nothing with it other than return False, particularly during development. You may have an exception but you don't know the error details.
Was This Post Helpful? 0
  • +
  • -

#3 NesTeckie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-June 17

Re: How detect/identify the serial port my device is connected to?

Posted 31 January 2018 - 04:12 AM

Hello,

Thanks for the reply. The GUI does not shut down and I just get a message box of the serial port not found. But you are right I didn't debug it as I should. I'll look into it and come back with more info.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1