Complete the code to import the pyserial library.
import [1]
The pyserial library is imported using import serial. This allows you to use serial communication functions.
Complete the code to open a serial connection on port '/dev/ttyUSB0' with baud rate 9600.
ser = serial.Serial(port=[1], baudrate=9600)
On Raspberry Pi, USB serial devices usually appear as /dev/ttyUSB0. This is the correct port string to open.
Fix the error in the code to read one line from the serial port and decode it as UTF-8.
line = ser.readline().[1]('utf-8').strip()
The data read from serial is bytes. To convert it to a string, use decode('utf-8').
Fill both blanks to write the string 'Hello' followed by a newline to the serial port.
ser.write([1].[2]('utf-8'))
To send data, convert the string to bytes using encode('utf-8'). Adding \n sends a newline.
Fill all three blanks to create a serial connection on port '/dev/ttyAMA0' with baud rate 115200 and timeout 1 second.
ser = serial.Serial(port=[1], baudrate=[2], timeout=[3])
This code opens the serial port /dev/ttyAMA0 at 115200 baud with a 1 second timeout for reading.
