Wiznet makers

ruilixin6

Published August 03, 2026 ©

70 UCC

0 VAR

0 Contests

0 Followers

0 Following

Data Transceiver Practice: hardware Configuration, MicroPython Programming

This experiment realizes UART send & receive on Fengya No.1 board, with bytes decoding, LED prompt and verification using XCOM.

COMPONENTS
PROJECT DESCRIPTION

【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.

Tweet

In the following experiment, we need to use a MiniUSB cable to connect the Fengya No. 1 Board - Universal Compatible Expansion Board 's UART0 to the USB port on it with the USB port on the computer:
In the following code, we have designed a program for implementing serial port (UART) data transmission and reception.
The sample code is as follows, located in the folder of the supporting materials: elegance-devkit v1\Demo\07 UART_ReadWrite:
# Python env   : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time    : 2024/6/23 下午9:16
# @Author  : 李清水
# @File    : main.py
# @Description : UART类实验,实现简单的串口数据收发

# ======================================== 导入相关模块 =========================================

# 硬件相关的模块
from machine import UART, Pin
# 时间相关的模块
import time

# ======================================== 全局变量 ============================================

# 串口接收计数变量
recv_count: int = 0

# ======================================== 功能函数 ============================================

def LED_Blink(led_obj: Pin, delay_time: int) -> None:
    """
    控制LED灯闪烁。

    进行LED灯的子系统闪烁控制,指示程序停止运行。

    Args:
        led_obj (Pin): LED对象,用于控制LED灯的GPIO控制器
        delay_time (int): 延时时间,单位为毫秒

    Returns:
        None
    """
    while True:
        # 点亮LED
        led_obj.on()
        # 延时 delay_time ms
        time.sleep_ms(delay_time)
        # 熄灭LED灯
        led_obj.off()
        # 再次延时 delay_time ms
        time.sleep_ms(delay_time)

# ======================================== 自定义类 ============================================

# ======================================== 初始化配置 ==========================================

# 上电延时3s
time.sleep(3)
# 打印调试信息
print("FreakStudio : UART Read and Write Experiment")

# 创建串口对象,设置波特率为115200
uart: UART = UART(0, 115200)
# 初始化uart对象,波特率为115200,数据位为8,无校验位,停止位为1
# 设置串口超时时间为100ms
uart.init(baudrate  = 115200,
          bits      = 8,
          parity    = None,
          stop      = 1,
          tx        = 0,
          rx        = 1,
          timeout   = 100)

# 设置GPIO 25为LED输出引脚,下拉电阻使能
LED: Pin = Pin(25, Pin.OUT, Pin.PULL_DOWN)

# ========================================  主程序  ===========================================

# 循环执行
while True:

    # 打印调试信息,输出串口收发次数
    print("UART0 RECV COUNT: ", recv_count)
    # 延时100ms
    time.sleep_ms(100)

    # 判断串口接收计数变量是否大于等于10
    if recv_count >= 10:
        # 串口接收计数变量归零
        recv_count = 0
        break

    # 向串口发送数据
    uart.write("Hello, UART!")
    # 发送调试信息
    print("UART0 SEND DATA")

    # 检查是否有数据可读
    if uart.any():
        # 打印可读取的字符数
        print("number of characters that can be read:", uart.any())
        # 点亮LED灯,做指示作用
        LED.on()
        # 读取一行数据
        data = uart.readline()
        # 接收到数据为 b'Hello, UART!\r\n',字符串是bytes类型
        print("UART0 RECV DATA: ", data)
        # 对接收到数据data进行解码
        print("DECODE UART0 RECV DATA: ", data.decode('utf-8'))
        # 延时300ms
        time.sleep_ms(300)
        # 关闭LED灯
        LED.off()
        # 延时300ms
        time.sleep_ms(300)
        # 串口接收计数变量递增
        recv_count = recv_count + 1

# 收发10次后,跳出循环,LED灯闪烁
print("Stop Program Running")
LED_Blink(LED, 100)
In the above code, we have performed the following operations:
Initialization configuration: Configure the UART0 serial port, set the baud rate to 115200, configure GPIO0 as TX (transmit) and GPIO1 as RX (receive), set the timeout period to 100ms, and configure the GPIO25 pin as the LED control pin
Use while loop to implement serial data transmission and reception, send the string Hello, UART! each time and try to read data, decode and print the received data, meanwhile control the LED to turn on and off to indicate the receiving operation
When the number of data receptions reaches 10, exit the loop and call the LED_Blink function to make the LED blink continuously
Here, the data received via the serial port is of the bytes type (an immutable binary byte sequence). As an immutable data type, bytes is used to represent byte sequences, storing data in units of bytes with a value range of 0 to 255. The bytes type is similar to the string type (str), except that its character encoding is bytes (the raw representation of binary data) rather than Unicode characters. The bytes type is extremely useful in scenarios involving binary data processing, such as network communication, file reading and writing, and encryption and decryption.In Python, adding the prefix b before a string indicates the bytes type.
In the above program, we use the following code to decode the received immutable binary byte sequence and convert it into a string type:
# 对接收到数据data进行解码
print("DECODE UART0 RECV DATA: ", data.decode('utf-8'))
Open the XCOM software and select the COM port corresponding to the CH340K chip:
The serial communication parameters are consistent with those in the program:
Keep the rest of the sending and receiving parameters as default:
Click to open the serial port:
Burn the program, open the terminal, and the following content will be displayed:
As can be seen, when we do not perform any operation, XCOM software can normally receive the data sent by the Raspberry Pi Pico.
We try to send a string from the computer via the XCOM software Hello, Pico! to the Raspberry Pi Pico:
It can be seen that the terminal outputs the number of characters received via the serial port of the Raspberry Pi Pico, as well as the data before and after decoding:
Meanwhile, each time a string is sent from the computer via XCOM software, the Raspberry Pi Pico 's onboard LED will blink once. After ten consecutive transmissions, the Raspberry Pi Pico stops receiving serial data, and its onboard LED keeps blinking continuously:
Additionally, when the Raspberry Pi Pico transmits and receives data via the serial port, Fengya No. 1 Board - Universal Compatible Expansion Board the serial port transmit-receive indicator on it LED3 will light up intermittently:
Documents
Comments Write