.NET core 3.0 System.IO.Ports SerialPort始终在RPI上使用5-10%的CPU - c#

当我尝试在RaspberryPi上的Linux中从串行端口(uart)读取时,在循环中时,我总是会得到5-10%的CPU负载。由于SerialPorts应该被阻塞,因此不应使用那么多的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);
}

进阶密码

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 = port.BaseStream.EndRead(ar);
            byte[] received = new byte[actualLength];
            Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
            raiseAppSerialDataEvent(received);
        }
        catch (IOException exc)
        {
            handleAppSerialError(exc);
        }
        kickoffRead();
        }, null);
    };
    kickoffRead();

    while (true)
        Thread.Sleep(1000);
}

private static void handleAppSerialError(IOException exc)
{
    throw new NotImplementedException();
}

private static void raiseAppSerialDataEvent(byte[] received)
{
    Console.WriteLine(BitConverter.ToString(received));
}

两者具有相同的结果:两个进程同时使用5%到10%的CPU负载
.NET core 3.0 System.IO.Ports SerialPort始终在RPI上使用5-10%的CPU - c#

在运行.NET Core 3.0 Preview 2System.IO.Ports 4.6.0-preview-19073.11上使用RaspberryPi 3b+HypriotOS 1.10.0

参考方案

到目前为止(NET Core 3.1),SerialPort实现非常占用CPU。我已经根据dima117的响应将Mono SerialPort移植到了Net Standard库中
.NET Core - Use System.IO.Ports.SerialPort in visual studio code

我已将其发布到github:

https://github.com/michaldobrodenka/System.IO.Ports.Mono

通过此SerialPort实施,我的全能型硬件H3的CPU使用率从25%下降至5%

如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#

我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…

如何在ASP.NET Core(使用JavascriptService)应用程序中使Node.js代码调用csharp代码? - c#

我正在将asp.net内核用于简单的Web api服务器(实际上是使用Deepstream)。虽然C#可以使用NodeServices.InvokeExportAsync完美地调用nodejs代码,但是当我尝试将Action / Func作为NodeServices.InvokeExportAsync的参数传递给nodejs时,却得到了System.Aggr…

从.NET Core 2.1降级到.NET 4.7.1时,如何使用IApplicationBuilder和IServiceCollection? - c#

我不得不将我的项目从.NET Core 2.1更改为.NET 4.7.1,并且修复了几乎所有错误,但以下错误仍然困扰着我 “ IApplicationBuilder”不包含“ UseHsts”的定义,也找不到找不到接受类型为“ IApplicationBuilder”的第一个参数的扩展方法“ UseHsts”(是否缺少using指令或程序集引用?) “ IA…

在ASP.NET Core 2.1中添加自定义TagHelpers - c#

我严格按照ASP.NET Core文档进行操作,并花费了大量时间来拖曳堆栈溢出,试图实现简单的自定义TagHelper,但没有成功。任何人都可以就任何陷阱或已知错误提出建议吗?应用程序属性:AssemblyName: AmpWeb Target Framework .NET Core 2.1 NuGet软件包Microsoft.AspNetCore.All …

从Axios请求以ASP.NET Core API返回下载文件 - c#

大家好我正在尝试从Axios Request从ASP.NET Core Web API下载文件。这是我的示例API方法。 (基于此stackoverflow question的代码)[HttpPost("download")] public async Task<IActionResult> DownloadFile(){ .…