Serial Port

Serial Port Basics

Serial Port Principle

The English abbreviation for serial communication is UART (Universal Asynchronous Receiver Transmitter). Although it sounds like a complex concept, it is actually as shown below: two devices connected by a wire. The sender converts data into a binary sequence at one end of the wire, transmitting 0 and 1 signals in order using high and low levels. The receiver reads the high and low level signals on the wire at the other end and converts them back into a binary 0/1 sequence. Asynchronous transmission refers to full-duplex transmission, meaning data can be sent and received simultaneously, just like a phone call where we can speak and hear the other person at the same time.

Whenever we want to communicate between a PC and MCU, or between two MCUs, the simplest method is to use UART. Only two wires are needed to transmit data between two UARTs. Data flows from the Tx pin of the sending UART to the Rx pin of the receiving UART.

../../../../../_images/serial-two-wire.png

Serial communication principle diagram

Baud Rate

Baud rate refers to the number of bits transmitted per second by our serial communication. In simple terms, it is how many binary 1s and 0s can be sent in one second. For example, a baud rate of 9600 means 9600 binary digits composed of 0s and 1s can be sent in 1 second.

Transmitter TX and Receiver RX

UART communication basically uses 2 pins for data transmission. Tx - the pin for sending data, Rx - the pin for receiving data. For two serial ports to communicate, a minimum of three wires are needed.

../../../../../_images/uart_pin.png

RX represents the information receiver, TX represents the information transmitter

Attention

If connecting modules that do not have their own power supply, you must also connect VCC!

Serial Port Operations

The following demonstrates serial communication between the mPython Board and the blue:bit Bluetooth slave module, ultimately enabling Bluetooth communication on the mPython Board.

../../../../../_images/wiki_labplus_cn_14b2767026.png

blue:bit Bluetooth module

Create UART

from mpython import *                            # Import all mpython objects

uart=UART(1,baudrate=9600,tx=Pin.P15,rx=Pin.P16) # Create UART object, set baud rate to 9600, TX and RX pins are P15 and P16 respectively

The HC06 (blue:bit Bluetooth slave module) has a default factory baud rate of 9600. So when creating the UART here, we set the baud rate to 9600 for successful communication. Please set according to the baud rate of the serial port you need to connect.

UART(id, baudrate, bits, parity, stop, tx, rx, rts, cts, timeout), where id is the serial port number, which can be set to 1-2. The mPython Board supports 3 sets of serial ports. Port 0 is used for REPL. The baudrate parameter is the baud rate, tx parameter maps the transmit pin, rx parameter maps the receive pin. All pins can be used as serial input RX. Except for P2, P3, P4, and P10 which can only be used as input, all other pins can theoretically be used as output TX. Generally, you only need to set the above parameters; other parameters will keep their default values. For more UART parameters, please refer to the machine.UART chapter.

Serial Port Sending

You can use a Bluetooth-enabled computer or phone to download a Bluetooth debugging assistant and pair with the Bluetooth module. This enables communication between the mPython Board and the computer or phone.

After successful Bluetooth pairing, send byte data to the serial port:

>>> uart.write('hello,world!')

At this point, use the serial assistant to check if the data sent from the mPython Board was received. The uart.write(buf) function writes (sends) byte data to the serial port and returns the data length.

Serial Port Reading

The mPython Board receives serial port data and displays it on the OLED screen:

from mpython import *                               # Import all mpython objects

uart=UART(1,baudrate=9600,tx=Pin.P15,rx=Pin.P16,timeout=200)    # Instantiate UART, set baud rate 9600, TX and RX mapped to pins P15 and P16, timeout set to 200ms

while True:
    if(uart.any()):                     # When there is readable data in the serial port
        data = uart.readline()          # Read one line of data from the serial port
        print("received:",data)         # Print received data
        print("Received:%s" %data.decode('utf-8'))     # Note: bytes need to be decoded to string

At this point, you can send data to the serial port through the serial assistant. When the mPython Board receives serial data, it prints and displays it on the OLED screen. In the while loop, uart.any() is used to poll whether there is readable data in the serial port. When there is data, uart.readline() reads one line of data. Note that serial port data is received as bytes; if displaying on OLED, you need to use decode() to convert bytes to string.

In addition to UART.readline() for reading data, you can also use UART.read(length) to read a specified length of data from the serial port.

Extensions

Now that you have learned how to use the serial port, you can enable communication between the mPython Board and other MCUs (Arduino), computers/phones, and electronic modules. The applications are much broader - use your imagination to make good use of the serial port and create more interesting things!