Pyserial
Pyserial
index
modules |
next |
previous |
pySerial API
Classes
Native ports¶
class serial.Serial
__init__(port=None, baudrate=9600, bytesi
ze=EIGHTBITS, parity=PARITY_NONE, stopbits
=STOPBITS_ONE, timeout=None, xonxoff=Fals
e,rtscts=False, writeTimeout=None, dsrdtr=Fals
e, interCharTimeout=None)
port – Device name or port number number or None.
baudrate – Baud rate such as 9600 or 115200 etc.
bytesize – Number of data bits. Possible
values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
parity – Enable parity checking. Possible
values: PARITY_NONE, PARITY_EVEN, PARITY_ODD PARITY_MARK,
PARITY_SPACE
Paramete stopbits – Number of stop bits. Possible
rs: values: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_T
WO
timeout – Set a read timeout value.
xonxoff – Enable software flow control.
rtscts – Enable hardware (RTS/CTS) flow control.
dsrdtr – Enable hardware (DSR/DTR) flow control.
writeTimeout – Set a write timeout value.
interCharTimeout – Inter-character timeout, None to disable (default).
ValueError – Will be raised when parameter are out of range, e.g. baud
rate, data bits.
Raises: SerialException – In case the device can not be found or can not be
configured.
open()
Open port.
close()
Close port immediately.
__del__()
Destructor, close port when serial port instance is
freed.
read(size=1)
size – Number of bytes to read.
Parameters:
write(data)
data – Data to send.
Parameters:
inWaiting()
Return the number of chars in the receive buffer.
flush()
Flush of file like objects. In this case, wait until all
data is written.
flushInpu
t()
Flush input buffer, discarding all it’s contents.
flushO
utput()
Clear output buffer, aborting the current output
and discarding all that is in the buffer.
sen
dBr
eak(
durati
on=0.
25)
duration – Time (float) to activate the BREAK condition.
Parameters:
s
e
t
B
r
e
a
k
(l
e
v
el
=
Tr
u
e)
level – when true activate BREAK condition, else disable.
Parameters:
s
e
t
R
T
S
(
l
e
v
e
l
=
T
r
u
e
)
level – Set control line to logic level.
Parameters:
s
e
t
D
T
R
(
l
e
v
e
l
=
T
r
u
e
)
level – Set control line to logic level.
Parameters:
g
e
t
C
T
S
(
)
Returns: Current state (boolean)
Return the state of the CTS line.
Returns: Current state (boolean)
Return the state of the DSR line.
Returns: True
New in version 2.5.
Returns: False
New in version 2.5.
b – bytearray or array instance
Parameters:
Platform : Unix
Returns: File descriptor.
Return file descriptor number for the port that is
opened by this object. It is useful when serial
ports are used with select.
Platform : Windows
level – Set flow control state.
Parameters:
Set software flow control state.
Note
class serial.FileLike
An abstract file like class. It is used as base class
for Serial when no io module is available.
This class
implements readline() and readlines() bas
ed on read() and writelines() based
on write().
writelines(sequence)
Write a list of strings to the port.
flush()
Flush of file like objects. It’s a no-op in this class,
may be overridden.
read()
Raises NotImplementedError, needs to be
overridden by subclass.
write(data)
Raises NotImplementedError, needs to be
overridden by subclass.
seek(pos, whence=0)
Raises IOError: always, as method is not supported on serial port
New in version 2.5.
tell()
Raises IOError: always, as method is not supported on serial port
New in version 2.5.
truncate(self, n=N
one)
Raises IOError: always, as method is not supported on serial port
New in version 2.5.
isatty()
Raises IOError: always, as method is not supported on serial port
New in version 2.5.
To be able to
use the file like
object as
iterator for
e.g. for line
in Serial(
0): ... usag
e:
next()
Return the next line by calling readline().
__iter
__()
Returns self.
Other
high
level
acces
s
functi
ons.
rea
dli
ne(si
ze=N
one, e
ol='\n'
)
size – Max number of bytes to read, None -> no limit.
Parameters:
eol – The end of line character.
x
r
e
a
d
l
i
n
e
s
(
s
i
z
e
h
i
n
t
=
N
o
n
e
)
Read lines, implemented as generator.
Unlike readlines (that only returns on a timeout)
is this function yielding lines as they are received.
class rfc2217.PortManager
This class provides helper functions for
implementing RFC 2217 compatible servers.
Note
escape(data)
data – data to be sent over the network.
Parameters:
filter(data)
data – data read from the network, including Telnet and RFC
Parameters: 2217 controls.
check_modem_lines(force_
notification=False)
force_notification – Set to false. Parameter is for internal use.
Parameters:
See also
Exceptions
exception serial.SerialExcep
tion
Base class for serial port exceptions.
exception serial.SerialTim
eoutException
Exception that is raised on write timeouts.
Constants
Parity
serial.PARITY_NONE
serial.PARITY_EVE
N
serial.PARITY_
ODD
serial.PARIT
Y_MARK
serial.PA
RITY_SPA
CE
Stop bits
serial
.STOP
BITS_
ONE
ser
ial
.ST
OP
BI
TS
_O
NE
_P
OI
NT
_F
IV
E
s
e
r
i
a
l
.
S
T
O
P
B
I
T
S
_
T
W
O
N
o
t
e
t
h
a
t
1
.
5
s
t
o
p
b
i
t
s
a
r
e
n
o
t
s
u
p
p
o
r
t
e
d
o
n
P
O
S
I
X
.
I
t
w
i
l
l
f
a
l
l
b
a
c
k
t
o
2
s
t
o
p
b
i
t
s
.
B
y
t
e
s
i
z
e
s
e
r
i
a
l
.
F
I
V
E
B
I
T
S
s
e
r
i
a
l
.
S
I
X
B
I
T
S
s
e
r
i
a
l
.
S
E
V
E
N
B
I
T
S
A string indicating the pySerial version, such
as 2.5.
serial.protocol_handler_packages.append("my
_handlers")
s = serial.serial_for_url("foobar://")
For an URL starting with XY:// is the
function serial_for_url() attempts to
import PACKAGE.protocol_XY with each
candidate for PACKAGE from this list.
logging=[debug|info|warning|erro
r]: Prints diagnostic messages (not useful
for end users). It uses the logging module
and a logger calledpySerial.socket so
that the application can setup up logging
handlers etc. It will
call logging.basicConfig() which
initializes for output on sys.stderr(if no
logging was set up already).
logging=[debug|info|warning|erro
r]: Prints diagnostic messages (not useful
for end users). It uses the logging module
and a logger calledpySerial.loop so
that the application can setup up logging
handlers etc. It will
call logging.basicConfig() which
initializes for output on sys.stderr (if no
logging was set up already).
Returns: an iterable.
The function returns an iterable that yields tuples
of three strings:
Note