Why do Raspberry Pi devices often use serial communication to connect to external devices?
Think about how many wires are needed and how simple the connection is.
Serial communication uses just a few wires to send data one bit at a time, making it simple and efficient for connecting small external devices like sensors or modules to a Raspberry Pi.
What will be the output of this Python code snippet running on a Raspberry Pi reading from a serial device?
import serial ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) data = ser.readline().decode('utf-8').strip() print(data)
Consider what readline() and decode() do in this context.
The code reads one line from the serial device, decodes it from bytes to a string, removes whitespace, and prints it. This outputs the first line sent by the device.
What error will this Raspberry Pi Python code raise when trying to open a serial connection?
import serial ser = serial.Serial('/dev/ttyS0', baudrate='9600')
Check the type of the baudrate parameter.
The baudrate parameter must be an integer. Passing it as a string causes a TypeError.
Which option correctly opens a serial port at 115200 baud on Raspberry Pi using Python?
Check parameter types and names carefully.
Option A uses correct parameter names and types: baudrate and timeout are integers, port is a string.
A Raspberry Pi reads 10 bytes from a serial device using data = ser.read(10). If the device sends only 6 bytes before closing, what is the length of data?
Think about how read() behaves when fewer bytes are available.
The read() method returns as many bytes as are available up to the requested number. If only 6 bytes are sent before closing, data will contain 6 bytes.
