.net 5.0: can't use serial ports
Asked Answered
D

1

5

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
Death answered 19/11, 2020 at 16:5 Comment(0)
S
18

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

Springhouse answered 21/2, 2021 at 19:20 Comment(1)
Same issue and working solution with VS2022 / .net 6.0. Interesting. Are serial ports that exotic?Cornellcornelle

© 2022 - 2024 — McMap. All rights reserved.