简体   繁体   English

.Net Core 似乎以大约 1000 / 秒的速度读取 GPIO 引脚。 go 如何更快?

[英].Net Core seems to max out reading GPIO pin at about 1000 / second. How to go faster?

I wrote a C#.Net Core 3.1 app to read pin state changes on the GPIO of an Orange Pi Zero.我写了一个 C#.Net Core 3.1 应用程序来读取橙色 Pi 零的 GPIO 上的引脚 state 变化。 I am using the System.Devices.Gpio IoT library.我正在使用 System.Devices.Gpio IoT 库。 I am running Armbian Focal (based on Ubuntu 20.04).我正在运行 Armbian Focal(基于 Ubuntu 20.04)。

I am trying to read the frequency of a square wave signal, which makes a pin on the GPIO rise and fall.我正在尝试读取方波信号的频率,这会使 GPIO 上的引脚上升和下降。 So I'm trying to read how many rising (or falling) edges occur in 1 second.所以我试图读取 1 秒内出现了多少个上升(或下降)边沿。

I have tried both polling the pin in a loop, and registering to events/callbacks to pin changes, and I can't seem to read any faster than about 1000 times per second (1 Khz)我已经尝试在循环中轮询引脚,并注册到事件/回调以进行引脚更改,但我似乎无法以每秒大约 1000 次(1 Khz)的速度读取

Unfortunately, I need to read at least 5000 times per second (and preferably more).不幸的是,我需要每秒至少阅读 5000 次(最好是更多)。

I don't think the limitation is on the hardware side.我认为限制不在硬件方面。 I think it's the fact that.Net Core runs on top of a CLR which is an extra layer that is slowing things down.我认为这是事实,即 .Net Core 运行在 CLR 之上,这是一个额外的层,会减慢速度。

I am looking at writing this portion of the app in Python (I'll leave the rest of the app in C# /.Net Core), but I don't know if it will speed things up?我正在考虑在 Python 中编写应用程序的这一部分(我将把应用程序的 rest 留在 C# /.Net Core 中),但我不知道它会加快速度吗? I know Python is sort of the de facto programming language for SBC's nowadays, which is why I'm considering it (lots of community support and samples).我知道 Python 是当今 SBC 的事实上的编程语言,这就是我考虑它的原因(大量社区支持和示例)。 But it's an interpreted language.但它是一种解释性语言。 Would it not be slower than C# which is compiled?会不会比编译的 C# 慢? But then again, maybe Python talks directly to Linux, unlike C#.Net Core which goes through the CLR.但话又说回来,也许 Python 直接与 Linux 对话,这与通过 CLR 的 C#.Net Core 不同。 Also Python is baked into Ubuntu so maybe that tight integration has some benefits. Python 也被烘焙到 Ubuntu 中,因此紧密集成可能有一些好处。

I know C would be the quickest, but also the hardest of the languages to write in (other than Assembly).我知道 C 将是最快的,但也是最难编写的语言(除了汇编)。 I'm not too familiar with either C nor Python, but I thought Python would have the less steep learning curve.我对 C 和 Python 都不太熟悉,但我认为 Python 的学习曲线会不那么陡峭。

In summary, my question is: Is there anything I can perhaps do in.Net Core to speed up reading the GPIO?总之,我的问题是:我可以在.Net Core 中做些什么来加快读取 GPIO 的速度吗? Would perhaps using a third party library instead of System.Devices.Gpio help (I doubt it)?也许使用第三方库而不是 System.Devices.Gpio 有帮助(我对此表示怀疑)? Is the limitation perhaps on the hardware or maybe in Amrbian (Ubuntu) (I doubt it)?限制可能是在硬件上还是在 Amrbian(Ubuntu)中(我怀疑)? Should I even bother trying it in Python or would it be a waste of time and I should go straight to C?我是否应该费心在 Python 中尝试它,或者这会浪费时间吗?

Sorry, I know my question is probably not according to StackOverflow policies.抱歉,我知道我的问题可能不符合 StackOverflow 政策。 I was just wondering if anyone maybe has had any experience in reading GPIO pin state changes really fast (as in to measure frequency in the KHz range) - on an Orange or Raspberry Pi or something like that...我只是想知道是否有人在读取 GPIO 引脚 state 方面有任何经验(例如在 KHz 范围内测量频率)——在橙色或树莓派或类似的东西上......

I'd appreciate any pointers.我会很感激任何指示。

Thank you.谢谢你。

Although re-writing the code in a faster language (perhaps Python, definitely C) would have helped, at the end we decided to put a hardware assist in place (as recommended by some on other posts I've read).尽管用更快的语言(可能是 Python,肯定是 C)重新编写代码会有所帮助,但最后我们决定放置一个硬件辅助(正如我读过的其他帖子中的一些建议)。 We are going for Raspberry Pi RF Frequency Counter solution.我们将使用Raspberry Pi 射频频率计数器解决方案。

Thanks to @r_ahlskog and PMF for your inputs however.不过,感谢@r_ahlskog和 PMF 的输入。 It's very much appreciated.非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM