Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 1 | ======= |
| 2 | Tools |
| 3 | ======= |
| 4 | |
| 5 | .. module:: serial |
| 6 | |
| 7 | serial.tools.list_ports |
| 8 | ======================= |
| 9 | .. module:: serial.tools.list_ports |
| 10 | |
| 11 | This module can be executed to get a list of ports (``python -m |
| 12 | serial.tools.list_ports``). It also contains the following functions. |
| 13 | |
| 14 | |
Chris Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 15 | .. function:: comports(include_links=False) |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 16 | |
Chris Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 17 | :param bool include_links: include symlinks under ``/dev`` when they point |
| 18 | to a serial port |
Chris Liechti | 06ae1dc | 2016-05-05 23:54:35 +0200 | [diff] [blame] | 19 | :return: a list containing :class:`ListPortInfo` objects. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 20 | |
Chris Liechti | 06ae1dc | 2016-05-05 23:54:35 +0200 | [diff] [blame] | 21 | The function returns a list of :obj:`ListPortInfo` objects. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 22 | |
| 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 Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 31 | Under Linux, OSX and Windows, extended information will be available for |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 32 | 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 Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 36 | |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 37 | If *include_links* is true, all devices under ``/dev`` are inspected and |
Chris Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 38 | tested if they are a link to a known serial port device. These entries |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 39 | 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 Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 42 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 43 | :platform: Posix (/dev files) |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 44 | :platform: Linux (/dev files, sysfs) |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 45 | :platform: OSX (iokit) |
| 46 | :platform: Windows (setupapi, registry) |
| 47 | |
| 48 | |
Chris Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 49 | .. function:: grep(regexp, include_links=False) |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 50 | |
| 51 | :param regexp: regular expression (see stdlib :mod:`re`) |
Chris Liechti | cc4f5b4 | 2017-03-18 23:51:42 +0100 | [diff] [blame] | 52 | :param bool include_links: include symlinks under ``/dev`` when they point |
| 53 | to a serial port |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 54 | :return: an iterable that yields :class:`ListPortInfo` objects, see also |
| 55 | :func:`comports`. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 56 | |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 57 | 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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 61 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 62 | |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 63 | .. class:: ListPortInfo |
| 64 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 65 | This object holds information about a serial port. It supports indexed |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 66 | 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 87 | USB specific data, these are all ``None`` if it is not an USB device (or the |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 88 | platform does not support extended info). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 89 | |
| 90 | .. attribute:: vid |
| 91 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 92 | USB Vendor ID (integer, 0...65535). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 93 | |
| 94 | .. attribute:: pid |
| 95 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 96 | USB product ID (integer, 0...65535). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 97 | |
| 98 | .. attribute:: serial_number |
| 99 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 100 | USB serial number as a string. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 101 | |
| 102 | .. attribute:: location |
| 103 | |
| 104 | USB device location string ("<bus>-<port>[-<port>]...") |
| 105 | |
| 106 | .. attribute:: manufacturer |
| 107 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 108 | USB manufacturer string, as reported by device. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 109 | |
| 110 | .. attribute:: product |
| 111 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 112 | USB product string, as reported by device. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 113 | |
| 114 | .. attribute:: interface |
| 115 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 116 | Interface specific description, e.g. used in compound USB devices. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 117 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 118 | Comparison operators are implemented such that the :obj:`ListPortInfo` objects |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 119 | 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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 122 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 123 | |
| 124 | **Command line usage** |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 125 | |
| 126 | Help for ``python -m serial.tools.list_ports``:: |
| 127 | |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 128 | usage: list_ports.py [-h] [-v] [-q] [-n N] [-s] [regexp] |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 129 | |
| 130 | Serial port enumeration |
| 131 | |
| 132 | positional arguments: |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 133 | regexp only show ports that match this regex |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 134 | |
| 135 | optional arguments: |
Chris Liechti | bdf21a1 | 2017-03-19 21:55:18 +0100 | [diff] [blame] | 136 | -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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 142 | |
| 143 | Examples: |
| 144 | |
| 145 | - List all ports with details:: |
| 146 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 147 | $ 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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 156 | |
| 157 | - List the 2nd port matching a USB VID:PID pattern:: |
| 158 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 159 | $ python -m serial.tools.list_ports 1234:5678 -q -n 2 |
| 160 | /dev/ttyUSB1 |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 161 | |
| 162 | .. versionadded:: 2.6 |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 163 | .. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 164 | |
| 165 | |
| 166 | .. _miniterm: |
| 167 | |
| 168 | serial.tools.miniterm |
| 169 | ===================== |
| 170 | .. module:: serial.tools.miniterm |
| 171 | |
| 172 | This is a console application that provides a small terminal application. |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 173 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 174 | Miniterm itself does not implement any terminal features such as VT102 |
| 175 | compatibility. However it may inherit these features from the terminal it is run. |
| 176 | For example on GNU/Linux running from an xterm it will support the escape |
| 177 | sequences of the xterm. On Windows the typical console window is dumb and does |
| 178 | not support any escapes. When ANSI.sys is loaded it supports some escapes. |
| 179 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 180 | The default is to filter terminal control characters, see ``--filter`` for |
| 181 | different options. |
| 182 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 183 | Miniterm:: |
| 184 | |
| 185 | --- Miniterm on /dev/ttyS0: 9600,8,N,1 --- |
| 186 | --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- |
| 187 | |
| 188 | Command line options can be given so that binary data including escapes for |
| 189 | terminals are escaped or output as hex. |
| 190 | |
| 191 | Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs` |
Chris Liechti | 894d0dd | 2016-02-08 23:01:01 +0100 | [diff] [blame] | 192 | such as ``rfc2217://<host>:<port>`` respectively ``socket://<host>:<port>`` as |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 193 | *port* argument when invoking. |
| 194 | |
| 195 | Command 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 Liechti | dfa2d6c | 2015-12-25 17:25:30 +0100 | [diff] [blame] | 218 | --ask ask again for port when open fails |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 219 | |
| 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 240 | Available 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 | |
| 250 | Miniterm supports some control functions while being connected. |
| 251 | Typing :kbd:`Ctrl+T Ctrl+H` when it is running shows the help text:: |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 252 | |
| 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 Liechti | d2bf42e | 2017-07-18 01:54:40 +0200 | [diff] [blame] | 277 | :kbd:`Ctrl+T s` suspends the connection (port is opened) and reconnects when a |
Chris Liechti | 591c451 | 2017-07-16 22:05:04 +0200 | [diff] [blame] | 278 | key is pressed. This can be used to temporarily access the serial port with an |
Chris Liechti | d2bf42e | 2017-07-18 01:54:40 +0200 | [diff] [blame] | 279 | other application, without exiting miniterm. If reconnecting fails it is |
| 280 | also possible to exit (:kbd:`Ctrl+]`) or change the port (:kbd:`p`). |
Chris Liechti | 591c451 | 2017-07-16 22:05:04 +0200 | [diff] [blame] | 281 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 282 | .. 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 289 | Added ``--ask`` option. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 290 | |