blob: 45e7aef8d6f8c78685ad8bde5d9b1a8fd0f0aa06 [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 Liechti06ae1dc2016-05-05 23:54:35 +020017 :return: a list containing :class:`ListPortInfo` objects.
Chris Liechti589c92a2015-09-04 23:04:34 +020018
Chris Liechti06ae1dc2016-05-05 23:54:35 +020019 The function returns a list of :obj:`ListPortInfo` objects.
Chris Liechti589c92a2015-09-04 23:04:34 +020020
21 Items are returned in no particular order. It may make sense to sort the
22 items. Also note that the reported strings are different across platforms
23 and operating systems, even for the same device.
24
25 .. note:: Support is limited to a number of operating systems. On some
26 systems description and hardware ID will not be available
27 (``None``).
28
29 :platform: Posix (/dev files)
Chris Liechti6c693d72016-01-17 20:23:42 +010030 :platform: Linux (/dev files, sysfs)
Chris Liechti589c92a2015-09-04 23:04:34 +020031 :platform: OSX (iokit)
32 :platform: Windows (setupapi, registry)
33
34
35.. function:: grep(regexp)
36
37 :param regexp: regular expression (see stdlib :mod:`re`)
Chris Liechti6c693d72016-01-17 20:23:42 +010038 :return: an iterable that yields :class:`ListPortInfo` objects, see also
39 :func:`comports`.
Chris Liechti589c92a2015-09-04 23:04:34 +020040
41 Search for ports using a regular expression. Port name, description and
42 hardware ID are searched (case insensitive). The function returns an
Chris Liechti6c693d72016-01-17 20:23:42 +010043 iterable that contains the same data that :func:`comports` generates, but
Chris Liechtid32faae2015-12-21 23:40:43 +010044 includes only those entries that match the regexp.
Chris Liechti589c92a2015-09-04 23:04:34 +020045
Chris Liechti6c693d72016-01-17 20:23:42 +010046
Chris Liechti3ee3eff2015-11-13 02:01:01 +010047.. class:: ListPortInfo
48
Chris Liechti73034f62015-11-20 23:16:39 +010049 This object holds information about a serial port. It supports indexed
Chris Liechti3ee3eff2015-11-13 02:01:01 +010050 access for backwards compatibility, as in ``port, desc, hwid = info``.
51
52 .. attribute:: device
53
54 Full device name/path, e.g. ``/dev/ttyUSB0``. This is also the
55 information returned as first element when accessed by index.
56
57 .. attribute:: name
58
59 Short device name, e.g. ``ttyUSB0``.
60
61 .. attribute:: description
62
63 Human readable description or ``n/a``. This is also the information
64 returned as second element when accessed by index.
65
66 .. attribute:: hwid
67
68 Technical description or ``n/a``. This is also the information
69 returned as third element when accessed by index.
70
Chris Liechti6c693d72016-01-17 20:23:42 +010071 USB specific data, these are all ``None`` if it is not an USB device (or the
Chris Liechti73034f62015-11-20 23:16:39 +010072 platform does not support extended info).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010073
74 .. attribute:: vid
75
Chris Liechti73034f62015-11-20 23:16:39 +010076 USB Vendor ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010077
78 .. attribute:: pid
79
Chris Liechti73034f62015-11-20 23:16:39 +010080 USB product ID (integer, 0...65535).
Chris Liechti3ee3eff2015-11-13 02:01:01 +010081
82 .. attribute:: serial_number
83
Chris Liechti73034f62015-11-20 23:16:39 +010084 USB serial number as a string.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010085
86 .. attribute:: location
87
88 USB device location string ("<bus>-<port>[-<port>]...")
89
90 .. attribute:: manufacturer
91
Chris Liechti73034f62015-11-20 23:16:39 +010092 USB manufacturer string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010093
94 .. attribute:: product
95
Chris Liechti73034f62015-11-20 23:16:39 +010096 USB product string, as reported by device.
Chris Liechti3ee3eff2015-11-13 02:01:01 +010097
98 .. attribute:: interface
99
100 Interface specifc description, e.g. used in compound USB devices.
101
Chris Liechti6c693d72016-01-17 20:23:42 +0100102 Comparison operators are implemented such that the :obj:`ListPortInfo` objects
Chris Liechti73034f62015-11-20 23:16:39 +0100103 can be sorted by ``device``. Strings are split into groups of numbers and
104 text so that the order is "natural" (i.e. ``com1`` < ``com2`` <
105 ``com10``).
Chris Liechti589c92a2015-09-04 23:04:34 +0200106
Chris Liechtid32faae2015-12-21 23:40:43 +0100107
108**Command line usage**
Chris Liechti589c92a2015-09-04 23:04:34 +0200109
110Help for ``python -m serial.tools.list_ports``::
111
112 usage: list_ports.py [-h] [-v] [-q] [-n N] [regexp]
113
114 Serial port enumeration
115
116 positional arguments:
117 regexp only show ports that match this regex
118
119 optional arguments:
120 -h, --help show this help message and exit
121 -v, --verbose show more messages
122 -q, --quiet suppress all messages
123 -n N only output the N-th entry
124
125Examples:
126
127- List all ports with details::
128
Chris Liechtid32faae2015-12-21 23:40:43 +0100129 $ python -m serial.tools.list_ports -v
130 /dev/ttyS0
131 desc: ttyS0
132 hwid: PNP0501
133 /dev/ttyUSB0
134 desc: CP2102 USB to UART Bridge Controller
135 hwid: USB VID:PID=10C4:EA60 SER=0001 LOCATION=2-1.6
136 2 ports found
137
Chris Liechti589c92a2015-09-04 23:04:34 +0200138
139- List the 2nd port matching a USB VID:PID pattern::
140
Chris Liechtid32faae2015-12-21 23:40:43 +0100141 $ python -m serial.tools.list_ports 1234:5678 -q -n 2
142 /dev/ttyUSB1
Chris Liechti589c92a2015-09-04 23:04:34 +0200143
144.. versionadded:: 2.6
Chris Liechtid32faae2015-12-21 23:40:43 +0100145.. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple
Chris Liechti589c92a2015-09-04 23:04:34 +0200146
147
148.. _miniterm:
149
150serial.tools.miniterm
151=====================
152.. module:: serial.tools.miniterm
153
154This is a console application that provides a small terminal application.
Chris Liechti6c693d72016-01-17 20:23:42 +0100155
Chris Liechti589c92a2015-09-04 23:04:34 +0200156Miniterm itself does not implement any terminal features such as VT102
157compatibility. However it may inherit these features from the terminal it is run.
158For example on GNU/Linux running from an xterm it will support the escape
159sequences of the xterm. On Windows the typical console window is dumb and does
160not support any escapes. When ANSI.sys is loaded it supports some escapes.
161
Chris Liechti6c693d72016-01-17 20:23:42 +0100162The default is to filter terminal control characters, see ``--filter`` for
163different options.
164
Chris Liechti589c92a2015-09-04 23:04:34 +0200165Miniterm::
166
167 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
168 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
169
170Command line options can be given so that binary data including escapes for
171terminals are escaped or output as hex.
172
173Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
Chris Liechti894d0dd2016-02-08 23:01:01 +0100174such as ``rfc2217://<host>:<port>`` respectively ``socket://<host>:<port>`` as
Chris Liechti589c92a2015-09-04 23:04:34 +0200175*port* argument when invoking.
176
177Command line options ``python -m serial.tools.miniterm -h``::
178
179 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
180 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
181 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
182 [--menu-char NUM] [-q] [--develop]
183 [port] [baudrate]
184
185 Miniterm - A simple terminal program for the serial port.
186
187 positional arguments:
188 port serial port name
189 baudrate set baud rate, default: 9600
190
191 optional arguments:
192 -h, --help show this help message and exit
193
194 port settings:
195 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
196 --rtscts enable RTS/CTS flow control (default off)
197 --xonxoff enable software flow control (default off)
198 --rts RTS set initial RTS line state (possible values: 0, 1)
199 --dtr DTR set initial DTR line state (possible values: 0, 1)
Chris Liechtidfa2d6c2015-12-25 17:25:30 +0100200 --ask ask again for port when open fails
Chris Liechti589c92a2015-09-04 23:04:34 +0200201
202 data handling:
203 -e, --echo enable local echo (default off)
204 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
205 Latin1, UTF-8), default: UTF-8
206 -f NAME, --filter NAME
207 add text transformation
208 --eol {CR,LF,CRLF} end of line mode
209 --raw Do no apply any encodings/transformations
210
211 hotkeys:
212 --exit-char NUM Unicode of special character that is used to exit the
213 application, default: 29
214 --menu-char NUM Unicode code of special character that is used to
215 control miniterm (menu), default: 20
216
217 diagnostics:
218 -q, --quiet suppress non-error messages
219 --develop show Python traceback on error
220
221
Chris Liechti6c693d72016-01-17 20:23:42 +0100222Available filters (``--filter`` option):
223
224- ``colorize``: Apply different colors for received and echo
225- ``debug``: Print what is sent and received
226- ``default``: remove typical terminal control codes from input
227- ``direct``: do-nothing: forward all data unchanged
228- ``nocontrol``: Remove all control codes, incl. ``CR+LF``
229- ``printable``: Show decimal code for all non-ASCII characters and replace most control codes
230
231
232Miniterm supports some control functions while being connected.
233Typing :kbd:`Ctrl+T Ctrl+H` when it is running shows the help text::
Chris Liechti589c92a2015-09-04 23:04:34 +0200234
235 --- pySerial (3.0a) - miniterm - help
236 ---
237 --- Ctrl+] Exit program
238 --- Ctrl+T Menu escape key, followed by:
239 --- Menu keys:
240 --- Ctrl+T Send the menu character itself to remote
241 --- Ctrl+] Send the exit character itself to remote
242 --- Ctrl+I Show info
243 --- Ctrl+U Upload file (prompt will be shown)
244 --- Ctrl+A encoding
245 --- Ctrl+F edit filters
246 --- Toggles:
247 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
248 --- Ctrl+E echo Ctrl+L EOL
249 ---
250 --- Port settings (Ctrl+T followed by the following):
251 --- p change port
252 --- 7 8 set data bits
253 --- N E O S M change parity (None, Even, Odd, Space, Mark)
254 --- 1 2 3 set stop bits (1, 2, 1.5)
255 --- b change baud rate
256 --- x X disable/enable software flow control
257 --- r R disable/enable hardware flow control
258
259.. versionchanged:: 2.5
260 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
261.. versionchanged:: 2.6
262 File moved from the examples to :mod:`serial.tools.miniterm`.
263.. versionchanged:: 3.0
264 Apply encoding on serial port, convert to Unicode for console.
265 Added new filters, default to stripping terminal control sequences.
Chris Liechti6c693d72016-01-17 20:23:42 +0100266 Added ``--ask`` option.
Chris Liechti589c92a2015-09-04 23:04:34 +0200267