Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a serial port on Raspberry Pi.
Raspberry Pi
import serial ser = serial.Serial([1], 9600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Windows COM port names like COM1.
Using USB serial device names when not connected via USB.
✗ Incorrect
On Raspberry Pi, the primary serial port is usually accessed via /dev/serial0.
2fill in blank
mediumComplete the code to send the string 'Hello' over the serial port.
Raspberry Pi
ser.write([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string without converting to bytes causes errors.
Forgetting the byte prefix
b.✗ Incorrect
The write method requires bytes, so prefix the string with b.
3fill in blank
hardFix the error in reading one byte from the serial port.
Raspberry Pi
data = ser.read([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 0 or negative numbers causes no data or errors.
Passing a larger number reads more bytes than needed.
✗ Incorrect
To read one byte, pass 1 as the argument to read.
4fill in blank
hardFill both blanks to create a dictionary mapping bytes to their ASCII codes for letters only.
Raspberry Pi
ascii_map = {chr([1]): [2] for [1] in range(65, 91)} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
ord(i) causes a TypeError because i is an integer, not a string or character.Using the wrong variable name in the comprehension.
✗ Incorrect
Use i as the loop variable and i as the value since range(65, 91) generates the ASCII codes for uppercase letters directly.
5fill in blank
hardFill all three blanks to filter and map serial data bytes greater than 100.
Raspberry Pi
filtered = [[1] for [2] in data if [2] [3] 100]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for output and loop variable causes confusion.
Using < instead of > changes the filter condition.
✗ Incorrect
Use byte as the output, b as the loop variable, and > to filter bytes greater than 100.
