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 | |
| 15 | .. function:: comports() |
| 16 | |
Chris Liechti | 06ae1dc | 2016-05-05 23:54:35 +0200 | [diff] [blame] | 17 | :return: a list containing :class:`ListPortInfo` objects. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 18 | |
Chris Liechti | 06ae1dc | 2016-05-05 23:54:35 +0200 | [diff] [blame] | 19 | The function returns a list of :obj:`ListPortInfo` objects. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 20 | |
| 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 30 | :platform: Linux (/dev files, sysfs) |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 31 | :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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 38 | :return: an iterable that yields :class:`ListPortInfo` objects, see also |
| 39 | :func:`comports`. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 40 | |
| 41 | Search for ports using a regular expression. Port name, description and |
| 42 | hardware ID are searched (case insensitive). The function returns an |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 43 | iterable that contains the same data that :func:`comports` generates, but |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 44 | includes only those entries that match the regexp. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 45 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 46 | |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 47 | .. class:: ListPortInfo |
| 48 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 49 | This object holds information about a serial port. It supports indexed |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 50 | 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 71 | 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] | 72 | platform does not support extended info). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 73 | |
| 74 | .. attribute:: vid |
| 75 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 76 | USB Vendor ID (integer, 0...65535). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 77 | |
| 78 | .. attribute:: pid |
| 79 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 80 | USB product ID (integer, 0...65535). |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 81 | |
| 82 | .. attribute:: serial_number |
| 83 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 84 | USB serial number as a string. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 85 | |
| 86 | .. attribute:: location |
| 87 | |
| 88 | USB device location string ("<bus>-<port>[-<port>]...") |
| 89 | |
| 90 | .. attribute:: manufacturer |
| 91 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 92 | USB manufacturer string, as reported by device. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 93 | |
| 94 | .. attribute:: product |
| 95 | |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 96 | USB product string, as reported by device. |
Chris Liechti | 3ee3eff | 2015-11-13 02:01:01 +0100 | [diff] [blame] | 97 | |
| 98 | .. attribute:: interface |
| 99 | |
| 100 | Interface specifc description, e.g. used in compound USB devices. |
| 101 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 102 | Comparison operators are implemented such that the :obj:`ListPortInfo` objects |
Chris Liechti | 73034f6 | 2015-11-20 23:16:39 +0100 | [diff] [blame] | 103 | 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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 106 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 107 | |
| 108 | **Command line usage** |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 109 | |
| 110 | Help 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 | |
| 125 | Examples: |
| 126 | |
| 127 | - List all ports with details:: |
| 128 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 129 | $ 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 Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 138 | |
| 139 | - List the 2nd port matching a USB VID:PID pattern:: |
| 140 | |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 141 | $ python -m serial.tools.list_ports 1234:5678 -q -n 2 |
| 142 | /dev/ttyUSB1 |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 143 | |
| 144 | .. versionadded:: 2.6 |
Chris Liechti | d32faae | 2015-12-21 23:40:43 +0100 | [diff] [blame] | 145 | .. versionchanged:: 3.0 returning ``ListPortInfo`` objects instead of a tuple |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 146 | |
| 147 | |
| 148 | .. _miniterm: |
| 149 | |
| 150 | serial.tools.miniterm |
| 151 | ===================== |
| 152 | .. module:: serial.tools.miniterm |
| 153 | |
| 154 | This is a console application that provides a small terminal application. |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 155 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 156 | Miniterm itself does not implement any terminal features such as VT102 |
| 157 | compatibility. However it may inherit these features from the terminal it is run. |
| 158 | For example on GNU/Linux running from an xterm it will support the escape |
| 159 | sequences of the xterm. On Windows the typical console window is dumb and does |
| 160 | not support any escapes. When ANSI.sys is loaded it supports some escapes. |
| 161 | |
Chris Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 162 | The default is to filter terminal control characters, see ``--filter`` for |
| 163 | different options. |
| 164 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 165 | Miniterm:: |
| 166 | |
| 167 | --- Miniterm on /dev/ttyS0: 9600,8,N,1 --- |
| 168 | --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- |
| 169 | |
| 170 | Command line options can be given so that binary data including escapes for |
| 171 | terminals are escaped or output as hex. |
| 172 | |
| 173 | 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] | 174 | such as ``rfc2217://<host>:<port>`` respectively ``socket://<host>:<port>`` as |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 175 | *port* argument when invoking. |
| 176 | |
| 177 | Command 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 Liechti | dfa2d6c | 2015-12-25 17:25:30 +0100 | [diff] [blame] | 200 | --ask ask again for port when open fails |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 201 | |
| 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 222 | Available 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 | |
| 232 | Miniterm supports some control functions while being connected. |
| 233 | 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] | 234 | |
| 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 Liechti | 6c693d7 | 2016-01-17 20:23:42 +0100 | [diff] [blame] | 266 | Added ``--ask`` option. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 267 | |