# -*- coding: utf-8 -*
import serial
import time
# 打开串口
ser = serial.Serial("/dev/ttyAMA0", 115200)
def main():
while True:
data = ser.read_all() # 读数据
if data == b"OK": # 读数据类型 bytes
ser.write("ok".encode()) # 写数据加.encode()
# 清空接收缓冲区
ser.flushInput()
# 必要的软件延时
time.sleep(0.1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
if ser != None:
ser.close()
树莓派python串口收发数据
最新推荐文章于 2024-06-27 23:12:18 发布