import serial # Configure the serial port port = '/dev/ttyS2' # Replace with the appropriate serial port baud_rate = 9600 # Replace with the desired baud rate ser = serial.Serial(port, baud_rate) while True: # Read data from the serial port data_received = ser.readline().decode().strip() print('Received data:', data_received) # Check if specific event occurred if data_received == 'Hello, serial device!': # Write data back to the serial port data_to_send = 'Hello' ser.write(data_to_send.encode()) # Close the serial port ser.close()