CCCPaste

serialHoren

import serial
import time

def send_and_receive(port='/dev/ttyACM0', baudrate=9600, timeout=1):
    try:
        # Open the serial port
        ser = serial.Serial(port, baudrate, timeout=timeout)
        
        # Wait for the Arduino to initialize (optional)
        time.sleep(2)

        # Define commands to send
        commands = ["command1", "command2", "command3"]

        for command in commands:
            # Send the command
            ser.write(command.encode())
            print(f"Sent command: {command}")

            # Wait for a response
            response = ser.readline().decode().strip()
            print(f"Received response: {response}")

        # Close the serial port
        ser.close()
        print("Communication finished.")
    except Exception as e:
        print("Error:", str(e))

if __name__ == "__main__":
    send_and_receive()