-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleitura_serial.py
More file actions
35 lines (27 loc) · 1.05 KB
/
leitura_serial.py
File metadata and controls
35 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# MANTEM SOMENTE OS DADOS RECEBIDOS, SIMILAR AO MQTT
import serial #pip install pyserial
import time
# Configuração da porta serial
porta = 'COM2' # Altere conforme necessário
baud_rate = 9600
try:
ser = serial.Serial(porta, baud_rate, timeout=1)
time.sleep(2)
print(f"[INFO] Lendo dados da porta {porta}...")
while True:
if ser.in_waiting:
linha = ser.readline().decode('utf-8', errors='ignore').strip()
if linha:
print(f"[RECEBIDO] {linha}")
# Sobrescreve o conteúdo anterior com a nova linha
with open("alarme.txt", "w") as arquivo:
arquivo.write(linha + "\n")
time.sleep(0.2) # Evita leitura excessiva da porta
except serial.SerialException as e:
print(f"[ERRO] Problema na porta serial: {e}")
except KeyboardInterrupt:
print("\n[INFO] Leitura interrompida pelo usuário.")
finally:
if 'ser' in locals() and ser.is_open:
ser.close()
print(f"[INFO] Porta {porta} fechada.")