Zombie BBC micro:bit serial ports created when using pyocd-gdbserver --persist
Last updated: Jan 24, 2023
So I was happily using pyocd-gdbserver to program and enter debugging mode on a BBC micro:bit attached to one of my laptop’s USB port, as described here. Then I stopped being able to read data through the USB port… Long story short, multiple ‘zombie’ ports were created and my Python script was connecting to a zombie instead of the live one.
setserial -g /dev/ttyACM*
output:
/dev/ttyACM0, UART: unknown, Port: 0x0000, IRQ: 0, Flags: low_latency /dev/ttyACM1, UART: unknown, Port: 0x0000, IRQ: 0, Flags: low_latency
Sometimes for fun, I would also see a ttyACM2. Why would two ports have the same Port number? The answer is they don’t. They are the same port. Connecting to /dev/ttyACM1 got me nothing. Connecting to /dev/ttyACM0 got me connected to the BBC micro:bit. I had set the pyocd-gdb utility running using:
sudo ~/.local/bin/pyocd-gdbserver -t nrf51 -bh -r --persist
I think that the –persist flag does the damage. Run the script without this and I think we are good to go. I altered my serial port script to flag up when more than one micro:bit is found. For good measure, I sort the ports into reverse order and connect to the first one with the PID and VID for the micro:bit, which will be the lowest numbered ttyACM port. This is a work around when zombies appear.
Please find my Python 3 serial_port.py script for finding and returning a serial port connection to a BBC micro:bit below.
import logging
import serial
import serial.tools.list_ports as list_ports
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
PID_MICROBIT = 516
VID_MICROBIT = 3368
TIMEOUT = 0.1
def find_comport(pid, vid, baud):
''' return a serial port '''
ser_port = serial.Serial(timeout=TIMEOUT)
ser_port.baudrate = baud
ports = list(list_ports.comports())
print('scanning ports')
for p in ports:
print('port: {}'.format(p))
try:
print('pid: {} vid: {}'.format(p.pid, p.vid))
except AttributeError:
continue
if (p.pid == pid) and (p.vid == vid):
print('found target device pid: {} vid: {} port: {}'.format(
p.pid, p.vid, p.device))
ser_port.port = str(p.device)
return ser_port
return None
def main():
logging.debug('looking for microbit')
ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200)
if not ser_micro:
logging.info('microbit not found')
return
logging.debug('opening and monitoring microbit port')
ser_micro.open()
while True:
line = ser_micro.readline().decode('utf-8')
if line: # If it isn't a blank line
logging.info(line)
ser_micro.close()
if __name__ == '__main__':
print('starting')
main()
print('exiting')