blob: 7bd230448bc12f3d018ec0557613775b9019d77d [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
47 iterable that contains the same tuples that :func:`comport` generates but
48 only those that match the regexp.
49
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
110Command line usage
111
112Help for ``python -m serial.tools.list_ports``::
113
114 usage: list_ports.py [-h] [-v] [-q] [-n N] [regexp]
115
116 Serial port enumeration
117
118 positional arguments:
119 regexp only show ports that match this regex
120
121 optional arguments:
122 -h, --help show this help message and exit
123 -v, --verbose show more messages
124 -q, --quiet suppress all messages
125 -n N only output the N-th entry
126
127Examples:
128
129- List all ports with details::
130
131 python -m serial.tools.list_ports -v
132
133- List the 2nd port matching a USB VID:PID pattern::
134
135 python -m serial.tools.list_ports 1234:5678 -q -n 2
136
137.. versionadded:: 2.6
Chris Liechti3ee3eff2015-11-13 02:01:01 +0100138.. versionchanged:: 3.0 returning ListPortInfo objects instead of a tuple
Chris Liechti589c92a2015-09-04 23:04:34 +0200139
140
141.. _miniterm:
142
143serial.tools.miniterm
144=====================
145.. module:: serial.tools.miniterm
146
147This is a console application that provides a small terminal application.
148Miniterm itself does not implement any terminal features such as VT102
149compatibility. However it may inherit these features from the terminal it is run.
150For example on GNU/Linux running from an xterm it will support the escape
151sequences of the xterm. On Windows the typical console window is dumb and does
152not support any escapes. When ANSI.sys is loaded it supports some escapes.
153
154Miniterm::
155
156 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
157 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
158
159Command line options can be given so that binary data including escapes for
160terminals are escaped or output as hex.
161
162Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
163such as ``rfc2217:://<host>:<port>`` respectively ``socket://<host>:<port>`` as
164*port* argument when invoking.
165
166Command line options ``python -m serial.tools.miniterm -h``::
167
168 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
169 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
170 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
171 [--menu-char NUM] [-q] [--develop]
172 [port] [baudrate]
173
174 Miniterm - A simple terminal program for the serial port.
175
176 positional arguments:
177 port serial port name
178 baudrate set baud rate, default: 9600
179
180 optional arguments:
181 -h, --help show this help message and exit
182
183 port settings:
184 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
185 --rtscts enable RTS/CTS flow control (default off)
186 --xonxoff enable software flow control (default off)
187 --rts RTS set initial RTS line state (possible values: 0, 1)
188 --dtr DTR set initial DTR line state (possible values: 0, 1)
189
190 data handling:
191 -e, --echo enable local echo (default off)
192 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
193 Latin1, UTF-8), default: UTF-8
194 -f NAME, --filter NAME
195 add text transformation
196 --eol {CR,LF,CRLF} end of line mode
197 --raw Do no apply any encodings/transformations
198
199 hotkeys:
200 --exit-char NUM Unicode of special character that is used to exit the
201 application, default: 29
202 --menu-char NUM Unicode code of special character that is used to
203 control miniterm (menu), default: 20
204
205 diagnostics:
206 -q, --quiet suppress non-error messages
207 --develop show Python traceback on error
208
209
210Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is
211running shows the help text::
212
213 --- pySerial (3.0a) - miniterm - help
214 ---
215 --- Ctrl+] Exit program
216 --- Ctrl+T Menu escape key, followed by:
217 --- Menu keys:
218 --- Ctrl+T Send the menu character itself to remote
219 --- Ctrl+] Send the exit character itself to remote
220 --- Ctrl+I Show info
221 --- Ctrl+U Upload file (prompt will be shown)
222 --- Ctrl+A encoding
223 --- Ctrl+F edit filters
224 --- Toggles:
225 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
226 --- Ctrl+E echo Ctrl+L EOL
227 ---
228 --- Port settings (Ctrl+T followed by the following):
229 --- p change port
230 --- 7 8 set data bits
231 --- N E O S M change parity (None, Even, Odd, Space, Mark)
232 --- 1 2 3 set stop bits (1, 2, 1.5)
233 --- b change baud rate
234 --- x X disable/enable software flow control
235 --- r R disable/enable hardware flow control
236
237.. versionchanged:: 2.5
238 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
239.. versionchanged:: 2.6
240 File moved from the examples to :mod:`serial.tools.miniterm`.
241.. versionchanged:: 3.0
242 Apply encoding on serial port, convert to Unicode for console.
243 Added new filters, default to stripping terminal control sequences.
244