Bonjour à tous,
Je cherche à allumer une led en fonction des commandes reçus par bluetooth, le code fonctionne, cependant j'ai quelque erreur quand le buffer est déja plein.
Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
36
37
38
39
40
41
42
43 import io import serial import RPi.GPIO as GPIO PORT = "/dev/rfcomm0" BAUDRATE = 9600 TIMEOUT = 0.1 ENCODING = "Utf-8" LED_PIN = 22 try: GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(LED_PIN, GPIO.OUT) etat = 0 while True : ser = serial.Serial(PORT, BAUDRATE, timeout=TIMEOUT) buffer = io.BufferedReader(ser) with io.TextIOWrapper(buffer, encoding=ENCODING) as file: for line in file: # print(ser.in_waiting) # if ser.in_waiting > 0: if len(line)>0 : line = line.strip().lower() print(line) print("line :",line.encode("hex")) if line == "on": GPIO.output(LED_PIN, GPIO.HIGH) etat = 1 elif line == "off": GPIO.output(LED_PIN, GPIO.LOW) etat = 0 if etat == 1 : GPIO.output(LED_PIN, GPIO.HIGH) finally: GPIO.cleanup()
Je veux utiliser la fonction ser_waiting, mais le problème est qu'elle me renvoie toujours 0...
Si certains ont des suggestions, merci d'avance.
Partager