I am a beginner with the microbit and I have tried to copy some code from different tutorials/code examples. I am just trying to set up the serial communication between a computer and the microbit. My plan is to have the microbit just echo every line it receives and send it back for now. But the lines are split and I am not sure if it is the microbit or the computer doing the splitting. I have tried from two different computers with the same result.
Here is the microbit code:
from microbit import *
uart.init(baudrate=115200)
msg_str = ""
while True:
msg_bytes = uart.readline()
if (msg_bytes):
print(msg_bytes)
My computer code, a Python program with pyserial:
import serial
import serial.tools.list_ports as list_ports
def find_microbit_comport():
ports = list(list_ports.comports())
for p in ports:
if (p.pid == 516) and (p.vid == 3368):
return str(p.device)
if __name__ == '__main__':
ser = serial.Serial()
ser.baudrate = 115200
ser.timeout = 1
ser.port = find_microbit_comport()
ser.open()
ser.write(b'testing')
text = ser.readline()
while text != b'':
print(text.decode('utf-8'))
text = ser.readline()
ser.close()
The problem I have that is that it prints out this:
b'te'
b'sting'
I was expecting it to print b'testing' in one word. I could write code to combine the text, but I could then get out of sync.