Bird
0
0

What will be the output of this Python code on Raspberry Pi communicating with Arduino over UART?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Serial UART Communication
What will be the output of this Python code on Raspberry Pi communicating with Arduino over UART?
import serial
ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
ser.write(b'Hello Arduino')
response = ser.readline().decode().strip()
print(response)
APrints the response string sent by Arduino
BPrints 'Hello Arduino'
CPrints an empty line
DRaises a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code behavior

    The code sends 'Hello Arduino' bytes to Arduino and waits to read a line response with timeout.
  2. Step 2: Understand the output

    It prints the decoded and stripped response from Arduino, not the sent message or empty line unless no response.
  3. Final Answer:

    Prints the response string sent by Arduino -> Option A
  4. Quick Check:

    ser.readline() reads Arduino reply [OK]
Quick Trick: ser.readline() reads Arduino reply, not sent data [OK]
Common Mistakes:
MISTAKES
  • Confusing sent data with received data
  • Expecting print to show sent bytes
  • Ignoring decode and strip steps

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes