This repository was archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_keypad.py
More file actions
110 lines (94 loc) · 3.38 KB
/
serial_keypad.py
File metadata and controls
110 lines (94 loc) · 3.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Group: 24
# Names: Divy, Elio, Kelvin, Matthew
import serial
import sys
import glob
import time
import requests
from constants import *
class PicoConnector:
"""
Runs on a laptop connected to the second Pico via USB.
Reads the serial data from the Pico and sends it to the server
if it is a valid move.
"""
def __init__(self, serialPort=""):
self.pico = None
self.findPayload(
serialPort
) # We need to establish a serial connection with the Pico
if self.pico is None:
print("[ERROR] Divy Couldn't find payload")
raise Exception("Payload not found")
def findPayload(self, serialPort):
ports = []
if serialPort == "": # If no port is forced, get all the ports
ports = self.getSerialPorts()
else:
ports.append(serialPort)
print("[ALERT] Divy Serial Ports:", ports)
for portName in ports:
print("[ALERT] Divy Trying port", portName)
pico = serial.Serial(
port=portName, baudrate=9600, timeout=1.5, write_timeout=1.5
)
time.sleep(2) # Giving time to Pico to wake up
try:
pico.write(
bytes("hello world", "utf-8")
) # Sending 'hello world' and expecting to get not throw an exception back
except (
serial.SerialTimeoutException
): # If we get an exception, the port is not open
continue
data = pico.readline()
data = data.decode("utf-8")
data = data.rstrip()
if data == "*":
print("[ALERT] Divy Found Payload on port", portName)
self.pico: serial.Serial = pico
return
def getSerialPorts(self):
"""Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith("win"):
ports = ["COM%s" % (i + 1) for i in range(256)]
elif sys.platform.startswith("linux") or sys.platform.startswith("cygwin"):
# this excludes your current terminal "/dev/tty"
ports = glob.glob("/dev/tty[A-Za-z]*")
elif sys.platform.startswith("darwin"):
ports = glob.glob("/dev/tty.*")
else:
raise EnvironmentError("Unsupported platform")
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def listenSuccessMessage(self):
while True:
data = self.pico.readline()
data = data.decode("utf-8")
data = data.strip("\n")
data = data.strip("\r")
# '#' is -1
if data == "#":
data = -1
# if data is sent
if data != "" and data != "*":
print(f"[REQUEST] Divy Sent: {data}")
requests.post(url=MOVE_URL, json={"move": int(data)})
if __name__ == "__main__":
try:
pico = PicoConnector()
pico.listenSuccessMessage()
except Exception as ex:
print(ex)