Challenge - 5 Problems
Serial Port Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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}")
Attempts:
2 left
💡 Hint
Look at the parameters passed to serial.Serial constructor.
✗ Incorrect
The serial port is set with baudrate 9600 and timeout 1 second, so printing these attributes shows those values.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about what None means in Python for timeout.
✗ Incorrect
Setting timeout=None means the read() call blocks indefinitely until data is received.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if the baudrate value is supported by the serial library.
✗ Incorrect
Baudrate -1 is invalid (must be > 0) and causes ValueError when initializing serial.Serial.
📝 Syntax
advanced1: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=?)
Attempts:
2 left
💡 Hint
Timeout expects a float number in seconds.
✗ Incorrect
Timeout must be a float representing seconds; 0.5 means half a second.
🚀 Application
expert2: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))
Attempts:
2 left
💡 Hint
Read returns as many bytes as available up to the requested number within the timeout.
✗ Incorrect
The read(10) waits up to 2 seconds. If only 5 bytes arrive in that time, it returns those 5 bytes.
