The pyserial library allows Python programs to communicate with serial ports, such as those used by devices connected to a Raspberry Pi via USB or GPIO pins.
Click to reveal answer
beginner
How do you open a serial port using pyserial?
You create a <code>Serial</code> object with the port name and baud rate, for example: <br><code>import serial<br>ser = serial.Serial('/dev/ttyUSB0', 9600)</code>
Click to reveal answer
beginner
What method do you use to read data from a serial port in pyserial?
You can use ser.read(size) to read a specific number of bytes or ser.readline() to read until a newline character.
Click to reveal answer
beginner
How do you write data to a serial port using pyserial?
Use the ser.write(data) method, where data is a bytes object. For example: ser.write(b'Hello') sends the string 'Hello'.
Click to reveal answer
beginner
Why is it important to close the serial port after use?
Closing the serial port with ser.close() frees the resource so other programs or processes can use it and prevents errors or conflicts.
Click to reveal answer
Which pyserial method reads a line of text from the serial port?
Aread()
Bopen()
Cwrite()
Dreadline()
✗ Incorrect
The readline() method reads bytes until it encounters a newline character.
How do you specify the baud rate when opening a serial port with pyserial?
AAs a parameter in Serial(), e.g., Serial('/dev/ttyUSB0', 9600)
BBy calling ser.set_baudrate(9600)
CBy setting an environment variable
DBaud rate is fixed and cannot be changed
✗ Incorrect
The baud rate is set as the second argument when creating the Serial object.
What data type must you use when writing data with ser.write()?
Ainteger
Bstring
Cbytes
Dlist
✗ Incorrect
The write() method requires a bytes object, so strings must be encoded first.
What happens if you try to open a serial port that is already in use?
AThe port resets automatically
BAn exception is raised
CThe program silently ignores it
DThe port opens normally
✗ Incorrect
Trying to open a busy port raises a serial.SerialException.
Which of these is a common serial port name on Raspberry Pi for USB serial devices?
A/dev/ttyUSB0
B/dev/serial0
C/dev/ttyS0
D/dev/null
✗ Incorrect
/dev/ttyUSB0 is typically the first USB serial device connected.
Explain how to set up and use the pyserial library to read data from a serial device on a Raspberry Pi.
Think about opening the port, reading, and closing it.
You got /4 concepts.
Describe the steps to send a message to a device connected via serial port using pyserial.
Remember that write() needs bytes, not strings.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the pyserial library when used with Raspberry Pi?
easy
A. To communicate with devices over serial ports
B. To create graphical user interfaces
C. To manage Wi-Fi connections
D. To control GPIO pins directly
Solution
Step 1: Understand pyserial's role
pyserial is designed to handle serial communication, which means sending and receiving data over serial ports.
Step 2: Compare options with pyserial's function
Options B, C, and D relate to GUI, Wi-Fi, and GPIO control, which are not pyserial's purpose.
Final Answer:
To communicate with devices over serial ports -> Option A
Quick Check:
pyserial = serial communication [OK]
Hint: pyserial = serial port communication library [OK]
Common Mistakes:
Confusing pyserial with GPIO control libraries
Thinking pyserial manages network connections
Assuming pyserial creates user interfaces
2. Which of the following is the correct way to open a serial port using pyserial in Python?
easy
A. serial.Open(port='/dev/ttyUSB0', speed=9600)
B. serial.connect('/dev/ttyUSB0', 9600)
C. serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1)
D. serial.start(port='/dev/ttyUSB0', baud=9600)
Solution
Step 1: Recall pyserial syntax for opening ports
The correct method is to create a Serial object with parameters: port, baudrate, and optionally timeout.
Step 2: Check each option's method and parameters
Only serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1) uses serial.Serial() with correct parameter names and values.
Final Answer:
serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1) -> Option C
Quick Check:
Use serial.Serial() with port and baudrate [OK]
Hint: Use serial.Serial() with port and baudrate [OK]
Common Mistakes:
Using incorrect method names like Open or connect
Using wrong parameter names like speed or baud
Omitting required parameters
3. What will be the output of this code snippet if the serial device sends the bytes b'OK'?
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
response = ser.read(2)
print(response)
ser.close()
medium
A. b'OK'
B. 'OK'
C. OK
D. b"\x00\x00"
Solution
Step 1: Understand ser.read(2) behavior
The read(2) method reads 2 bytes from the serial port and returns them as a bytes object.
Step 2: Interpret the print output
Printing a bytes object shows it with a leading b and quotes, so b'OK' will be printed.
4. Identify the error in this pyserial code snippet:
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write('Hello')
ser.close()
medium
A. The close method should be called before write
B. The Serial constructor is missing the timeout parameter
C. The port name '/dev/ttyUSB0' is invalid
D. The write method requires bytes, not a string
Solution
Step 1: Check the write method argument type
pyserial's write() expects bytes, but 'Hello' is a string, causing a TypeError.
Step 2: Verify other parts of the code
Timeout is optional, port name can be valid, and close() should be after write().
Final Answer:
The write method requires bytes, not a string -> Option D
Quick Check:
write() needs bytes, encode strings first [OK]
Hint: Always encode strings to bytes before write() [OK]
Common Mistakes:
Passing string directly to write() without encoding
Thinking timeout is mandatory
Closing port before writing data
5. You want to read lines from a serial device that sends text ending with newline characters. Which pyserial method and parameters should you use to read a full line safely with a 2-second timeout?
hard
A. Use serial.Serial('/dev/ttyUSB0', 9600, timeout=None) and ser.readline()
B. Use serial.Serial('/dev/ttyUSB0', 9600, timeout=2) and ser.readline()
C. Use serial.Serial('/dev/ttyUSB0', 9600) and ser.read(100)
D. Use serial.Serial('/dev/ttyUSB0', 9600, timeout=0) and ser.readline()
Solution
Step 1: Set timeout for safe reading
Setting timeout=2 seconds ensures readline() waits up to 2 seconds for a line, avoiding blocking forever.
Step 2: Use readline() to read full lines
readline() reads until newline character, perfect for line-based text from serial devices.
Final Answer:
Use serial.Serial('/dev/ttyUSB0', 9600, timeout=2) and ser.readline() -> Option B
Quick Check:
timeout + readline() = safe line read [OK]
Hint: Use timeout with readline() to avoid blocking [OK]
Common Mistakes:
Using read() instead of readline() for lines
Setting timeout=None causes blocking
Setting timeout=0 causes non-blocking read with no wait
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.