question

FrancescoBernardini-0342 avatar image
0 Votes"
FrancescoBernardini-0342 asked FrancescoBernardini-0342 commented

'Ports' is not a member of 'MyComputer'

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





dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered FrancescoBernardini-0342 commented

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...



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot, I also missed the difference between Core and Framework which you now definitely clarified.

The code you suggested does not produce immediate errors so I'll try to pursue this way before switching back to FW 4.8

0 Votes 0 ·

It definitely worked, and now I'm finally able to intercept the tag of an RFID card connected via Arduino, upon scan.
196908-image.png

Thanks again!

0 Votes 0 ·
image.png (4.8 KiB)
DSPatrick avatar image
0 Votes"
DSPatrick answered FrancescoBernardini-0342 commented

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



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your answer,
actually I get stuck already at line 1, as I can't use My.Computer.Ports object

0 Votes 0 ·
JiachenLiMFST-9349 avatar image
1 Vote"
JiachenLiMFST-9349 answered FrancescoBernardini-0342 commented

Hi @FrancescoBernardini-0342 ,
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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dear Jiachen,
thanks a lot for your answer, maybe I have not been completely clear in my question:

I am actually using System.IO.Ports.SerialPort Class to define the variable:

 Dim com7 As IO.Ports.SerialPort = Nothing

But afterwards, when I try to assign it, say, COM7 serial port:

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

The part related to My.Computer.Ports gets marked as error
196809-image.png
and this is the description of the error (it says "'Ports' is not a member of 'MyComputer'")
196911-image.png


You mentioned that .NET FW5 should support My.Computer.Ports Object, I thought I was using that:

196893-image.png

Is there any other settings that I should make sure I have correctly specified, somewhere else in the menus?


0 Votes 0 ·
image.png (8.7 KiB)
image.png (7.4 KiB)
image.png (29.2 KiB)
JiachenLiMFST-9349 avatar image JiachenLiMFST-9349 FrancescoBernardini-0342 ·

Hi @FrancescoBernardini-0342 ,
May be there is some problem with my expression.
I mean My.Computer.Ports Object is only supported by .net framework4.8 and earlier.
The My.Computer.Ports Object cannot be used with .net 5.0 and later.
The Microsoft.VisualBasic.Devices Namespace does not contain the Ports class anymore.


0 Votes 0 ·

Thank a lot, I did not get the most important part!

I'll try to switch to 4.8 to solve my issue, I don't think I need to use the latest as the functionalities I'm looking for are very basic.

Anyway, could you kindly address me to some piece of documentation, or books, where it is explained how to deal with serial ports from FW5.0 on?

Thanks again!

0 Votes 0 ·

Dear Jiachen,
I am sorry to have unaccepted your answer, as it was still useful in its own regard. I accepted Castorix31's answer because he actually provided me with a solution that worked with my own setup.

If I could accept both I'd definitely do that.

Best regards

Francesco

0 Votes 0 ·