We are developing applications in .Net Core and one of them require to access a serial port.
As I learned that System.IO.Ports won't be implemented in .Net Core, I was looking for a nuget library that supplies that functionality, but couldn't get one compatible with .net core (VS Code is showing an error message).
Is there any alternative out there?
UPDATE: I found that the official SerialPort API is being taken into consideration for porting to .Net Core (see https://github.com/dotnet/corefx/issues/984)
This is now fully cross platform in 2021.
Simply NuGet install either using the command line or your IDE : "System.IO.Ports"
The following example is a .NET 5 console mode program that compiles and works on both Linux and Windows.
using System;
using System.IO.Ports;
namespace PipelinesTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reading a GPS Device on COM3");
SerialPort _port = new SerialPort("COM3", 4800, Parity.None, 8, StopBits.One);
_port.DataReceived += PortOnDataReceived;
_port.Open();
Console.WriteLine("Press Return to Exit");
Console.ReadLine();
_port.Close();
_port.DataReceived -= PortOnDataReceived;
Console.WriteLine("Ended");
}
private static void PortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = sender as SerialPort;
var line = port.ReadLine();
Console.WriteLine(line);
}
}
}
NOTE: I should have mentioned when I first answered this, you'll need to change the "COM3" in the constructor above to something like "/dev/ttys0" or similar for Linux. You MIGHT also have to run your app as Root/SU on a Linux machine, this is just the nature of Linux's multi user security unfortunately.