Bird
0
0
Raspberry Piprogramming~20 mins

Baud rate and timeout configuration in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serial Port Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this serial port configuration code?
Consider this Python code snippet configuring a serial port on a Raspberry Pi. What will be printed?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=1)
print(f"Baudrate: {ser.baudrate}, Timeout: {ser.timeout}")
ABaudrate: 9600, Timeout: 1.0
BBaudrate: 115200, Timeout: 1
CBaudrate: 9600, Timeout: None
DBaudrate: 115200, Timeout: None
Attempts:
2 left
💡 Hint
Look at the parameters passed to serial.Serial constructor.
🧠 Conceptual
intermediate
1:30remaining
Which timeout setting causes the read() call to wait indefinitely?
In Raspberry Pi serial communication, which timeout value makes the read() function wait forever until data arrives?
Atimeout=0
Btimeout=None
Ctimeout=1
Dtimeout=-1
Attempts:
2 left
💡 Hint
Think about what None means in Python for timeout.
🔧 Debug
advanced
2:00remaining
Why does this serial port code raise a ValueError?
This code snippet raises a ValueError. What is the cause?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', baudrate=-1, timeout=1)
AMissing port open call before setting baudrate
BTimeout must be None or zero, not 1
CBaudrate -1 is invalid; baudrate must be positive
DDevice path '/dev/ttyUSB0' does not exist
Attempts:
2 left
💡 Hint
Check if the baudrate value is supported by the serial library.
📝 Syntax
advanced
1:30remaining
Which option correctly sets a 0.5 second timeout for serial read?
Choose the correct Python code to set a 0.5 second timeout on a serial.Serial object.
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=?)
Atimeout=500
Btimeout='0.5'
Ctimeout=None
Dtimeout=0.5
Attempts:
2 left
💡 Hint
Timeout expects a float number in seconds.
🚀 Application
expert
2:30remaining
How many bytes will be read with this configuration?
Given this code, how many bytes will the read() call return if 10 bytes are sent but only 5 bytes arrive within the timeout?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=2)
data = ser.read(10)
print(len(data))
A5
B10
C0
DRaises TimeoutError
Attempts:
2 left
💡 Hint
Read returns as many bytes as available up to the requested number within the timeout.