To better understand why I need this: I have a Pico that controls a DDS generator that should work as standalone device or remotely controlled by computer. The problem with USB devices is that they can always change their device names.
1. step: Identify all the Picos under all devices:
Code: Select all
from serial.tools import list_ports
def scan_for_picos():
'''returns list of connected Raspi Picos'''
picos = []
for port in list_ports.comports():
print("Checking ", port.device)
if port.manufacturer != None:
if "MicroPython" in port.manufacturer:
picos.append(port.device)
return picos
On my Pico I have a file called 'info.py' containing just one function:
Code: Select all
def info():
print( "PICO_2") # PICO_2 = name of my Pico
Code: Select all
for pico in scan_for_picos():
info = get_info_pico(pico, 0.5)
print (pico, ' INFO: ', info)
Code: Select all
def get_info_pico(pico, timeout):
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'import info\r')
s.write(b'info.info()\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 = 'info.info()\r'
pos1 = text.find(keyword)
info = text[pos1 + len(keyword):]
pos2 = info.find('\r')
info = info[:pos2]
info = info.strip()
return info
Code: Select all
Checking /dev/ttyS5
...
Checking /dev/ttyACM1
/dev/ttyACM0 INFO: SINGEN
/dev/ttyACM1 INFO: PICO_2
Sorry for the length of this posting!