Recall & Review
beginner
What is the purpose of the
pyserial library?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?✗ 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?✗ 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()?✗ 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?
✗ 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?
✗ 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.
