Bird
0
0
Raspberry Piprogramming~10 mins

Serial protocol design in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"/dev/ttyS0"
B"/dev/ttyUSB0"
C"COM1"
D"/dev/serial0"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Windows COM port names like COM1.
Using USB serial device names when not connected via USB.
2fill in blank
medium

Complete 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'
Ab'Hello'
B"Hello"
C'Hello'
DHello
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string without converting to bytes causes errors.
Forgetting the byte prefix b.
3fill in blank
hard

Fix 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'
A-1
B1
C10
D0
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.
4fill in blank
hard

Fill 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'
Ai
Bord(i)
Cchr(i)
Di+32
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.
5fill in blank
hard

Fill 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'
Abyte
Bb
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for output and loop variable causes confusion.
Using < instead of > changes the filter condition.