uart module lets you talk to a device connected to your board using
a serial interface.
init(baudrate=9600, bits=8, parity=None, stop=1, *, tx=None, rx=None)¶
Initialize serial communication with the specified parameters on the specified
rxpins. Note that for correct communication, the parameters have to be the same on both communicating devices.
Initializing the UART on external pins will cause the Python console on USB to become unaccessible, as it uses the same hardware. To bring the console back you must reinitialize the UART without passing anything for
Noneto these arguments). This means that calling
uart.init(115200)is enough to restore the Python console.
baudratedefines the speed of communication. Common baud rates include:
bitsdefines the size of bytes being transmitted, and the board only supports 8. The
parityparameter defines how parity is checked, and it can be
stopparameter tells the number of stop bits, and has to be 1 for this board.
rxare not specified then the internal USB-UART TX/RX pins are used which connect to the USB serial convertor on the micro:bit, thus connecting the UART to your PC. You can specify any other pins you want by passing the desired pin objects to the
When connecting the device, make sure you “cross” the wires – the TX pin on your board needs to be connected with the RX pin on the device, and the RX pin – with the TX pin on the device. Also make sure the ground pins of both devices are connected.
Trueif any characters waiting, else
Read characters. If
nbytesis specified then read at most that many bytes.
The timeout for all UART reads depends on the baudrate and is otherwise not changeable via Python. The timeout, in milliseconds, is given by:
microbit_uart_timeout_char = 13000 / baudrate + 1
The internal UART RX buffer is 64 bytes, so make sure data is read before the buffer is full or some of the data might be lost.
0x03will KeyboardInterrupt your program. You can enable or disable this using
Read as much data as possible.
Return value: a bytes object or
Read bytes into the
nbytesis specified then read at most that many bytes. Otherwise, read at most
Return value: number of bytes read and stored into
Read a line, ending in a newline character.
Return value: the line read or
Noneon timeout. The newline character is included in the returned bytes.
Write the buffer of bytes to the bus.
Return value: number of bytes written or