Bird
0
0

You want to send sensor data from Arduino to Raspberry Pi over UART and log it. Which Python code snippet correctly reads lines continuously and prints them?

hard🚀 Application Q8 of 15
Raspberry Pi - Serial UART Communication
You want to send sensor data from Arduino to Raspberry Pi over UART and log it. Which Python code snippet correctly reads lines continuously and prints them?
Awhile True: line = ser.readline() if line: print(line.decode().strip())
Bfor i in range(10): print(ser.read(10))
Cline = ser.read() print(line)
Dser.write(b'Start') print('Logging started')
Step-by-Step Solution
Solution:
  1. Step 1: Identify continuous reading method

    Using while True with readline() reads lines continuously.
  2. Step 2: Decode and strip for clean output

    Decoding bytes and stripping newline gives readable sensor data.
  3. Final Answer:

    while True: line = ser.readline() if line: print(line.decode().strip()) -> Option A
  4. Quick Check:

    Continuous read with decode and strip = while True: line = ser.readline() if line: print(line.decode().strip()) [OK]
Quick Trick: Use readline() in a loop and decode output for UART data [OK]
Common Mistakes:
MISTAKES
  • Using read() without loop
  • Not decoding bytes
  • Writing instead of reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes