import serial
# Configure the serial port
ser = serial.Serial(
port='/dev/serial0', # or '/dev/ttyAMA0'
baudrate=9600,
timeout=1
)
try:
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print("Received:", line)
except KeyboardInterrupt:
print("Exiting...")
finally:
ser.close()
✅ Wiring (if using GPIO)
RPi Pin
Function
Wire to
GPIO 14 (Pin 8)
TXD
RX of your device
GPIO 15 (Pin 10)
RXD
TX of your device
GND (Pin 6)
Ground
GND of your device
⚠️ Important: Make sure your device uses 3.3V logic. If it's 5V, use a level shifter.
✅ Troubleshooting Tips
If you get permission errors: sudo usermod -a -G dialout pi
If no data: check wiring, baud rate, and device TX/RX direction