'Ports' is not a member of 'MyComputer'

Francesco Bernardini 21 Reputation points
2022-04-26T21:20:46.087+00:00

Hi everyone,

I feel this is a very trivial question, but unfortunately I've not been able to find a solution anywhere on the net.

I am doing basic things with serial ports, so I imported System.IO.Ports in order to obtain the basic classes and defining the needed variables.

However, when I try to initialize a SerialPort object assigning it something like

My.Computer.Ports.OpenSerialPort("COM1")

I get the error message

" 'Ports' is not a member of 'MyComputer' "

and indeed, if I type My.Computer. and scroll through the options, I see every other voice (Mouse, Keyboard, Network, etc.) but not Ports.

Is there any component I may install in the solution, in order to have 'Ports' appear under My.Computer?

I am programming a Windows App under .NET 5.0 framework, using Visual Basic.

Thank you in advance for your help

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,581 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,831 Reputation points
    2022-04-27T08:39:06.417+00:00

    As you said you use .NET 5 (which is NET Core, not .NET Framework), you cannot use My.Computer.Ports
    You add the Nuget Package System.IO.Ports 6.0.0
    Then you can test :

        Dim serialPort As IO.Ports.SerialPort = New IO.Ports.SerialPort()
        serialPort.PortName = "COM1"
        serialPort.Open()
        Dim bOpen = serialPort.IsOpen
        Dim nBaudRate = serialPort.BaudRate
        Debug.WriteLine(String.Format("Baud Rate : {0}", nBaudRate.ToString()))
        ' etc...
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,671 Reputation points Microsoft Vendor
    2022-04-27T01:39:03.37+00:00

    Hi @Francesco Bernardini ,
    I tested based on .net framework4.8, .net 5, .net 6.
    Only .net framework supports My.Computer.Ports Object.
    You can choose to use the System.IO.Ports.SerialPort Class instead.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Dave Patrick 426.1K Reputation points MVP
    2022-04-26T22:32:48.957+00:00

    I'm not using .Net 5.0 but in a recent project I used

            For Each sp As String In My.Computer.Ports.SerialPortNames
                If UCase(sp) = UCase(sPort.PortName) Then
                    If Not sPort.IsOpen Then
                        sPort.Open()
                    End If
                End If
            Next
    

    sPort.PortName was setup / defined elsewhere in app.config