-
Notifications
You must be signed in to change notification settings - Fork 2
/
SerialReader.py
128 lines (99 loc) · 2.77 KB
/
SerialReader.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
# -*- python=3.8.x
"""
Created on 17/07/2023
Reads data from Raspberry Pi Pico
@author: Alex Kedziora
"""
import serial
import time
import csv
import sys
"""
class ArduinoUNO:
ser: serial.Serial
def __init__(self,COM:str):
self.ser = serial.Serial(COM, 9600, timeout=2) # MAX 115200
def Read(self):
return self.ser.readline() # Read the data (looks for terminating character '/r/n')
def Write(self, text: str= ""):
self.ser.write(text)
"""
class RPPico:
ser: serial.Serial()
_NO_IMAGES: int
def __init__(self, COM:str):
self.ser = serial.Serial(COM, 128000, xonxoff=True) # MAX 128000
def Read(self):
ser_bytes = self.ser.readline()
return str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8")) # Read the data (looks for terminating character '/r/n')
def Write(self, text: str= ""):
self.ser.write(text)
## COMMANDS
def Led_on(self):
self.ser.write(b"LED_ON\n")
def Led_off(self):
self.ser.write(b"LED_OFF\n")
def Number_of_images(self, no: int):
self._NO_IMAGES = no
text = "N_"+ str(no) + "\n"
self.ser.write(bytes(text, "utf-8"))
def Delay(self, d: int):
# delay in us
text = "D_"+ str(d) + "\n"
self.ser.write(bytes(text, "utf-8"))
def Start(self):
self.ser.write(b"S_TRUE\n")
def Info(self):
self.ser.write(b"INFO\n")
# There are 3 values to be printed
for i in range(3):
print(str(self.Read()).replace("\\r\\n",""))
def Get_data(self) -> list:
li: list = []
self.ser.write(b"GD\n")
i=0
time.sleep(0.001)
while(True):
text = self.Read()
if("END" in text):
return li
else:
li.append(text)
def Reset(self):
self.ser.write(b"RESTART\n")
def Test(self) -> str:
self.ser.write(b"TEST\n")
return self.Read()
def save_data(li : list, file_name: str):
with open(file_name+'.csv', 'w') as f:
# using csv.writer method from CSV package
write = csv.writer(f)
write.writerow([str(i).replace("b","").replace("\\r","").replace("\\n","").replace("'","") for i in li])
"""
def main():
data: list = []
rp = RPPico("COM7")
start = time.time()
con = True
i = 0
rp.Number_of_images(100)
d: str =""
while(con):
d = rp.Test()
print(d)
if(len(d) != 0):
con = False
print(d)
print(data)
"""
"""
while(con ):
text = ar.Read()
end = time.time()
if(len(text) != 0):
data.append(text)
if(end-start > 20):
con = False
save_data(data)
"""