forked from tanchou/Verilog
Add connectESPV2.py for socket communication and hex input handling
This commit is contained in:
53
Semaine_7/ESP32/leds_commands/tests/pyhton/connectESPV2.py
Normal file
53
Semaine_7/ESP32/leds_commands/tests/pyhton/connectESPV2.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import select
|
||||||
|
|
||||||
|
# Adresse du serveur
|
||||||
|
SERVER_IP = "192.168.1.105"
|
||||||
|
SERVER_PORT = 1234
|
||||||
|
|
||||||
|
def recv_loop(sock):
|
||||||
|
"""Écoute les données reçues du socket et les affiche en hexadécimal"""
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = sock.recv(1024)
|
||||||
|
if not data:
|
||||||
|
print("Déconnecté du serveur")
|
||||||
|
break
|
||||||
|
print("Reçu :", ' '.join(f"{b:02X}" for b in data))
|
||||||
|
except Exception as e:
|
||||||
|
print("Erreur réception :", e)
|
||||||
|
break
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Connexion au serveur
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.connect((SERVER_IP, SERVER_PORT))
|
||||||
|
print(f"Connecté à {SERVER_IP}:{SERVER_PORT}")
|
||||||
|
|
||||||
|
# Lancer la boucle de réception dans un thread séparé
|
||||||
|
threading.Thread(target=recv_loop, args=(sock,), daemon=True).start()
|
||||||
|
|
||||||
|
print("Tape des chiffres hexadécimaux (0-9, A-F). Ctrl+C pour quitter.")
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
# Lire un seul caractère sans attente (mode ligne)
|
||||||
|
rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
|
||||||
|
if rlist:
|
||||||
|
char = sys.stdin.readline().strip().upper()
|
||||||
|
if len(char) == 0:
|
||||||
|
continue
|
||||||
|
for c in char:
|
||||||
|
if c in "0123456789ABCDEF":
|
||||||
|
value = int(c, 16)
|
||||||
|
sock.send(bytes([value]))
|
||||||
|
print(f"Envoyé : {value:02X}")
|
||||||
|
else:
|
||||||
|
print(f"Ignoré : {c} (non hexadécimal)")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nDéconnexion")
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Reference in New Issue
Block a user