blob: 8ed7fcee5a598f8c9168479c02b83265331ac5a2 [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
Chris Liechticc4f5b42017-03-18 23:51:42 +010015.. function:: comports(include_links=False)
Chris Liechti589c92a2015-09-04 23:04:34 +020016
Chris Liechticc4f5b42017-03-18 23:51:42 +010017 :param bool include_links: include symlinks under ``/dev`` when they point
18 to a serial port
Chris Liechti06ae1dc2016-05-05 23:54:35 +020019 :return: a list containing :class:`ListPortInfo` objects.
Chris Liechti589c92a2015-09-04 23:04:34 +020020
Chris Liechti06ae1dc2016-05-05 23:54:35 +020021 The function returns a list of :obj:`ListPortInfo` objects.
Chris Liechti589c92a2015-09-04 23:04:34 +020022
23 Items are returned in no particular order. It may make sense to sort the
24 items. Also note that the reported strings are different across platforms
25 and operating systems, even for the same device.
26
27 .. note:: Support is limited to a number of operating systems. On some
28 systems description and hardware ID will not be available
29 (``None``).
30
Chris Liechticc4f5b42017-03-18 23:51:42 +010031 Under Linux, OSX and Windows, extended information will be available for
Chris Liechtibdf21a12017-03-19 21:55:18 +010032 USB devices (e.g. the :attr:`ListPortInfo.hwid` string contains `VID:PID`,
33 `SER` (serial number), `LOCATION` (hierarchy), which makes them searchable
34 via :func:`grep`. The USB info is also available as attributes of
35 :attr:`ListPortInfo`.
Chris Liechticc4f5b42017-03-18 23:51:42 +010036
Chris Liechtibdf21a12017-03-19 21:55:18 +010037 If *include_links* is true, all devices under ``/dev`` are inspected and
Chris Liechticc4f5b42017-03-18 23:51:42 +010038 tested if they are a link to a known serial port device. These entries
Chris Liechtibdf21a12017-03-19 21:55:18 +010039 will include ``LINK`` in their ``hwid`` string. This implies that the same
40 device listed twice, once under its original name and once under linked
41 name.
Chris Liechticc4f5b42017-03-18 23:51:42 +010042
Chris Liechti589c92a2015-09-04 23:04:34 +020043 :platform: Posix (/dev files)
Chris Liechti6c693d72016-01-17 20:23:42 +010044 :platform: Linux (/dev files, sysfs)
Chris Liechti589c92a2015-09-04 23:04:34 +020045 :platform: OSX (iokit)
46 :platform: Windows (setupapi, registry)
47
48
Chris Liechticc4f5b42017-03-18 23:51:42 +010049.. function:: grep(regexp, include_links=False)
Chris Liechti589c92a2015-09-04 23:04:34 +020050
51 :param regexp: regular expression (see stdlib :mod:`re`)
Chris Liechticc4f5b42017-03-18 23:51:42 +010052 :param bool include_links: include symlinks under ``/dev`` when they point
53 to a serial port
Chris Liechti6c693d72016-01-17 20:23:42 +010054 :return: an iterable that yields :class:`ListPortInfo` objects, see also
55 :func:`comports`.
Chris Liechti589c92a2015-09-04 23:04:34 +020056
Chris Liechtibdf21a12017-03-19 21:55:18 +010057 Search for ports using a regular expression. Port ``name``,
58 ``description`` and ``hwid`` are searched (case insensitive). The function
59 returns an iterable that contains the same data that :func:`comports`
60 generates, but includes only those entries that match the regexp.
Chris Liechti589c92a2015-09-04 23:04:34 +020061
Chris Liechti6c693d72016-01-17 20:23:42 +010062
Chris Liechti3ee3eff2015-11-13 02:01:01 +010063.. class:: ListPortInfo
64
Chris Liechti73034f62015-11-20 23:16:39 +010065 This object holds information about a serial port. It supports indexed
Chris Liechti3ee3eff2015-11-13 02:01:01 +010066 access for backwards compatibility, as in ``port, desc, hwid = info``.
67
68 .. attribute:: device
69
70 Full device name/path, e.g. ``/dev/ttyUSB0``. This is also the
71 information returned as first element when accessed by index.
72
73 .. attribute:: name
74
75 Short device name, e.g. ``ttyUSB0``.
76
77 .. attribute:: description
78
79 Human readable description or ``n/a``. This is also the information
80 returned as second element when accessed by index.
81
82 .. attribute:: hwid
83
84 Technical description or ``n/a``. This is also the information
85 returned as third element when accessed by index.
86
Chris Liechti6c693d72016-01-17 20:23:42 +010087 USB specific data, these are all ``None`` if it is not an USB device (or the
Chris Liechti73034f62015-11-20 23:16:39 +010088 platform does not support extended info).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010089
90 .. attribute:: vid
91
Chris Liechti73034f62015-11-20 23:16:39 +010092 USB Vendor ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010093
94 .. attribute:: pid
95
Chris Liechti73034f62015-11-20 23:16:39 +010096 USB product ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010097
98 .. attribute:: serial_number
99
Chris Liechti73034f62015-11-20 23:16:39 +0100100 USB serial number as a string.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100101
102 .. attribute:: location
103
104 USB device location string ("<bus>-<port>[-<port>]...")
105
106 .. attribute:: manufacturer
107
Chris Liechti73034f62015-11-20 23:16:39 +0100108 USB manufacturer string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100109
110 .. attribute:: product
111
Chris Liechti73034f62015-11-20 23:16:39 +0100112 USB product string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100113
114 .. attribute:: interface
115
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100116 Interface specific description, e.g. used in compound USB devices.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100117
Chris Liechti6c693d72016-01-17 20:23:42 +0100118 Comparison operators are implemented such that the :obj:`ListPortInfo` objects
Chris Liechti73034f62015-11-20 23:16:39 +0100119 can be sorted by ``device``. Strings are split into groups of numbers and
120 text so that the order is "natural" (i.e. ``com1`` < ``com2`` <
121 ``com10``).
Chris Liechti589c92a2015-09-04 23:04:34 +0200122
Chris Liechtid32faae2015-12-21 23:40:43 +0100123
124**Command line usage**
Chris Liechti589c92a2015-09-04 23:04:34 +0200125
126Help for ``python -m serial.tools.list_ports``::
127
Chris Liechtibdf21a12017-03-19 21:55:18 +0100128 usage: list_ports.py [-h] [-v] [-q] [-n N] [-s] [regexp]
Chris Liechti589c92a2015-09-04 23:04:34 +0200129
130 Serial port enumeration
131
132 positional arguments:
Chris Liechtibdf21a12017-03-19 21:55:18 +0100133 regexp only show ports that match this regex
Chris Liechti589c92a2015-09-04 23:04:34 +0200134
135 optional arguments:
Chris Liechtibdf21a12017-03-19 21:55:18 +0100136 -h, --help show this help message and exit
137 -v, --verbose show more messages
138 -q, --quiet suppress all messages
139 -n N only output the N-th entry
140 -s, --include-links include entries that are symlinks to real devices
141
Chris Liechti589c92a2015-09-04 23:04:34 +0200142
143Examples:
144
145- List all ports with details::
146
Chris Liechtid32faae2015-12-21 23:40:43 +0100147 $ python -m serial.tools.list_ports -v
148 /dev/ttyS0
149 desc: ttyS0
150 hwid: PNP0501
151 /dev/ttyUSB0
152 desc: CP2102 USB to UART Bridge Controller
153 hwid: USB VID:PID=10C4:EA60 SER=0001 LOCATION=2-1.6
154 2 ports found
155
Chris Liechti589c92a2015-09-04 23:04:34 +0200156
157- List the 2nd port matching a USB VID:PID pattern::
158
Chris Liechtid32faae2015-12-21 23:40:43 +0100159 $ python -m serial.tools.list_ports 1234:5678 -q -n 2
160 /dev/ttyUSB1
Chris Liechti589c92a2015-09-04 23:04:34 +0200161
162.. versionadded:: 2.6
Chris Liechtid32faae2015-12-21 23:40:43 +0100163.. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple
Chris Liechti589c92a2015-09-04 23:04:34 +0200164
165
166.. _miniterm:
167
168serial.tools.miniterm
169=====================
170.. module:: serial.tools.miniterm
171
172This is a console application that provides a small terminal application.
Chris Liechti6c693d72016-01-17 20:23:42 +0100173
Chris Liechti589c92a2015-09-04 23:04:34 +0200174Miniterm itself does not implement any terminal features such as VT102
175compatibility. However it may inherit these features from the terminal it is run.
176For example on GNU/Linux running from an xterm it will support the escape
177sequences of the xterm. On Windows the typical console window is dumb and does
178not support any escapes. When ANSI.sys is loaded it supports some escapes.
179
Chris Liechti6c693d72016-01-17 20:23:42 +0100180The default is to filter terminal control characters, see ``--filter`` for
181different options.
182
Chris Liechti589c92a2015-09-04 23:04:34 +0200183Miniterm::
184
185 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
186 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
187
188Command line options can be given so that binary data including escapes for
189terminals are escaped or output as hex.
190
191Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
Chris Liechti894d0dd2016-02-08 23:01:01 +0100192such as ``rfc2217://<host>:<port>`` respectively ``socket://<host>:<port>`` as
Chris Liechti589c92a2015-09-04 23:04:34 +0200193*port* argument when invoking.
194
195Command line options ``python -m serial.tools.miniterm -h``::
196
197 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
198 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
199 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
200 [--menu-char NUM] [-q] [--develop]
201 [port] [baudrate]
202
203 Miniterm - A simple terminal program for the serial port.
204
205 positional arguments:
206 port serial port name
207 baudrate set baud rate, default: 9600
208
209 optional arguments:
210 -h, --help show this help message and exit
211
212 port settings:
213 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
214 --rtscts enable RTS/CTS flow control (default off)
215 --xonxoff enable software flow control (default off)
216 --rts RTS set initial RTS line state (possible values: 0, 1)
217 --dtr DTR set initial DTR line state (possible values: 0, 1)
Chris Liechtidfa2d6c2015-12-25 17:25:30 +0100218 --ask ask again for port when open fails
Chris Liechti589c92a2015-09-04 23:04:34 +0200219
220 data handling:
221 -e, --echo enable local echo (default off)
222 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
223 Latin1, UTF-8), default: UTF-8
224 -f NAME, --filter NAME
225 add text transformation
226 --eol {CR,LF,CRLF} end of line mode
227 --raw Do no apply any encodings/transformations
228
229 hotkeys:
230 --exit-char NUM Unicode of special character that is used to exit the
231 application, default: 29
232 --menu-char NUM Unicode code of special character that is used to
233 control miniterm (menu), default: 20
234
235 diagnostics:
236 -q, --quiet suppress non-error messages
237 --develop show Python traceback on error
238
239
Chris Liechti6c693d72016-01-17 20:23:42 +0100240Available filters (``--filter`` option):
241
242- ``colorize``: Apply different colors for received and echo
243- ``debug``: Print what is sent and received
244- ``default``: remove typical terminal control codes from input
245- ``direct``: do-nothing: forward all data unchanged
246- ``nocontrol``: Remove all control codes, incl. ``CR+LF``
247- ``printable``: Show decimal code for all non-ASCII characters and replace most control codes
248
249
250Miniterm supports some control functions while being connected.
251Typing :kbd:`Ctrl+T Ctrl+H` when it is running shows the help text::
Chris Liechti589c92a2015-09-04 23:04:34 +0200252
253 --- pySerial (3.0a) - miniterm - help
254 ---
255 --- Ctrl+] Exit program
256 --- Ctrl+T Menu escape key, followed by:
257 --- Menu keys:
258 --- Ctrl+T Send the menu character itself to remote
259 --- Ctrl+] Send the exit character itself to remote
260 --- Ctrl+I Show info
261 --- Ctrl+U Upload file (prompt will be shown)
262 --- Ctrl+A encoding
263 --- Ctrl+F edit filters
264 --- Toggles:
265 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
266 --- Ctrl+E echo Ctrl+L EOL
267 ---
268 --- Port settings (Ctrl+T followed by the following):
269 --- p change port
270 --- 7 8 set data bits
271 --- N E O S M change parity (None, Even, Odd, Space, Mark)
272 --- 1 2 3 set stop bits (1, 2, 1.5)
273 --- b change baud rate
274 --- x X disable/enable software flow control
275 --- r R disable/enable hardware flow control
276
Chris Liechtid2bf42e2017-07-18 01:54:40 +0200277:kbd:`Ctrl+T s` suspends the connection (port is opened) and reconnects when a
Chris Liechti591c4512017-07-16 22:05:04 +0200278key is pressed. This can be used to temporarily access the serial port with an
Chris Liechtid2bf42e2017-07-18 01:54:40 +0200279other application, without exiting miniterm. If reconnecting fails it is
280also possible to exit (:kbd:`Ctrl+]`) or change the port (:kbd:`p`).
Chris Liechti591c4512017-07-16 22:05:04 +0200281
Chris Liechti589c92a2015-09-04 23:04:34 +0200282.. versionchanged:: 2.5
283 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
284.. versionchanged:: 2.6
285 File moved from the examples to :mod:`serial.tools.miniterm`.
286.. versionchanged:: 3.0
287 Apply encoding on serial port, convert to Unicode for console.
288 Added new filters, default to stripping terminal control sequences.
Chris Liechti6c693d72016-01-17 20:23:42 +0100289 Added ``--ask`` option.
Chris Liechti589c92a2015-09-04 23:04:34 +0200290