How can my microbit display a full string from an external Python script via serial communication if it only displays half the string?

6 hours ago 1
ARTICLE AD BOX

I created a microbit serial communication project for my college's Intro to Computer Science course. My external Python script can receive data from the microbit, but the microbit cannot read from the script. My computer is a Windows 11 Lenovo Yoga. laptop.

Below is the Python code from my external as provided in the assignment, with the lines beginning at the msgStr variable being my only addition. I already imported the PySerial library using the pip install command before building the project.

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: print(line) msgStr = "This is a message from Christian's laptop." if msgStr: ser_micro.write(msgStr.encode('utf-8')) ser_micro.close() main()

I also have the following microbit Python code for it:

def on_button_pressed_a(): serial.write_number(random_number) serial.write_line(" ") basic.show_number(random_number) input.on_button_pressed(Button.A, on_button_pressed_a) def on_button_pressed_b(): basic.show_string(message) input.on_button_pressed(Button.B, on_button_pressed_b) def on_data_received(): global message message = serial.read_line() serial.on_data_received(serial.delimiters(Delimiters.NEW_LINE), on_data_received) message = "" random_number = 0 random_number = randint(0, 10) message = "" serial.redirect(SerialPin.USB_TX, SerialPin.USB_RX, BaudRate.BAUD_RATE115200) serial.redirect_to_usb()

I use the Python 3.14 IDLE shell to run the program. When I press button A on the microbit, it displays the generated random number (5) on the LED screen and on the console in the external Python script, which tells me that the Python program successfully receives data from the microbit.

Code results:

looking for microbit scanning ports port: COM5 - USB Serial Device (COM5) pid: 516 vid: 3368 found target device pid: 516 vid: 3368 port: COM5 opening and monitoring microbit port 5 5

Below are some modified changes to both scripts:

Modified Microbit Python script:

def on_data_received(): global message message = serial.read_string() serial.on_data_received(serial.delimiters(Delimiters.NEW_LINE), on_data_received)

Modified external Python script:

while True: line = ser_micro.readline().decode('utf-8') if line: print(line) msgStr = "This is a message from Christian's laptop.\n" if msgStr: ser_micro.write(msgStr.encode('utf-8')) ser_micro.close() main()

I implemented .read_string() in the microbit Python editor and added the \n at the end of msgStr in the external Python script. When I pressed Button B after making the changes, half of the original message displayed: "This is a message fr"

Original message: "This is a message from Christian's laptop".

How should I get the rest of the message to display on the screen?

Read Entire Article