How Texting Works

The main loop in each program repeatedly and rapidly checks to find out if any characters were typed into the keyboard or received by the radio.  If uart.any() is zero, meaning nothing was typed into the terminal, the script moves on to check for messages from the radio.  If that’s none too, the while True loop just repeats, rapidly, without doing anything else.  

while True:
    if uart.any():
        tx = input("Send: ")
        radio.send(tx)
    message = radio.receive()
    if message is not None:
        print("Receive: ", message)

If uart.any() is something other than zero, meaning you have typed characters into the terminal, tx = input("Send: ") makes the Send: prompt appear.  It then stores the characters you type into the tx variable as a string, up until you press the Enter key.  Then, radio.send(tx) transmits that string over the radio for another micro:bit to receive and forward to its terminal.

Remember that message = radio.receive() either stores a string or None in message.  If message has a string, then print("Receive: ", message) displays it.


Did You Know?

UART — it stands for universal asynchronous receiver transmitter. UART communication is one of several common communication protocols that devices use to exchange data.

Buffers — Incoming messages to the UART are stored in a special string called a buffer that is set aside for keeping characters until you need them. The radio object also has a buffer.

TX & RX — The abbreviation tx is short for transmit.  If you see tx in scripts, you might also see rx, which is shorthand for receive.  For example, the message variable could be named rx instead.

Dropped Packets — In its current form, the application drops packets sometimes.  In other words, not every message you type will be received and displayed by the other micro:bit.