This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
EMS/1.Software/Verify/EMSProgram20240109/UART.py

32 lines
815 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#测试Python控制RK3568开发板的串口功能
#①发送和接收功能
#②需要完成接收的异步操作,且选择占用最小的方式
import time
import serial
import serial.tools.list_ports
class Uart:
def SerialSet(self, port, baudrate):
ser = serial.Serial(port, baudrate, 8, 'N', 1)
flag = ser.is_open
if flag:
print('success')
return ser
else:
print('Open Error')
def SerialSend(self, ser):
# 串口发送 ABCDEFG并输出发送的字节数。
write_len = ser.write("ABCDEFG\r\n".encode('utf-8'))
print("串口发出{}个字节。".format(write_len))
# ser.close()
uart = Uart()
ser = uart.SerialSet("/dev/ttyS4", 115200)
while True:
uart.SerialSend(ser)
time.sleep(1)