tera term mbed launcher

Handy python script to launch tera term connected to the serial port of the first mbed it finds.

import _winreg as winreg
import itertools

DEVICE_NAME = "\\Device\\thcdcacm0"
TERA_TERM = "C:\\Program Files (x86)\\teraterm\\ttermpro.exe"

# Copied from http://stackoverflow.com/questions/1205383/listing-serial-com-ports-on-windows
def enumerate_serial_ports():
    """
	Uses the Win32 registry to return a iterator of serial
    (COM) ports existing on this computer.
    """
    path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'
    try:
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)
    except WindowsError:
        raise IterationError

    for i in itertools.count():
        try:
            val = winreg.EnumValue(key, i)
            yield (str(val[1]), str(val[0]))
        except EnvironmentError:
            break


def mbed_comm_port():		
	for comm in enumerate_serial_ports():
		if comm[1] == DEVICE_NAME:
			return comm[0]


if __name__ == "__main__":
	comm = mbed_comm_port()
	if comm:
		port = int(comm[3:])
		import subprocess
		subprocess.call([TERA_TERM, '/C=%d' % port, '/BAUD=9600'])


1 comment on tera term mbed launcher:

11 May 2015

Nice script! I just wanted to note that if you use "subprocess.call()" it will not exit the Python script until TeraTerm is closed, I've used "subprocess.Popen()" with the same arguments, so it doesn't wait for the child process to finish, exiting Python. This way when the script is launched it only opens TeraTerm and does not keep the shell opened in the start bar.

Please log in to post comments.