Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
Raspberry Pi - Serial UART Communication
What will be the output of this code snippet?
import serial
ser = serial.Serial('/dev/ttyS0', baudrate=9600, timeout=0.5)
data = ser.read(10)
print(len(data))

Assuming no data is sent to the port.
AError: timeout too short
B10
CNone
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand ser.read with timeout and no data

    ser.read(10) tries to read 10 bytes but waits only 0.5 seconds.
  2. Step 2: Since no data arrives, read returns empty bytes

    len(data) will be 0 because no bytes were received before timeout.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    Read length with no data = 0 [OK]
Quick Trick: Read returns empty bytes if no data before timeout [OK]
Common Mistakes:
MISTAKES
  • Expecting read to block indefinitely
  • Assuming read returns None on timeout
  • Confusing timeout with read length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes