dcsimg
Results 1 to 2 of 2

Thread: How detect/identify the serial port where my device is connected automatically?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    3

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

    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

  2. #2
    Fanatic Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    913

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

    Since you've gotten no replies yet, I assume this is via USB and wonder if it modifies the virtual com port name once plugged in (i.e., check in Device Manager)? If so, something like this should work and you won't have to poll each com port.

    Code:
        Public Function findComDevice(ByVal PartialName As String) As String
            Try
                Dim moReturn As Management.ManagementObjectCollection
                Dim moSearch As Management.ManagementObjectSearcher
                Dim mo As Management.ManagementObject
                moSearch = New Management.ManagementObjectSearcher("Select * from Win32_PnPEntity")
                moReturn = moSearch.Get
    
                For Each mo In moReturn
                    If IsNothing(mo.Properties.Item("Name").Value) Then Continue For
                    If CStr(mo.Properties.Item("Name").Value).ToUpper.Contains(PartialName.ToUpper) Then
                        'returns something like: "Prolific USB-to-Serial Comm Port (COM17)"
                        Dim IPRegEx As Regex = New Regex("(?<=\().*?(?=\))")
                        Return IPRegEx.Match(mo.Properties.Item("Name").Value.ToString).Value
    
                    End If
                Next
                Return Nothing
            Catch ex As Exception
                Messagebox.Show(ex.ToString)
                Return Nothing
            End Try
    
        End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width