Bird
0
0
Raspberry Piprogramming~5 mins

pyserial library usage in Raspberry Pi - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aread()
Bopen()
Cwrite()
Dreadline()
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
What data type must you use when writing data with ser.write()?
Ainteger
Bstring
Cbytes
Dlist
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
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
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.