-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxbee_chat.py
More file actions
54 lines (47 loc) · 1.59 KB
/
xbee_chat.py
File metadata and controls
54 lines (47 loc) · 1.59 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import argparse, sys, threading, time
import serial
PORT = "COM6"
BAUD = 9600
def receiver(ser, stop_flag):
buf = bytearray()
while not stop_flag["stop"]:
chunk = ser.read(1024) # non-blocking due to timeout
if chunk:
buf.extend(chunk)
while b"\n" in buf:
line, _, buf = buf.partition(b"\n")
msg = line.decode("utf-8", errors="replace").rstrip("\r")
# Print incoming on its own line, restore prompt
sys.stdout.write(f"\n<REMOTE> {msg}\n> ")
sys.stdout.flush()
else:
time.sleep(0.02)
def main():
p = argparse.ArgumentParser(description="XBee transparent-mode chat")
p.add_argument("--name", default="Me", help="Name prefix for your messages")
args = p.parse_args()
ser = serial.Serial(PORT, BAUD, timeout=0.05)
stop_flag = {"stop": False}
t = threading.Thread(target=receiver, args=(ser, stop_flag), daemon=True)
t.start()
try:
print("Connected. Type messages and press Enter to send. Ctrl+C to quit.")
sys.stdout.write("> "); sys.stdout.flush()
while True:
try:
text = input()
except EOFError:
break
out = f"{args.name}: {text}\n"
ser.write(out.encode("utf-8"))
ser.flush()
sys.stdout.write("> "); sys.stdout.flush()
except KeyboardInterrupt:
pass
finally:
stop_flag["stop"] = True
t.join(timeout=0.5)
ser.close()
if __name__ == "__main__":
main()