Bird
0
0
Raspberry Piprogramming~20 mins

Serial protocol design in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serial Protocol Master
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 Raspberry Pi serial read code?
Consider this Python code snippet running on a Raspberry Pi reading from a serial port. What will be printed?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
data = ser.read(5)
print(data)
Ab''
Bb'hello'
Chello
DNone
Attempts:
2 left
💡 Hint
Think about what happens if no data is available on the serial port within the timeout.
🧠 Conceptual
intermediate
1:30remaining
Which serial protocol feature ensures data integrity?
In serial communication protocols, which feature helps detect errors in transmitted data?
AStop bit
BBaud rate
CParity bit
DStart bit
Attempts:
2 left
💡 Hint
This feature adds an extra bit to check if data was corrupted.
🔧 Debug
advanced
2:00remaining
Why does this Raspberry Pi serial write code raise an exception?
This code snippet attempts to write a string to the serial port but raises an exception. What is the cause?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write('Hello World')
ANo exception, code runs fine
BTypeError because write expects bytes, not str
CValueError because baud rate is invalid
DSerialException because port is closed
Attempts:
2 left
💡 Hint
Check the data type expected by the write method.
📝 Syntax
advanced
2:00remaining
Which option correctly initializes a serial port with 8 data bits, no parity, and 1 stop bit?
Choose the correct Python code to configure a serial port on Raspberry Pi with 8N1 settings.
Aserial.Serial('/dev/ttyUSB0', 9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
Bserial.Serial('/dev/ttyUSB0', 9600, bytesize=8, parity='N', stopbits=1)
Cserial.Serial('/dev/ttyUSB0', 9600, data_bits=8, parity_bit='none', stop_bit=1)
Dserial.Serial('/dev/ttyUSB0', 9600, bits=8, parity='none', stops=1)
Attempts:
2 left
💡 Hint
Use constants from the serial module for configuration.
🚀 Application
expert
3:00remaining
How many bytes will be in the buffer after this Raspberry Pi serial read sequence?
Given this code reading from a serial port, how many bytes remain unread in the input buffer after execution?
Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=0)
ser.write(b'1234567890')
data1 = ser.read(4)
data2 = ser.read(3)
remaining = ser.in_waiting
print(remaining)
A4
B7
C0
D3
Attempts:
2 left
💡 Hint
Count total bytes written, bytes read, then subtract.