Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net 5.0: can't use serial ports

I'm building an application for Windows, and I need to transfer data via serial port, but I can't figure out how to make it work. I tried different declarations, different imports, but nothing. Here are the 3 errors I get, from the 3 methods you can see in the pics:

'SerialPort1' is not declared. It could be inaccessible because of the protection level.\ 'SerialPorts' is not declared. It could be inaccessible because of the protection level.\ 'Ports' is not a member of 'MyComputer'.

Also, it doesn't signal it as an error, but I can't find System.IO.Ports in the 'nuGet package' folder, and it cannot be installed (and neither can its pre-requisites)

I have the latest version of the .NET framework: 5.0.100
Thanks to anyone that can help!

Imports used:

Imports System.Windows.Forms
Imports System
Imports System.IO.Ports

This sub is in a public class in the actual program

Sub GetSerialPortNames() Show all available COM ports. Get a list of serial port names.

    SerialPort1.Open() 'first way i tried

    Dim ports As String() = SerialPorts.GetPortNames()    'second way i tried

    ' Show all available COM ports. third way i tried
    For Each sp As String In My.Computer.Ports.SerialPortNames
        ListBox_Serial_Ports.Items.Add(sp)
    Next

End Sub
like image 231
paolo tassinari Avatar asked Dec 08 '22 09:12

paolo tassinari


1 Answers

Install System.IO.Ports directly from NuGet. It fixes the project and other issues with VS2019 and .net 5.0 that are present at the moment.

Install-Package System.IO.Ports -Version 5.0.0

or any higher stable version at the time

like image 185
Sebastian 506563 Avatar answered Dec 26 '22 02:12

Sebastian 506563