API Reference
Complete PySerial API reference. All methods, properties, constants, and exceptions for the serial.Serial class and port discovery utilities.
Reference for serial.Serial, port discovery, constants, and exceptions.
serial.Serial Constructor
serial.Serial(
port=None,
baudrate=9600,
bytesize=EIGHTBITS,
parity=PARITY_NONE,
stopbits=STOPBITS_ONE,
timeout=None,
xonxoff=False,
rtscts=False,
write_timeout=None,
dsrdtr=False,
inter_byte_timeout=None,
exclusive=None,
)| Parameter | Type | Default | Description |
|---|---|---|---|
port | str or None | None | Device name (/dev/ttyUSB0, COM3). If None, port must be opened later with open(). |
baudrate | int | 9600 | Bits per second |
bytesize | int | EIGHTBITS (8) | Data bits: 5, 6, 7, or 8 |
parity | str | PARITY_NONE ('N') | 'N', 'E', 'O', 'M', or 'S' |
stopbits | float | STOPBITS_ONE (1) | 1, 1.5, or 2 |
timeout | float or None | None | Read timeout (seconds). None = block, 0 = non-blocking |
xonxoff | bool | False | Software flow control |
rtscts | bool | False | Hardware RTS/CTS flow control |
write_timeout | float or None | None | Write timeout (seconds) |
dsrdtr | bool | False | DSR/DTR flow control |
inter_byte_timeout | float or None | None | Inter-byte read timeout |
exclusive | bool or None | None | Exclusive access (Linux only) |
import serial
# Minimal
ser = serial.Serial('/dev/ttyUSB0', 9600)
# Full configuration
ser = serial.Serial('/dev/ttyUSB0', 115200,
bytesize=8, parity='N', stopbits=1,
timeout=1, write_timeout=1, rtscts=True)
# Create without opening
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600
ser.open()Log serial data automatically
TofuPilot records test results from your PySerial scripts, tracks pass/fail rates, and generates compliance reports. Free to start.
Properties
Port Information
| Property | Type | Read/Write | Description |
|---|---|---|---|
port | str | R/W | Port name or device path |
name | str | R | Device name (same as port) |
is_open | bool | R | True if port is currently open |
Communication Settings
| Property | Type | Read/Write | Description |
|---|---|---|---|
baudrate | int | R/W | Baud rate |
bytesize | int | R/W | Data bits (5-8) |
parity | str | R/W | Parity setting |
stopbits | float | R/W | Stop bits |
timeout | float or None | R/W | Read timeout (seconds) |
write_timeout | float or None | R/W | Write timeout (seconds) |
inter_byte_timeout | float or None | R/W | Inter-byte timeout |
Flow Control
| Property | Type | Read/Write | Description |
|---|---|---|---|
xonxoff | bool | R/W | Software flow control enabled |
rtscts | bool | R/W | Hardware RTS/CTS flow control enabled |
dsrdtr | bool | R/W | DSR/DTR flow control enabled |
Buffer Status
| Property | Type | Read/Write | Description |
|---|---|---|---|
in_waiting | int | R | Bytes in the input buffer |
out_waiting | int | R | Bytes in the output buffer (platform-dependent) |
Modem Control Lines
| Property | Type | Read/Write | Description |
|---|---|---|---|
dtr | bool | R/W | Data Terminal Ready |
rts | bool | R/W | Request To Send |
cts | bool | R | Clear To Send |
dsr | bool | R | Data Set Ready |
ri | bool | R | Ring Indicator |
cd | bool | R | Carrier Detect |
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
print(f"Port: {ser.port}")
print(f"Open: {ser.is_open}")
print(f"Baud: {ser.baudrate}")
print(f"Input buffer: {ser.in_waiting} bytes")
print(f"CTS: {ser.cts}, DSR: {ser.dsr}")
# Change settings on the fly
ser.baudrate = 115200
ser.timeout = 2.0
ser.close()Read Methods
read(size=1)
Read up to size bytes. Returns fewer bytes if timeout expires before size bytes arrive.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
size | int | 1 | Maximum bytes to read |
Returns: bytes (length 0 to size)
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Read exactly 1 byte
byte = ser.read()
# Read up to 10 bytes
data = ser.read(10)
if len(data) < 10:
print(f"Timeout: got {len(data)} of 10 bytes")
ser.close()readline(size=-1)
Read bytes until \n is found or size bytes have been read. Requires a timeout to avoid blocking forever if no newline arrives.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
size | int | -1 | Max bytes to read. -1 for no limit. |
Returns: bytes (includes the \n terminator if found)
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
line = ser.readline()
text = line.decode('utf-8').strip()
print(text)
# With size limit
line = ser.readline(100)
ser.close()read_until(expected=b'\n', size=None)
Read bytes until expected sequence is found or size bytes have been read.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
expected | bytes | b'\n' | Terminator sequence to search for |
size | int or None | None | Max bytes to read. None for no limit. |
Returns: bytes (includes the expected terminator if found)
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Read until "OK" response
response = ser.read_until(b'OK')
# Read until custom packet delimiter
packet = ser.read_until(b'</packet>')
# With size limit
data = ser.read_until(b'END', size=1000)
ser.close()read_all()
Read all bytes currently in the input buffer. Equivalent to ser.read(ser.in_waiting).
Returns: bytes
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
time.sleep(0.5) # let data accumulate
if ser.in_waiting:
data = ser.read_all()
print(f"Got {len(data)} bytes")
ser.close()Write Methods
write(data)
Write bytes to the serial port.
Parameters:
| Name | Type | Description |
|---|---|---|
data | bytes or bytearray | Data to transmit |
Returns: int (number of bytes written)
Raises: SerialTimeoutException if write_timeout is set and expires
import serial
import struct
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
# Write raw bytes
ser.write(b'Hello\n')
# Write encoded string
ser.write("AT+GMR\r\n".encode('utf-8'))
# Write binary struct
data = struct.pack('>HHf', 1234, 5678, 25.6)
ser.write(data)
ser.close()writelines(lines)
Write an iterable of byte strings. Does not add separators between items.
Parameters:
| Name | Type | Description |
|---|---|---|
lines | iterable of bytes | Sequence of byte strings |
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
commands = [b'AT\r\n', b'ATE0\r\n', b'AT+GMR\r\n']
ser.writelines(commands)
ser.close()flush()
Block until all data in the output buffer has been transmitted.
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.write(b'COMMAND\r\n')
ser.flush() # wait until bytes are on the wire
response = ser.readline()
ser.close()Control Methods
open()
Open the serial port. Called automatically by the constructor when port is provided.
Raises: SerialException if the port cannot be opened.
import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600
ser.open()
print(f"Opened: {ser.is_open}")
ser.close()close()
Close the serial port and release the resource.
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
# ... do work ...
ser.close()reset_input_buffer()
Clear the receive buffer, discarding all unread data.
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
# Discard stale data before sending a command
ser.reset_input_buffer()
ser.write(b'AT\r\n')
response = ser.readline()
ser.close()reset_output_buffer()
Clear the transmit buffer, discarding all unsent data.
send_break(duration=0.25)
Send a break condition for duration seconds.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
duration | float | 0.25 | Break duration in seconds |
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.send_break(0.5)
ser.close()set_buffer_size(rx_size=4096, tx_size=None)
Set internal buffer sizes. Platform-dependent; may raise AttributeError if not supported.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
rx_size | int | 4096 | Receive buffer size in bytes |
tx_size | int or None | None | Transmit buffer size in bytes |
Port Discovery
serial.tools.list_ports.comports(include_links=False)
List all available serial ports on the system.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
include_links | bool | False | Include symlinks (Linux/macOS) |
Returns: List of ListPortInfo objects
import serial.tools.list_ports
for port in serial.tools.list_ports.comports():
print(f"{port.device}: {port.description}")
if port.vid:
print(f" USB VID:PID = {port.vid:04X}:{port.pid:04X}")ListPortInfo Properties
| Property | Type | Description |
|---|---|---|
device | str | Device path (/dev/ttyUSB0, COM3) |
name | str | Short device name |
description | str | Human-readable description |
hwid | str | Hardware ID string |
vid | int or None | USB Vendor ID |
pid | int or None | USB Product ID |
serial_number | str or None | USB serial number |
location | str or None | Physical USB location |
manufacturer | str or None | Manufacturer name |
product | str or None | Product name |
interface | str or None | Interface description |
URL Handlers
PySerial supports URL-style port specifications for special backends.
| Scheme | Description | Example |
|---|---|---|
socket:// | TCP/IP socket | socket://192.168.1.100:7777 |
rfc2217:// | Remote serial over network | rfc2217://server:4000 |
loop:// | Loopback (testing) | loop:// |
hwgrep:// | Hardware grep | hwgrep://0403:6001 |
import serial
# Loopback for testing (no hardware needed)
ser = serial.Serial('loop://', timeout=1)
ser.write(b'test')
print(ser.read(4)) # b'test'
ser.close()
# TCP socket to a serial-over-IP device
ser = serial.Serial('socket://192.168.1.100:7777', timeout=1)
# Find FTDI device by USB IDs
ser = serial.Serial('hwgrep://0403:6001')Constants
Byte Size
| Constant | Value |
|---|---|
serial.FIVEBITS | 5 |
serial.SIXBITS | 6 |
serial.SEVENBITS | 7 |
serial.EIGHTBITS | 8 |
Parity
| Constant | Value |
|---|---|
serial.PARITY_NONE | 'N' |
serial.PARITY_EVEN | 'E' |
serial.PARITY_ODD | 'O' |
serial.PARITY_MARK | 'M' |
serial.PARITY_SPACE | 'S' |
Stop Bits
| Constant | Value |
|---|---|
serial.STOPBITS_ONE | 1 |
serial.STOPBITS_ONE_POINT_FIVE | 1.5 |
serial.STOPBITS_TWO | 2 |
Exceptions
| Exception | Parent | Raised When |
|---|---|---|
serial.SerialException | Exception | Port doesn't exist, permission denied, device disconnected |
serial.SerialTimeoutException | SerialException | Read or write timeout exceeded |
serial.PortNotOpenError | SerialException | Operation on a closed port |
serial.WriteTimeoutError | SerialTimeoutException | Write timeout exceeded |
import serial
try:
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.write(b'test')
data = ser.read(10)
except serial.SerialTimeoutException:
print("Operation timed out")
except serial.SerialException as e:
print(f"Serial error: {e}")
except PermissionError:
print("Permission denied")
except FileNotFoundError:
print("Port not found")
finally:
if 'ser' in locals() and ser.is_open:
ser.close()Context Manager
serial.Serial supports with for automatic cleanup.
import serial
with serial.Serial('/dev/ttyUSB0', 9600, timeout=1) as ser:
ser.write(b'data\n')
response = ser.read(100)
# port is closed automaticallyVersion
import serial
print(serial.__version__) # e.g., '3.5'