blob: ae0dfa55125f0857e09265605a51347fff01813a [file] [log] [blame]
Chris Liechti589c92a2015-09-04 23:04:34 +02001=======
2 Tools
3=======
4
5.. module:: serial
6
7serial.tools.list_ports
8=======================
9.. module:: serial.tools.list_ports
10
11This module can be executed to get a list of ports (``python -m
12serial.tools.list_ports``). It also contains the following functions.
13
14
15.. function:: comports()
16
Chris Liechti3ee3eff2015-11-13 02:01:01 +010017 :return: an iterable that yields :class:`ListPortInfo` objects.
Chris Liechti589c92a2015-09-04 23:04:34 +020018
19 The function returns an iterable that yields tuples of three strings:
20
21 - port name as it can be passed to :class:`serial.Serial` or
22 :func:`serial.serial_for_url`
23 - description in human readable form
24 - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.
25
26 Items are returned in no particular order. It may make sense to sort the
27 items. Also note that the reported strings are different across platforms
28 and operating systems, even for the same device.
29
30 .. note:: Support is limited to a number of operating systems. On some
31 systems description and hardware ID will not be available
32 (``None``).
33
34 :platform: Posix (/dev files)
35 :platform: Linux (/dev files, sysfs and lsusb)
36 :platform: OSX (iokit)
37 :platform: Windows (setupapi, registry)
38
39
40.. function:: grep(regexp)
41
42 :param regexp: regular expression (see stdlib :mod:`re`)
Chris Liechti3ee3eff2015-11-13 02:01:01 +010043 :return: an iterable that yields :class:`ListPortInfo` objects, see also :func:`comports`.
Chris Liechti589c92a2015-09-04 23:04:34 +020044
45 Search for ports using a regular expression. Port name, description and
46 hardware ID are searched (case insensitive). The function returns an
Chris Liechtid32faae2015-12-21 23:40:43 +010047 iterable that contains the same tuples that :func:`comport` generates, but
48 includes only those entries that match the regexp.
Chris Liechti589c92a2015-09-04 23:04:34 +020049
Chris Liechti3ee3eff2015-11-13 02:01:01 +010050.. class:: ListPortInfo
51
Chris Liechti73034f62015-11-20 23:16:39 +010052 This object holds information about a serial port. It supports indexed
Chris Liechti3ee3eff2015-11-13 02:01:01 +010053 access for backwards compatibility, as in ``port, desc, hwid = info``.
54
55 .. attribute:: device
56
57 Full device name/path, e.g. ``/dev/ttyUSB0``. This is also the
58 information returned as first element when accessed by index.
59
60 .. attribute:: name
61
62 Short device name, e.g. ``ttyUSB0``.
63
64 .. attribute:: description
65
66 Human readable description or ``n/a``. This is also the information
67 returned as second element when accessed by index.
68
69 .. attribute:: hwid
70
71 Technical description or ``n/a``. This is also the information
72 returned as third element when accessed by index.
73
74 USB specific data, these are all ``None`` if it is not a USB device (or the
Chris Liechti73034f62015-11-20 23:16:39 +010075 platform does not support extended info).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010076
77 .. attribute:: vid
78
Chris Liechti73034f62015-11-20 23:16:39 +010079 USB Vendor ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010080
81 .. attribute:: pid
82
Chris Liechti73034f62015-11-20 23:16:39 +010083 USB product ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010084
85 .. attribute:: serial_number
86
Chris Liechti73034f62015-11-20 23:16:39 +010087 USB serial number as a string.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010088
89 .. attribute:: location
90
91 USB device location string ("<bus>-<port>[-<port>]...")
92
93 .. attribute:: manufacturer
94
Chris Liechti73034f62015-11-20 23:16:39 +010095 USB manufacturer string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010096
97 .. attribute:: product
98
Chris Liechti73034f62015-11-20 23:16:39 +010099 USB product string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100100
101 .. attribute:: interface
102
103 Interface specifc description, e.g. used in compound USB devices.
104
Chris Liechti73034f62015-11-20 23:16:39 +0100105 Comparison operators are implemented such that the ``ListPortInfo`` objects
106 can be sorted by ``device``. Strings are split into groups of numbers and
107 text so that the order is "natural" (i.e. ``com1`` < ``com2`` <
108 ``com10``).
Chris Liechti589c92a2015-09-04 23:04:34 +0200109
Chris Liechtid32faae2015-12-21 23:40:43 +0100110
111**Command line usage**
Chris Liechti589c92a2015-09-04 23:04:34 +0200112
113Help for ``python -m serial.tools.list_ports``::
114
115 usage: list_ports.py [-h] [-v] [-q] [-n N] [regexp]
116
117 Serial port enumeration
118
119 positional arguments:
120 regexp only show ports that match this regex
121
122 optional arguments:
123 -h, --help show this help message and exit
124 -v, --verbose show more messages
125 -q, --quiet suppress all messages
126 -n N only output the N-th entry
127
128Examples:
129
130- List all ports with details::
131
Chris Liechtid32faae2015-12-21 23:40:43 +0100132 $ python -m serial.tools.list_ports -v
133 /dev/ttyS0
134 desc: ttyS0
135 hwid: PNP0501
136 /dev/ttyUSB0
137 desc: CP2102 USB to UART Bridge Controller
138 hwid: USB VID:PID=10C4:EA60 SER=0001 LOCATION=2-1.6
139 2 ports found
140
Chris Liechti589c92a2015-09-04 23:04:34 +0200141
142- List the 2nd port matching a USB VID:PID pattern::
143
Chris Liechtid32faae2015-12-21 23:40:43 +0100144 $ python -m serial.tools.list_ports 1234:5678 -q -n 2
145 /dev/ttyUSB1
Chris Liechti589c92a2015-09-04 23:04:34 +0200146
147.. versionadded:: 2.6
Chris Liechtid32faae2015-12-21 23:40:43 +0100148.. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple
Chris Liechti589c92a2015-09-04 23:04:34 +0200149
150
151.. _miniterm:
152
153serial.tools.miniterm
154=====================
155.. module:: serial.tools.miniterm
156
157This is a console application that provides a small terminal application.
158Miniterm itself does not implement any terminal features such as VT102
159compatibility. However it may inherit these features from the terminal it is run.
160For example on GNU/Linux running from an xterm it will support the escape
161sequences of the xterm. On Windows the typical console window is dumb and does
162not support any escapes. When ANSI.sys is loaded it supports some escapes.
163
164Miniterm::
165
166 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
167 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
168
169Command line options can be given so that binary data including escapes for
170terminals are escaped or output as hex.
171
172Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
173such as ``rfc2217:://<host>:<port>`` respectively ``socket://<host>:<port>`` as
174*port* argument when invoking.
175
176Command line options ``python -m serial.tools.miniterm -h``::
177
178 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
179 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
180 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
181 [--menu-char NUM] [-q] [--develop]
182 [port] [baudrate]
183
184 Miniterm - A simple terminal program for the serial port.
185
186 positional arguments:
187 port serial port name
188 baudrate set baud rate, default: 9600
189
190 optional arguments:
191 -h, --help show this help message and exit
192
193 port settings:
194 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
195 --rtscts enable RTS/CTS flow control (default off)
196 --xonxoff enable software flow control (default off)
197 --rts RTS set initial RTS line state (possible values: 0, 1)
198 --dtr DTR set initial DTR line state (possible values: 0, 1)
Chris Liechtidfa2d6c2015-12-25 17:25:30 +0100199 --ask ask again for port when open fails
Chris Liechti589c92a2015-09-04 23:04:34 +0200200
201 data handling:
202 -e, --echo enable local echo (default off)
203 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
204 Latin1, UTF-8), default: UTF-8
205 -f NAME, --filter NAME
206 add text transformation
207 --eol {CR,LF,CRLF} end of line mode
208 --raw Do no apply any encodings/transformations
209
210 hotkeys:
211 --exit-char NUM Unicode of special character that is used to exit the
212 application, default: 29
213 --menu-char NUM Unicode code of special character that is used to
214 control miniterm (menu), default: 20
215
216 diagnostics:
217 -q, --quiet suppress non-error messages
218 --develop show Python traceback on error
219
220
221Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is
222running shows the help text::
223
224 --- pySerial (3.0a) - miniterm - help
225 ---
226 --- Ctrl+] Exit program
227 --- Ctrl+T Menu escape key, followed by:
228 --- Menu keys:
229 --- Ctrl+T Send the menu character itself to remote
230 --- Ctrl+] Send the exit character itself to remote
231 --- Ctrl+I Show info
232 --- Ctrl+U Upload file (prompt will be shown)
233 --- Ctrl+A encoding
234 --- Ctrl+F edit filters
235 --- Toggles:
236 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
237 --- Ctrl+E echo Ctrl+L EOL
238 ---
239 --- Port settings (Ctrl+T followed by the following):
240 --- p change port
241 --- 7 8 set data bits
242 --- N E O S M change parity (None, Even, Odd, Space, Mark)
243 --- 1 2 3 set stop bits (1, 2, 1.5)
244 --- b change baud rate
245 --- x X disable/enable software flow control
246 --- r R disable/enable hardware flow control
247
248.. versionchanged:: 2.5
249 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
250.. versionchanged:: 2.6
251 File moved from the examples to :mod:`serial.tools.miniterm`.
252.. versionchanged:: 3.0
253 Apply encoding on serial port, convert to Unicode for console.
254 Added new filters, default to stripping terminal control sequences.
Chris Liechtidfa2d6c2015-12-25 17:25:30 +0100255 Added --ask option.
Chris Liechti589c92a2015-09-04 23:04:34 +0200256