tbird20d / grabserial

Grabserial - python-based serial dump and timing program - good for embedded Linux development
GNU General Public License v2.0
198 stars 79 forks source link

High CPU usage on Pi Zero 2 W #67

Closed huzeifa closed 2 years ago

huzeifa commented 2 years ago

Hello, I am using grabserial on Raspberry Pi Zero to capture logging data and I am noticing high CPU usage.

Please see the below screenshot.

image

When I use the following code to understand the high CPU usage, I observe 25% CPU usage.

import serial
with serial.Serial('/dev/tty.usb-1.2, 2000000, timeout=10) as ser:
    while True:
            data = ser.read().decode('utf-8')
            print(data, end='')

However, when I test the following code with time.sleep(0.01) at the end of while True loop, I observe significant improvement in CPU usage.

import serial
import time
with serial.Serial('/dev/ttyUSB0', 2000000, timeout=10) as ser:
    while True:
        if (ser.in_waiting > 0):
            data = ser.read(ser.in_waiting).decode('utf-8')
            print(data, end='')
        time.sleep(0.01)

Please see the image below. The name of the above script is pyserial.py image

Let me know your thoughts regarding this as I would like to use grabserial timing functionality on resource-constrained devices such as Raspberry Pi Zero 2 W.

Thanks

github-actions[bot] commented 2 years ago

Stale issue message