blob: 82024f1a52be0f731bdf88c00b6fc41ca7b4bb88 [file] [log] [blame]
cliechtic1c37602009-07-21 01:34:57 +00001==============
2 pySerial API
3==============
4
5.. module:: serial
6
7Classes
8=======
9
10.. class:: Serial
11
12 .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=0, rtscts=0, interCharTimeout=None)
13
14 :param port:
cliechtic1c37602009-07-21 01:34:57 +000015 Device name or port number number or None.
16
cliechtic1c37602009-07-21 01:34:57 +000017 :param baudrate:
18 Baud rate such as 9600 or 115200 etc.
19
20 :param bytesize:
21 Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
22
23 :param parity:
24 Enable parity checking. Possible values: PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACE
25
26 :param stopbits:
27 Number of stop bits. Possible values: STOPBITS_ONE STOPBITS_ONE_POINT_FIVE STOPBITS_TWO
28
29 :param timeout:
30 Set a read timeout value.
31
32 :param xonxoff:
cliechtic1c37602009-07-21 01:34:57 +000033 Enable software flow control.
34
35 :param rtscts:
cliechtic1c37602009-07-21 01:34:57 +000036 Enable hardware (RTS/CTS) flow control.
37
38 :param interCharTimeout:
cliechtic1c37602009-07-21 01:34:57 +000039 Inter-character timeout, None to disable.
40
cliechti5134aab2009-07-21 19:47:59 +000041 The port is immediately opened on object creation, when a ``port`` is
42 given. It is not opened when port is None.
cliechtic1c37602009-07-21 01:34:57 +000043
cliechti5134aab2009-07-21 19:47:59 +000044 - Number: number of device, numbering starts at zero.
45 - Device name: depending on operating system. e.g. ``/dev/ttyUSB0``
46 on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000047
cliechti5134aab2009-07-21 19:47:59 +000048 Possible values for the parameter ``timeout``::
49
50 timeout = None # wait forever
51 timeout = 0 # non-blocking mode (return immediately on read)
52 timeout = x # set timeout to x seconds (float allowed)
cliechtic1c37602009-07-21 01:34:57 +000053
54
55 .. method:: open()
56
57 Open port.
58
59 .. method:: close()
60
61 Close port immediately.
62
63 .. method:: setBaudrate(baudrate)
64
65 Change baud rate on an open port.
66
67 .. method:: inWaiting()
68
69 Return the number of chars in the receive buffer.
70
71 .. method:: read(size=1)
72
73 Read size bytes from the serial port. If a timeout is set it may return
74 less characters as requested. With no timeout it will block until the
75 requested number of bytes is read.
76
77 .. method:: write(s)
78
cliechti86e87872009-07-21 13:32:45 +000079 Write the string `s` to the port.
cliechtic1c37602009-07-21 01:34:57 +000080
cliechti5134aab2009-07-21 19:47:59 +000081 .. method:: flush():
cliechtic1c37602009-07-21 01:34:57 +000082
83 Flush of file like objects. In this case, wait until all data is
84 written.
85
86 .. method:: flushInput()
87
88 Flush input buffer, discarding all it's contents.
89
90 .. method:: flushOutput()
91
92 Clear output buffer, aborting the current output and
93 discarding all that is in the buffer.
94
95 .. method:: sendBreak(duration=0.25)
96
97 Send break condition. Timed, returns to idle state after given
98 duration.
99
100 .. method:: setBreak(level=True)
101
102 Set break: Controls TXD. When active, no transmitting is possible.
103
104 .. method:: setRTS(level=True)
105
106 Set RTS line to specified logic level.
107
108 .. method:: setDTR(level=True)
109
110 Set DTR line to specified logic level.
111
112 .. method:: getCTS()
113
114 Return the state of the CTS line.
115
116 .. method:: getDSR()
117
118 Return the state of the DSR line.
119
120 .. method:: getRI()
121
122 Return the state of the RI line.
123
124 .. method:: getCD()
125
126 Return the state of the CD line
127
128 Read-only attributes:
129
130 .. attribute:: portstr
131
cliechti5134aab2009-07-21 19:47:59 +0000132 Device name. This is always the device name even if the
133 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000134
135 .. attribute:: BAUDRATES
136
137 A list of valid baud rates. The list may be incomplete such that higher
138 baud rates may be supported by the device and that values in between the
139 standard baud rates are supported. (Read Only).
140
141 .. attribute:: BYTESIZES
142
143 A list of valid byte sizes for the device (Read Only).
144
145 .. attribute:: PARITIES
146
147 A list of valid parities for the device (Read Only).
148
149 .. attribute:: STOPBITS
150
151 A list of valid stop bit widths for the device (Read Only).
152
153
154 New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:
155
156 .. attribute:: port
157
158 Port name/number as set by the user.
159
160 .. attribute:: baudrate
161
162 Current baud rate setting.
163
164 .. attribute:: bytesize
165
166 Byte size in bits.
167
168 .. attribute:: parity
169
170 Parity setting.
171
172 .. attribute:: stopbits
173
174 Stop bit with.
175
176 .. attribute:: timeout
177
cliechti5134aab2009-07-21 19:47:59 +0000178 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000179
180 .. attribute:: xonxoff
181
182 If Xon/Xoff flow control is enabled.
183
184 .. attribute:: rtscts
185
186 If hardware flow control is enabled.
187
188 Platform specific methods.
189
190 .. warning:: Programs using the following methods are not portable to other platforms!
191
192 .. method:: nonblocking()
193
194 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000195
cliechtic1c37602009-07-21 01:34:57 +0000196 Configure the device for nonblocking operations. This can be useful if
197 the port is used with ``select``.
198
199 .. method:: fileno()
200
201 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000202
cliechtic1c37602009-07-21 01:34:57 +0000203 Return file descriptor number.
204
205
206 .. method:: setXON(level=True)
207
208 :platform: Windows
cliechti86e87872009-07-21 13:32:45 +0000209
cliechtic1c37602009-07-21 01:34:57 +0000210 Set software flow control state.
211
212
213Exceptions
214==========
215
216.. exception:: SerialException
217
218 Base class for serial port exceptions.
219
220.. exception:: SerialTimeoutException
221
222 Exception that is raised on write timeouts.
223
224
225Constants
226=========
227
cliechti5134aab2009-07-21 19:47:59 +0000228Parity
cliechtic1c37602009-07-21 01:34:57 +0000229------
230.. data:: PARITY_NONE
231.. data:: PARITY_EVEN
232.. data:: PARITY_ODD
233.. data:: PARITY_MARK
234.. data:: PARITY_SPACE
235
cliechti5134aab2009-07-21 19:47:59 +0000236Stopbits
cliechtic1c37602009-07-21 01:34:57 +0000237--------
238.. data:: STOPBITS_ONE
239.. data:: STOPBITS_ONE_POINT_FIVE
240.. data:: STOPBITS_TWO
241
cliechti5134aab2009-07-21 19:47:59 +0000242Bytesize
cliechtic1c37602009-07-21 01:34:57 +0000243--------
244.. data:: FIVEBITS
245.. data:: SIXBITS
246.. data:: SEVENBITS
247.. data:: EIGHTBITS