blob: 0613075c22344e5046cb580b480a617cca74b266 [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
32 USB devices (e.g. `VID:PID`, `SER` (serial number), `LOCATION` (hierarchy).
33
34 If ``include_links`` is true, all devices under ``/dev`` are inspected and
35 tested if they are a link to a known serial port device. These entries
36 will include ``LINK`` in their description (hwid). This implies that the
37 same device is listed twice, once under its original name and once under
38 the linked name.
39
Chris Liechti589c92a2015-09-04 23:04:34 +020040 :platform: Posix (/dev files)
Chris Liechti6c693d72016-01-17 20:23:42 +010041 :platform: Linux (/dev files, sysfs)
Chris Liechti589c92a2015-09-04 23:04:34 +020042 :platform: OSX (iokit)
43 :platform: Windows (setupapi, registry)
44
45
Chris Liechticc4f5b42017-03-18 23:51:42 +010046.. function:: grep(regexp, include_links=False)
Chris Liechti589c92a2015-09-04 23:04:34 +020047
48 :param regexp: regular expression (see stdlib :mod:`re`)
Chris Liechticc4f5b42017-03-18 23:51:42 +010049 :param bool include_links: include symlinks under ``/dev`` when they point
50 to a serial port
Chris Liechti6c693d72016-01-17 20:23:42 +010051 :return: an iterable that yields :class:`ListPortInfo` objects, see also
52 :func:`comports`.
Chris Liechti589c92a2015-09-04 23:04:34 +020053
54 Search for ports using a regular expression. Port name, description and
55 hardware ID are searched (case insensitive). The function returns an
Chris Liechti6c693d72016-01-17 20:23:42 +010056 iterable that contains the same data that :func:`comports` generates, but
Chris Liechtid32faae2015-12-21 23:40:43 +010057 includes only those entries that match the regexp.
Chris Liechti589c92a2015-09-04 23:04:34 +020058
Chris Liechti6c693d72016-01-17 20:23:42 +010059
Chris Liechti3ee3eff2015-11-13 02:01:01 +010060.. class:: ListPortInfo
61
Chris Liechti73034f62015-11-20 23:16:39 +010062 This object holds information about a serial port. It supports indexed
Chris Liechti3ee3eff2015-11-13 02:01:01 +010063 access for backwards compatibility, as in ``port, desc, hwid = info``.
64
65 .. attribute:: device
66
67 Full device name/path, e.g. ``/dev/ttyUSB0``. This is also the
68 information returned as first element when accessed by index.
69
70 .. attribute:: name
71
72 Short device name, e.g. ``ttyUSB0``.
73
74 .. attribute:: description
75
76 Human readable description or ``n/a``. This is also the information
77 returned as second element when accessed by index.
78
79 .. attribute:: hwid
80
81 Technical description or ``n/a``. This is also the information
82 returned as third element when accessed by index.
83
Chris Liechti6c693d72016-01-17 20:23:42 +010084 USB specific data, these are all ``None`` if it is not an USB device (or the
Chris Liechti73034f62015-11-20 23:16:39 +010085 platform does not support extended info).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010086
87 .. attribute:: vid
88
Chris Liechti73034f62015-11-20 23:16:39 +010089 USB Vendor ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010090
91 .. attribute:: pid
92
Chris Liechti73034f62015-11-20 23:16:39 +010093 USB product ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010094
95 .. attribute:: serial_number
96
Chris Liechti73034f62015-11-20 23:16:39 +010097 USB serial number as a string.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010098
99 .. attribute:: location
100
101 USB device location string ("<bus>-<port>[-<port>]...")
102
103 .. attribute:: manufacturer
104
Chris Liechti73034f62015-11-20 23:16:39 +0100105 USB manufacturer string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100106
107 .. attribute:: product
108
Chris Liechti73034f62015-11-20 23:16:39 +0100109 USB product string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100110
111 .. attribute:: interface
112
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100113 Interface specific description, e.g. used in compound USB devices.
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100114
Chris Liechti6c693d72016-01-17 20:23:42 +0100115 Comparison operators are implemented such that the :obj:`ListPortInfo` objects
Chris Liechti73034f62015-11-20 23:16:39 +0100116 can be sorted by ``device``. Strings are split into groups of numbers and
117 text so that the order is "natural" (i.e. ``com1`` < ``com2`` <
118 ``com10``).
Chris Liechti589c92a2015-09-04 23:04:34 +0200119
Chris Liechtid32faae2015-12-21 23:40:43 +0100120
121**Command line usage**
Chris Liechti589c92a2015-09-04 23:04:34 +0200122
123Help for ``python -m serial.tools.list_ports``::
124
125 usage: list_ports.py [-h] [-v] [-q] [-n N] [regexp]
126
127 Serial port enumeration
128
129 positional arguments:
130 regexp only show ports that match this regex
131
132 optional arguments:
133 -h, --help show this help message and exit
134 -v, --verbose show more messages
135 -q, --quiet suppress all messages
136 -n N only output the N-th entry
137
138Examples:
139
140- List all ports with details::
141
Chris Liechtid32faae2015-12-21 23:40:43 +0100142 $ python -m serial.tools.list_ports -v
143 /dev/ttyS0
144 desc: ttyS0
145 hwid: PNP0501
146 /dev/ttyUSB0
147 desc: CP2102 USB to UART Bridge Controller
148 hwid: USB VID:PID=10C4:EA60 SER=0001 LOCATION=2-1.6
149 2 ports found
150
Chris Liechti589c92a2015-09-04 23:04:34 +0200151
152- List the 2nd port matching a USB VID:PID pattern::
153
Chris Liechtid32faae2015-12-21 23:40:43 +0100154 $ python -m serial.tools.list_ports 1234:5678 -q -n 2
155 /dev/ttyUSB1
Chris Liechti589c92a2015-09-04 23:04:34 +0200156
157.. versionadded:: 2.6
Chris Liechtid32faae2015-12-21 23:40:43 +0100158.. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple
Chris Liechti589c92a2015-09-04 23:04:34 +0200159
160
161.. _miniterm:
162
163serial.tools.miniterm
164=====================
165.. module:: serial.tools.miniterm
166
167This is a console application that provides a small terminal application.
Chris Liechti6c693d72016-01-17 20:23:42 +0100168
Chris Liechti589c92a2015-09-04 23:04:34 +0200169Miniterm itself does not implement any terminal features such as VT102
170compatibility. However it may inherit these features from the terminal it is run.
171For example on GNU/Linux running from an xterm it will support the escape
172sequences of the xterm. On Windows the typical console window is dumb and does
173not support any escapes. When ANSI.sys is loaded it supports some escapes.
174
Chris Liechti6c693d72016-01-17 20:23:42 +0100175The default is to filter terminal control characters, see ``--filter`` for
176different options.
177
Chris Liechti589c92a2015-09-04 23:04:34 +0200178Miniterm::
179
180 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
181 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
182
183Command line options can be given so that binary data including escapes for
184terminals are escaped or output as hex.
185
186Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
Chris Liechti894d0dd2016-02-08 23:01:01 +0100187such as ``rfc2217://<host>:<port>`` respectively ``socket://<host>:<port>`` as
Chris Liechti589c92a2015-09-04 23:04:34 +0200188*port* argument when invoking.
189
190Command line options ``python -m serial.tools.miniterm -h``::
191
192 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
193 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
194 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
195 [--menu-char NUM] [-q] [--develop]
196 [port] [baudrate]
197
198 Miniterm - A simple terminal program for the serial port.
199
200 positional arguments:
201 port serial port name
202 baudrate set baud rate, default: 9600
203
204 optional arguments:
205 -h, --help show this help message and exit
206
207 port settings:
208 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
209 --rtscts enable RTS/CTS flow control (default off)
210 --xonxoff enable software flow control (default off)
211 --rts RTS set initial RTS line state (possible values: 0, 1)
212 --dtr DTR set initial DTR line state (possible values: 0, 1)
Chris Liechtidfa2d6c2015-12-25 17:25:30 +0100213 --ask ask again for port when open fails
Chris Liechti589c92a2015-09-04 23:04:34 +0200214
215 data handling:
216 -e, --echo enable local echo (default off)
217 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
218 Latin1, UTF-8), default: UTF-8
219 -f NAME, --filter NAME
220 add text transformation
221 --eol {CR,LF,CRLF} end of line mode
222 --raw Do no apply any encodings/transformations
223
224 hotkeys:
225 --exit-char NUM Unicode of special character that is used to exit the
226 application, default: 29
227 --menu-char NUM Unicode code of special character that is used to
228 control miniterm (menu), default: 20
229
230 diagnostics:
231 -q, --quiet suppress non-error messages
232 --develop show Python traceback on error
233
234
Chris Liechti6c693d72016-01-17 20:23:42 +0100235Available filters (``--filter`` option):
236
237- ``colorize``: Apply different colors for received and echo
238- ``debug``: Print what is sent and received
239- ``default``: remove typical terminal control codes from input
240- ``direct``: do-nothing: forward all data unchanged
241- ``nocontrol``: Remove all control codes, incl. ``CR+LF``
242- ``printable``: Show decimal code for all non-ASCII characters and replace most control codes
243
244
245Miniterm supports some control functions while being connected.
246Typing :kbd:`Ctrl+T Ctrl+H` when it is running shows the help text::
Chris Liechti589c92a2015-09-04 23:04:34 +0200247
248 --- pySerial (3.0a) - miniterm - help
249 ---
250 --- Ctrl+] Exit program
251 --- Ctrl+T Menu escape key, followed by:
252 --- Menu keys:
253 --- Ctrl+T Send the menu character itself to remote
254 --- Ctrl+] Send the exit character itself to remote
255 --- Ctrl+I Show info
256 --- Ctrl+U Upload file (prompt will be shown)
257 --- Ctrl+A encoding
258 --- Ctrl+F edit filters
259 --- Toggles:
260 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
261 --- Ctrl+E echo Ctrl+L EOL
262 ---
263 --- Port settings (Ctrl+T followed by the following):
264 --- p change port
265 --- 7 8 set data bits
266 --- N E O S M change parity (None, Even, Odd, Space, Mark)
267 --- 1 2 3 set stop bits (1, 2, 1.5)
268 --- b change baud rate
269 --- x X disable/enable software flow control
270 --- r R disable/enable hardware flow control
271
272.. versionchanged:: 2.5
273 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
274.. versionchanged:: 2.6
275 File moved from the examples to :mod:`serial.tools.miniterm`.
276.. versionchanged:: 3.0
277 Apply encoding on serial port, convert to Unicode for console.
278 Added new filters, default to stripping terminal control sequences.
Chris Liechti6c693d72016-01-17 20:23:42 +0100279 Added ``--ask`` option.
Chris Liechti589c92a2015-09-04 23:04:34 +0200280