if SERIAL_PATH not in sys.path: sys.path.append(SERIAL_PATH) try: import serial except ImportError: fail( "I can't find pyserial on your system. That's odd, it should be included in this project" ) #----- PORTSCAN --------------------------------------------------------------- # reuse or scan for a new port trace("will reuse cache or scan for new port") name = portscan.getName(DEVICE_NAME) if name != None: if DEBUG: trace("Using port:" + name) PORT = name else: name = portscan.find() if name == None: fail("No port selected, giving in") PORT = name #----- CONNECT TO SERIAL ------------------------------------------------------ # get the serial port def get_serial():
USE_EMBEDDED_PYSERIAL = True BAUD = 115200 if USE_EMBEDDED_PYSERIAL: from os import sys, path thisdir = path.dirname(path.abspath(__file__)) sys.path.append(thisdir) import serial #----- PORTSCAN ---------------------------------------------------------------- import portscan name = portscan.getName() if name != None: if DEBUG: print("Using port:" + name) PORT = name else: name = portscan.find() if name == None: raise ValueError("No port selected, giving in") PORT = name print("Your micro:bit has been detected") print("Now running your program...") #----- CONFIGURE SERIAL PORT ---------------------------------------------------
import serial #----- PORTSCAN ---------------------------------------------------------------- # This will check for the portscan.cache file with a remembered serial port # identifier. If that exists, it will just use the serial port identified # in that file. If the file does not exist, it performs a workflow that # prompts the user. Note that if you plug your micro:bit into a different # USB port or use a different USB hub configuration, this may renumber # the port and require a re-scan. Just delete the portscan.cache file # and re-run your app code, if that happens. import portscan name = portscan.getName() if name != None: if DEBUG: print("Using port:" + name) PORT = name else: name = portscan.find() if name == None: raise ValueError("No port selected, giving in") PORT = name print("Your micro:bit has been detected") print("Now running your program...") #----- CONFIGURE SERIAL PORT ---------------------------------------------------