Search code examples
javascriptserial-portusbbbc-microbitteraterm

Reading Serial Data Sent From Microbit in Windows


I am trying to send data from my microbit to my windows PC over serial communication. I followed the example here from the microbit website but can't seem to get my Tera Term window to display anything.

The microbit is plugged into one of the USB ports on the front of my PC and has the following code on it:

basic.forever(function () {
    serial.writeLine("test")
})

When I add a new connection to Tera Term I select COM1 (this is the only option I have)

New Connection in Tera Term

I then go into Setup > Serial Port and save the following settings

Serial Transmission Settings

Unfortunately, this doesn't display any data, I have tried different USB ports and cables to no avail.


Solution

  • 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')