-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_con.py
More file actions
38 lines (33 loc) · 1.02 KB
/
Copy pathfirst_con.py
File metadata and controls
38 lines (33 loc) · 1.02 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
36
37
38
import serial
import socket
import threading
print("[ARDUINO] Placa conectada en COM3")
ser = serial.Serial('COM3', 9600, timeout=1)
def handle_client(conn):
while True:
try:
data = conn.recv(1024).decode().strip()
if not data:
break
print(f"[CMD] Recibido: {data}")
ser.write((data + "\n").encode('utf-8'))
resp = ser.readline().decode().strip()
conn.sendall((resp or "OK").encode('utf-8'))
except Exception as e:
print("[ERROR]", e)
break
conn.close()
# Servidor socket local
HOST = '127.0.0.1'
PORT = 5678
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f"[SERVER] Escuchando en {HOST}:{PORT}")
while True:
conn, addr = s.accept()
print(f"[CLIENTE] Conectado: {addr}")
threading.Thread(target=handle_client, args=(conn,)).start()
if __name__ == "__main__":
main()