-
Notifications
You must be signed in to change notification settings - Fork 0
/
picoconnector_01.py
245 lines (196 loc) · 7.22 KB
/
picoconnector_01.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
''' picoconnect_xx.py allows getting info about all connected Pico
and connecting to one special one.
See examples of use below
Developed by [email protected]
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
from serial.tools import list_ports
import time
def scan_for_picos(verbose = True):
'''returns list of USB portnames with Raspi Picos connected '''
if verbose:
print("Scanning for connected Picos")
picos = []
for port in list_ports.comports():
if verbose:
print("Checking ", port.device)
if port.manufacturer != None:
if "MicroPython" in port.manufacturer:
picos.append(port.device)
if verbose:
print("Pico found on port:")
for p in picos:
print(p)
print()
return picos
#-----------------------------------------------------------------
def get_info_pico(pico, timeout=0.5):
'''
Get info stored on the pico in a file info.txt
This must contain a keyword in the first line:
The keyword is returned
'''
s=serial.Serial(pico, baudrate=115200)
if s.isOpen()==False:
s.open()
# send commands
s.write(b'\x03\x03') # Interrupt eventually running program
s.write(b'\x02') # Normal REPL
s.write(b'f = open("info.txt", "r")\r')
s.write(b't = f.read()\r')
s.write(b'f.close()\r')
s.write(b'print(t)\r')
# Receive answer (with timeout)
text = ''
t1 = time.time()
while True:
nbbytes = s.inWaiting()
if nbbytes >0:
c = s.read(nbbytes)
c = c.decode("utf-8")
text += c
if time.time() - t1 > timeout:
break
# Analize answer
if 'Traceback' in text: # Something went wrong
info = ''
else: # Filter interesting part
keyword = 'print(t)'
pos1 = text.find(keyword)
info = text[pos1 + len(keyword) +1:] # keyword found behind print()
info = info.split()[0] # take first line only
info = info.strip()
return info
#----------------------------------------------------------------------
def scan_picoinfo():
'''Print info on all picos connected'''
print("Info on Picos found:")
for pico_portname in scan_for_picos(verbose = False):
info = get_info_pico(pico_portname)
print (pico_portname, ' INFO: ', info)
print()
#----------------------------------------------------------------------
def find_pico(keyword, timeout = 1):
'''returns name of the port where a Pico with the right keyword is connected'''
picoportname = ''
for picoportname in scan_for_picos(verbose = False):
info = get_info_pico(picoportname, timeout)
if info == keyword:
break
return picoportname
#-----------------------------------------------------------------------
class Pico():
def __init__(self, keyword):
self.keyword = keyword
self.port = None
self.portname = ""
self.connected = False
def connect(self, verbose = True):
'''Connect to Pico with the right keyword
(keyword in first line of file INFO.TXT on Pico'''
self.portname = find_pico(self.keyword, 0.1)
if self.portname:
if verbose:
print('Found ', self.keyword, ' on port', self.portname)
self.port = serial.Serial(self.portname, baudrate = 115200)
if self.port.open == False:
self.port.open()
else:
if verbose:
print(self.keyword + " not found")
self.port = None
if self.port:
self.connected = True
if verbose:
print ( "Connected to ", self.keyword)
print()
def reset(self):
'''Reset Pico to restart in local mode'''
print("Pico soft reset")
self.port.write(b'\04')
self.get_answer(timeout = 0.5)
def set_rawREPL(self):
# Ctrl - A
self.port.write(b'\x01')
def set_normalREPL(self):
# Ctrl - B
self.port.write(b'\x02')
def close(self):
'''Close connection to Pico'''
if self.port:
self.port.close()
def interrupt_prog(self):
if self.port:
print("Interrupting running program\n")
self.port.write(b'\x03\x03')
def send_cmd(self, cmd, verbose = True, timeout = 1):
'''send command to Pico
cmd is a Python code line'''
if verbose:
print("CMD: ", cmd)
self.port.write(cmd.encode('utf-8') + b'\r')
self.get_answer()
def get_answer(self, timeout=0.2, verbose = True):
t1 = time.time()
text = ''
while True:
nbbytes = self.port.inWaiting()
if nbbytes >0:
c = self.port.read(nbbytes)
c = c.decode("utf-8")
text += c
if time.time() - t1 > timeout:
break
if verbose:
print('RESPONSE: ', text)
return text
def list_files(self):
'''Returns a list of the files on Pico '''
self.send_cmd ("import os", timeout=2, verbose = False)
time.sleep(1)
self.send_cmd ("os.listdir()", timeout=2, verbose = False)
t = self.get_answer( timeout = 2, verbose = False)
begin = t.find("[")
t = t[t.find("[")+1:t.find("]")]
t = t.replace(" ", "")
t = t.replace("'", "")
files = t.split(',')
return files
#--------------------------------------------------------------------------
''' TEST program: '''
if __name__ == "__main__":
''' You can have info on all Picos connected'''
picos = scan_for_picos()
#scan_picoinfo()
'''You can connect to one special Pico
This must contain the keyword in the first line of a file info.txt
on the Pico'''
keyword = 'PWMgenerator_02'
mypico = Pico(keyword)
mypico.connect()
''' The connected attribute tells if the connection was successful'''
if mypico.connected:
''' You can interrupt a program (e.g. started by main.py at boot)
or reset (reboot) the Pico'''
mypico.reset()
mypico.interrupt_prog()
time.sleep(0.1)
'''Commands can be sent and executed:'''
#mypico.send_cmd("import time", verbose = False)
#mypico.send_cmd("import time", verbose = True)
mypico.set_rawREPL()
mypico.send_cmd("3.14*5")
print()
print("Back to normal REPL")
mypico.set_normalREPL()
mypico.send_cmd("3.14*5")
print()
'''You can list the files on the Pico (except those in subfolders)'''
print("Files on Pico:")
files = mypico.list_files()
for f in files:
print(f)
'''Close the connection'''
mypico.close()