# RasPi <---> Arduino serial test -2024.2.16- import serial import time #ser = serial.Serial('/dev/ttyUSB0', 115200) ser = serial.Serial('/dev/ttyUSB0', 9600) time.sleep(3) print('START') command = 'aaa.' ser.write(command.encode('UTF-8')) i = 0 def serRead(): get_data = ser.readline() if get_data != "": data = get_data.strip().decode('UTF-8') print(data) return True return False try: while True: if serRead(): i += 1 if i == 1: command = 'bbb.' ser.write(command.encode('UTF-8')) elif i == 2: command = 'ccc.' ser.write(command.encode('UTF-8')) time.sleep(2) except KeyboardInterrupt: ser.close() print('END')