简体   繁体   English

python中的串行通信随着时间的推移导致更高的CPU使用率

[英]Serial communication in python causing higher CPU usage over time

I have a script that utilizes OpenCV to track an object and communicate the location to an arduino. 我有一个脚本,该脚本利用OpenCV跟踪对象并将位置传达给arduino。 Essentially all it's doing is passing an integer to the arduino and the arduino interprets the integer as left/middle/right and turns on the appropriate LED. 本质上,它所做的就是将一个整数传递给arduino,而arduino将该整数解释为左/中/右,并打开适当的LED。 It works fine for ~30 seconds after which CPU usage jumps to 95%+ and the process begins to lag like crazy. 它可以正常工作约30秒,之后CPU使用率将跃升至95%以上,并且该过程开始变得疯狂。 If I remove the ser.write command and print left/middle/right to terminal then it runs fine. 如果我删除ser.write命令并向终端打印左/中/右,那么它运行良好。 What might be getting backed up causing the high CPU usage? 可能会备份什么导致CPU使用率高? I've tried different baud rates and there is a 0.01 second delay after each ser.write command. 我尝试了不同的波特率,每个ser.write命令后都有0.01秒的延迟。

It was a buffer issue on the Arduino side. 在Arduino方面,这是一个缓冲区问题。 There was a line that kept printing a blank character out for every character it read in, causing the buffer to overflow. 有一行代码会为每个读入的字符不断打印出空白字符,从而导致缓冲区溢出。 I removed that line and it's working fine now. 我删除了那条线,现在工作正常。

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

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