Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.IO.Ports.SerialPort in .NET Core 1.1

I'm trying to create an console application using visual studio code with .Net Core and get all available ports. How to use System.IO.Ports.SerialPort class in visual studio code? I try to declare it with using statement but the only available is Compression and MemoryMappedFiles in namespace System.IO.

I'm using .Net Core 1.1.1 SDK

like image 902
jmvtrinidad Avatar asked Mar 10 '17 06:03

jmvtrinidad


3 Answers

If you are using linux OS you can use SerialPortStream library. It requires the libserial binaries and I was able to compile it only for linux (not for MacOS or Windows).

Other way is using the Mono implementation and Mono.Posix.NETStandard package. This way works only for NETStandard 2.0 projects.

I copied sources of System.IO.Ports classes from Mono into my NETStandard 2.0 project, added the Mono.Posix.NETStandard, Microsoft.Win32.Registry references and included <AllowUnsafeBlocks>true</AllowUnsafeBlocks> section into my .csproj file. And it works on MacOs and Windows.

like image 180
dima117 Avatar answered Nov 12 '22 00:11

dima117


For those coming to this post some years later...

Since .NET Core 2.0 it's possible to use the System.IO.Ports package.

like image 6
Drew Noakes Avatar answered Nov 12 '22 01:11

Drew Noakes


Please use the .NET API Browser. SerialPort not present for .NET Core 1.1: https://learn.microsoft.com/en-us/dotnet/api/?term=serialport&view=netcore-1.1 but it was added in .NET Core 2.0: https://learn.microsoft.com/en-us/dotnet/api/?term=serialport&view=netcore-2.0 You can always add reference to System.IO.dll from main Framework to get the SerialPort, but then You are not .NET Core 1.1 conform (cannot port to Linux, MacOS etc)

like image 2
Dmitri Kuznetsov Avatar answered Nov 12 '22 00:11

Dmitri Kuznetsov