Bird
0
0
Raspberry Piprogramming~10 mins

pyserial library usage 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 import the pyserial library.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Aserialport
Bpyserial
Cserial
Dpyserialport
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to import 'pyserial' instead of 'serial'.
Using incorrect module names like 'serialport'.
2fill in blank
medium

Complete the code to open a serial connection on port '/dev/ttyUSB0' with baud rate 9600.

Raspberry Pi
ser = serial.Serial(port=[1], baudrate=9600)
Drag options to blanks, or click blank then click option'
A"COM3"
B"COM1"
C"/dev/ttyS0"
D"/dev/ttyUSB0"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Windows port names on Raspberry Pi.
Forgetting to put the port name in quotes.
3fill in blank
hard

Fix the error in the code to read one line from the serial port and decode it as UTF-8.

Raspberry Pi
line = ser.readline().[1]('utf-8').strip()
Drag options to blanks, or click blank then click option'
Awrite
Bdecode
Cread
Dencode
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode instead of decode.
Trying to read without decoding bytes.
4fill in blank
hard

Fill both blanks to write the string 'Hello' followed by a newline to the serial port.

Raspberry Pi
ser.write([1].[2]('utf-8'))
Drag options to blanks, or click blank then click option'
A"Hello"
Bencode
Cdecode
D"Hello\n"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to encode the string before writing.
Not adding newline if needed.
5fill in blank
hard

Fill all three blanks to create a serial connection on port '/dev/ttyAMA0' with baud rate 115200 and timeout 1 second.

Raspberry Pi
ser = serial.Serial(port=[1], baudrate=[2], timeout=[3])
Drag options to blanks, or click blank then click option'
A"/dev/ttyAMA0"
B115200
C1
D"/dev/ttyS1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong port names.
Setting baudrate as a string instead of a number.