The following python 3 code scans through the serial ports until it locates the one connected to your microbit, using the microbit's VID and PID. The script will then display port information. After this, the script displays anything sent through the serial port from the microbit.
You can use the port name to set up Tera Term, or let the script continue to display the data coming through the serial port. The default baud rate for the microbit is 115200. In the example output below, the port name is COM5. Each time you unplug and replug the microbit, the port name can change.
example output:
starting
looking for microbit
scanning ports
port: COM5 - mbed Serial Port (COM5)
pid: 516 vid: 3368
found target device pid: 516 vid: 3368 port: COM5
opening and monitoring microbit port
code:
import serial
import serial.tools.list_ports as list_ports
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():
print('looking for microbit')
ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200)
if not ser_micro:
print('microbit not found')
return
print('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
print(line)
ser_micro.close()
if __name__ == '__main__':
print('starting')
main()
print('exiting')