40pin IO Development
Experiment 05 - UART Output
Hardware Connection
Loopback test: connect TXD and RXD on the hardware, then run the test program to write and read. The expected result is that the read data equals the written data.
Hardware Connection: connect TXD and RXD directly with a jumper:

Software Operation
(1) Select bus number and chip select from the printed serial devices (among which /dev/ttyS0 is the system debug port, not recommended for testing unless you fully understand its function). For RDK X5, select /dev/ttyS1 and input the baud rate.
(2) Enter the user home directory and run:
cd usersudo python3 ./test_serial.py

#!/usr/bin/env python3
import sys
import signal
import os
import time
# Import python serial library
import serial
import serial.tools.list_ports
def signal_handler(signal, frame):
sys.exit(0)
def serialTest():
print("List of enabled UART:")
os.system('ls /dev/tty[a-zA-Z]*')
uart_dev= input("请输出需要测试的串口设备名:")
baudrate = input("请输入波特率(9600,19200,38400,57600,115200,921600):")
try:
ser = serial.Serial(uart_dev, int(baudrate), timeout=1) # 1s timeout
except Exception as e:
print("open serial failed!\n")
print(ser)
print("Starting demo now! Press CTRL+C to exit")
while True:
test_data = "AA55"
write_num = ser.write(test_data.encode('UTF-8'))
print("Send: ", test_data)
received_data = ser.read(write_num).decode('UTF-8')
print("Recv: ", received_data)
time.sleep(1)
ser.close()
return 0
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
if serialTest() != 0:
print("Serial test failed!")
else:
print("Serial test success!")