Skip to content

Instantly share code, notes, and snippets.

@ntoll
Created March 7, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntoll/217769ed3478458c6c227449d9a81217 to your computer and use it in GitHub Desktop.
Save ntoll/217769ed3478458c6c227449d9a81217 to your computer and use it in GitHub Desktop.
"""
Listen to a connected micro:bit for incoming messages to which you can react
as needs apply.
"""
from serial.tools.list_ports import comports as list_serial_ports
from serial import Serial
def find_microbit():
"""
Finds the port to which the device is connected.
"""
ports = list_serial_ports()
for port in ports:
# Use the vendor and product ID to identify the micro:bit.
if "VID:PID=0D28:0204" in port[2].upper():
return port[0]
return None
def get_serial():
"""
Detect if a micro:bit is connected and return a serial object to talk to
it.
"""
port = find_microbit()
if port is None:
raise IOError('Could not find micro:bit.')
return Serial(port, 115200, timeout=1, parity='N')
serial = get_serial() # create the serial connection to the micro:bit
# Keep listening for bytes from the device. If any are received print them.
while True:
msg = serial.read_all() # Remember, msg will be bytes not a string.
if msg:
# At this point you could check the content of msg to react in more
# complicated ways than just printing it.
print(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment