Facebook
From ME, 9 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 277
  1. import serial
  2.  
  3. # Configure the serial port
  4. port = '/dev/ttyS2'  # Replace with the appropriate serial port
  5. baud_rate = 9600  # Replace with the desired baud rate
  6. ser = serial.Serial(port, baud_rate)
  7.  
  8. while True:
  9.     # Read data from the serial port
  10.     data_received = ser.readline().decode().strip()
  11.     print('Received data:', data_received)
  12.  
  13.     # Check if specific event occurred
  14.     if data_received == 'Hello, serial device!':
  15.         # Write data back to the serial port
  16.         data_to_send = 'Hello'
  17.         ser.write(data_to_send.encode())
  18.  
  19. # Close the serial port
  20. ser.close()