Results 1 to 8 of 8

Thread: Access SerialPort by string name

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Access SerialPort by string name

    Hello,

    I have some SerialPorts components COM1 to COM10.
    I have a list containing the names as strings "COM1", "COM2" ... "COM10"
    I am trying to iterate the list and send data eg:

    Code:
    For i As Integer = 0 To myPorts.Count - 1
           myPorts(i).Write("TEXT")
    Next
    The error is 'Write' is not a member of 'String'

    I found out that for other elements on the form i can use Me.Controls to access them with strings,
    but Serial Ports does not appear in Me.Controls. How can it be done?

    Thanks!

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    769

    Re: Access SerialPort by string name

    I have never iterated through a list of COM ports like that. This is the basic serial port code I have used:

    Code:
         Dim sp1 As New IO.Ports.SerialPort
            sp1.PortName = "COM1"
            sp1.Open()
            sp1.Write("Test")
            sp1.Close()
    This might give you some ideas.
    Last edited by paulg4ije; Feb 2nd, 2025 at 05:43 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: Access SerialPort by string name

    Code:
         Dim sp1 As New IO.Ports.SerialPort
            sp1.PortName = "COM1"
            sp1.Open()
            sp1.Write("Test")
            sp1.Close()
    This might give you some ideas.[/QUOTE]

    Thank you for your answer, i writed the same code 10 times instead of iterating, as a workaround,
    but is there really no way to access 'sp1.Open()' from your example, with a variable or string like 'sp' & '1'.Open() instead ?

    Thanks

  4. #4
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    769

    Re: Access SerialPort by string name

    Try something like this:

    Code:
            Dim ComPorts() As String = {"COM1", "COM3", "COM4", "COM5"}
    
            Dim sp1 As New IO.Ports.SerialPort
    
            For Each s As String In ComPorts
                sp1.PortName = s
                sp1.Open()
                sp1.Write("Test")
                sp1.Close()
            Next
    I created an array containing the COM ports on my PC. I can't easily test that the data actually arrives at the intended port, but it runs without errors.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: Access SerialPort by string name

    Yes. methods as String works, i need to acces the Object with a string, i have independent ports that needs to stay opened to receive data, its something like this:

    Dim sp1 As New IO.Ports.SerialPort 'already configured as COM1
    Dim sp2 As New IO.Ports.SerialPort 'already configured as COM2
    Dim sp3 As New IO.Ports.SerialPort 'already configured as COM3
    Dim sp4 As New IO.Ports.SerialPort 'already configured as COM4

    Lets say i have an input 'Close port:' and user enters "3".

    How to access the object sp3 by concatenating 'sp' and the user's input '3' ?
    or even if user inputs the full name of the Object like 'sp3', it will be a String, how do we access then sp3.Close() ?

    Ofc in this simple and intuitive example there are workarounds like:
    If input = 1 Then sp1.Close()
    If input = 2 Then sp2.Close()
    If input = 3 Then sp3.Close()

    But isnt there a method to access the object if we only know its name as a String?

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: Access SerialPort by string name

    You might want to consider something like a Dictionary https://learn.microsoft.com/en-us/do...2?view=net-9.0

    You could do something like
    Code:
    Dim ports as new Dictionary(Of String, SerialPort)
    
    Dim sp As New IO.Ports.SerialPort
    sp.PortName = "COM1"
    
    ports.Add("1", sp)
    
    'To get port 1 use
    Dim port = ports("1")
    port.Open()
    'etc

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,467

    Re: Access SerialPort by string name

    The most basic way would be to use an Array and match the index with the port number.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: Access SerialPort by string name

    The Dictionary method does indeed what i am looking for..

    Thank you all !

Posting Permissions

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



Click Here to Expand Forum to Full Width