Micro:bit uPython: Receiving data from serial port

In this tutorial we will check how read content from the serial port, using the micro:bit board. We will use the micro:bit pins as serial Tx and Rx pins, like we have already covered in this previous tutorial.

Introduction

In this tutorial we will check how read content from the serial port, using the micro:bit board. We will use the micro:bit pins as serial Tx and Rx pins, like we have already covered in this previous tutorial.

In order to test the communication, we are going to connect the micro:bit pins to a USB to serial converter, so we can use a computer and some serial software to send data to the board.

Naturally, this is a procedure we are going to do for testing since, if our aim was simply to exchange data with a computer, we could directly use the uPython prompt, which operates over USB.

Nonetheless, the true objective of analyzing how to establish serial connections using the micro:bit pins is opening the possibility of connecting the board to other devices that talk over serial.

The electric diagram of the connection between the micro:bit and the serial to USB converter can be checked here.

For this tutorial I’m using version 1.7.0 of uPython for the Micro:bit board.

The code

We will start our code by importing the uart object and the sleep function, both from the microbit module.

from microbit import uart, sleep

Then we will initialize the serial interface, using as Rx and Tx the micro:bit external pins 1 and 0, respectively. We will use a baud rate of 9600. Recall from the previous tutorial that, after we do this initialization, the uPython prompt will become unavailable.

uart.init(baudrate=9600, tx = pin0, rx = pin1)

Now, we can check if there are bytes available to read from the serial port. If there are none, we keep waiting until we can read some content.

To check if there are bytes available in the serial port, we can simply call the any method on our uart object. This method call takes no arguments and returns a Boolean value  indicating if there are bytes available. So, we will use the result of this method call as stopping condition of a loop that will execute until there are bytes to read.

Since we don’t need to do anything inside our loop, we will use the pass statement, which basically corresponds to a null operation that can be used when the Python syntax requires an expression [1], which is the case of the body of a loop.

while not uart.any():
  pass

After the loop finishes, we know that we should have content to read on the serial interface. So, after a small delay introduced with the sleep function, we will try to read all the bytes available with a call to the readall method on the uart object.

This method takes no arguments and will try to read and return as much data as possible [2]. Note however that this is a non-blocking function that should return after a timeout, which means that if we try to send some content first and then more content later, the last content may not be obtained as result of this function call.

We will store the content returned by the readall method call in a variable.

sleep(400)
content = uart.readall()

As mentioned before, the uPython prompt will become unavailable as soon as we set the serial interface to work with the external pins of the micro:bit. Thus, after reading the content, we will bring the prompt back by re-initializing the serial interface, passing only as argument of the init method a baud rate of 115200.

uart.init(baudrate=115200)

To finalize, we will print the content obtained from the serial port.

print(content)

The final code can be seen below.

from microbit import uart, sleep

uart.init(baudrate=9600, tx = pin0, rx = pin1)

while not uart.any():
  pass

sleep(400)
content = uart.readall()

uart.init(baudrate=115200)

print(content)

Testing the code

To test the code, simply upload the previous uPython script to your micro:bit, using a tool of your choice. I’ll be using uPyCraft, a uPython IDE.

Then, on your computer, open a serial tool of your choice. I’ll be using the Arduino IDE serial monitor. Then, establish a connection to the micro:bit using the COM port assigned to your Serial to USB converter.

If you don’t know beforehand what is the COM port assigned to your device, a simple trick when using the Arduino IDE is disconnecting the device, checking the list of available COM ports, re-connecting the device and checking the new one that is added.

After that, select a baud rate qual to 9600 (the one used when initializing the serial interface in the uPython code) and send some content. I will be sending a “TEST” string.

If you go back to the uPython tool you are using, the prompt should now be available and the content you sent from the serial tool should have been printed, like illustrated in figure 1.

Reading content from the serial port with uPython and the micro:bit board

Figure 1 – Receiving data from the serial port.

Related posts

References

[1] https://docs.python.org/2.0/ref/pass.html

[2] https://microbit-micropython.readthedocs.io/en/latest/uart.html#microbit.uart.uart.readall

Advertisements

3 Replies to “Micro:bit uPython: Receiving data from serial port”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s