-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino.py
59 lines (49 loc) · 1.69 KB
/
arduino.py
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
import serial
from variables import reading
class Arduino:
def __init__(self, port, baudrate, timeout=1):
"""
Initialize the Arduino object
:param port: the port to connect to
:param baudrate: the baudrate to use
:param timeout: the timeout for the connection
"""
# PORT of the arduino COM12
self.port = port
# set the baudrate
self.baudrate = baudrate
# set the timeout
self.timeout = timeout
# create a serial connection
self.ser = serial.Serial(port, baudrate=baudrate, timeout=timeout)
def write(self, data):
# write data to the serial connection
self.ser.write(data.encode())
def read(self):
# read data from the serial connection
return self.ser.readline().decode().lower()
def stop_reading(self):
# stop reading from the serial connection
reading[0] = False
def start_reading(self):
# start reading from the serial connection
reading[0] = True
def wait_for(self, data=[]):
# wait for data to be read from the serial connection
while True and data != []:
# check if reading flag is false
if not reading[0]:
# stop reading
break
# read the data
read = self.read()
# check if data is in the read data
for d in data:
# check if data is in the read data
if d.lower() in read:
# return the read data
return read
return "stopped reading"
def close(self):
# close the serial connection
self.ser.close()