我们正在开发.Net Core中的应用程序,其中一个需要访问串行端口.
当我了解System.IO.Ports将不会在.Net Core中实现时,我正在寻找一个提供该功能的nuget库,但无法与.net内核兼容(VS Code显示错误消息) ).
那里有替代品吗?
更新:我发现正在考虑将官方SerialPort API移植到.Net Core(请参阅https://github.com/dotnet/corefx/issues/984)
当我尝试从 RaspberryPi 上的 Linux 中的串行端口 (uart) 读取数据时,在循环中我总是获得 5-10% 的 CPU 负载。由于串行端口应该是阻塞的,所以这不应该使用那么多的CPU负载,还是我错了?
我尝试了两个代码:
var port = new SerialPort("/dev/ttyUSB0", 57600);
port.Open();
while (true)
{
if (port.BytesToRead > 0)
{
while (port.BytesToRead > 0)
Console.Write($"{port.ReadByte().ToString("X2")} ");
Console.WriteLine("");
}
Thread.Sleep(100);
}
Run Code Online (Sandbox Code Playgroud)
static int blockLimit = 100;
static void Main(string[] args)
{
var port = new SerialPort("/dev/ttyUSB0", 57600);
port.Open();
byte[] buffer = new byte[blockLimit];
Action kickoffRead = null;
kickoffRead = delegate
{
port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
{
try
{
int actualLength …Run Code Online (Sandbox Code Playgroud)