Seite 1 von 1

Anfängerfrage: DOS-Ausgabe im Python Programm ausgeben

Verfasst: Montag 25. Juni 2018, 09:12
von sunshineh
Hallo,

ich habe die rtl_433 Software installiert und kann im DOS-Fenster mit folgenden Befehl
rtl_433 -G
die Signale meines PIR abrufen:
{"time" : "2018-06-25 09:41:22", "model" : "Generic Remote", "id" : 53535, "cmd" : 222, "tristate" : "1F0F0F111F1!"}
{"time" : "2018-06-25 09:41:22", "model" : "Generic Remote", "id" : 53535, "cmd" : 222, "tristate" : "1F0F0F111F1!"}
{"time" : "2018-06-25 09:41:22", "model" : "Generic Remote", "id" : 53535, "cmd" : 222, "tristate" : "1F0F0F111F1!"}

Nun möchte ich diese Signale in meinem Python Programm abrufen

Code: Alles auswählen

import sys
import os

while 1:
    process = os.popen('rtl_433 -G')
    str = process.read()
    print(str)
Doch so bleibt das Programm stecken in dem Moment, wenn ein Signal kommt. Wie mache ich das richtig?

Re: Anfängerfrage: DOS-Ausgabe im Python Programm ausgeben

Verfasst: Montag 25. Juni 2018, 09:26
von Sirius3
`read` liest den gesamten Output, und da das Programm nie endet, dauert das Lesen auch ziemlich lange.

os.popen ist veraltet, statt dessen nimmt man subprocess.Popen:

Code: Alles auswählen

import subprocess
process = subprocess.Popen(['rtl_433', '-G'], stdout=subprocess.PIPE)
for line in process.stdout:
    print(line)