cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 1 | .. _examples: |
| 2 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 3 | ========== |
| 4 | Examples |
| 5 | ========== |
| 6 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 7 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 8 | Miniterm |
| 9 | ======== |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame^] | 10 | Miniterm is now available as module instead of example. |
| 11 | see :ref:`miniterm` for details. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 12 | |
| 13 | miniterm.py_ |
| 14 | The miniterm program. |
| 15 | |
| 16 | setup-miniterm-py2exe.py_ |
| 17 | This is a py2exe setup script for Windows. It can be used to create a |
| 18 | standalone ``miniterm.exe``. |
| 19 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 20 | .. _miniterm.py: https://github.com/pyserial/pyserial/blob/master/serial/tools/miniterm.py |
| 21 | .. _setup-miniterm-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-miniterm-py2exe.py |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | TCP/IP - serial bridge |
| 25 | ====================== |
| 26 | This program opens a TCP/IP port. When a connection is made to that port (e.g. |
| 27 | with telnet) it forwards all data to the serial port and vice versa. |
| 28 | |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 29 | This example only exports a raw socket connection. The next example |
| 30 | below gives the client much more control over the remote serial port. |
cliechti | dec243b | 2009-08-04 02:24:03 +0000 | [diff] [blame] | 31 | |
| 32 | - The serial port settings are set on the command line when starting the |
| 33 | program. |
| 34 | - There is no possibility to change settings from remote. |
| 35 | - All data is passed through as-is. |
| 36 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 37 | :: |
| 38 | |
| 39 | Usage: tcp_serial_redirect.py [options] [port [baudrate]] |
| 40 | |
| 41 | Simple Serial to Network (TCP/IP) redirector. |
| 42 | |
| 43 | Options: |
| 44 | -h, --help show this help message and exit |
| 45 | -q, --quiet suppress non error messages |
| 46 | --spy peek at the communication and print all data to the |
| 47 | console |
| 48 | |
| 49 | Serial Port: |
| 50 | Serial port settings |
| 51 | |
| 52 | -p PORT, --port=PORT |
| 53 | port, a number (default 0) or a device name |
| 54 | -b BAUDRATE, --baud=BAUDRATE |
| 55 | set baud rate, default: 9600 |
| 56 | --parity=PARITY set parity, one of [N, E, O], default=N |
| 57 | --rtscts enable RTS/CTS flow control (default off) |
| 58 | --xonxoff enable software flow control (default off) |
| 59 | --rts=RTS_STATE set initial RTS line state (possible values: 0, 1) |
| 60 | --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1) |
| 61 | |
| 62 | Network settings: |
| 63 | Network configuration. |
| 64 | |
| 65 | -P LOCAL_PORT, --localport=LOCAL_PORT |
| 66 | local TCP port |
cliechti | dec243b | 2009-08-04 02:24:03 +0000 | [diff] [blame] | 67 | --rfc2217 allow control commands with Telnet extension RFC-2217 |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 68 | |
| 69 | Newline Settings: |
| 70 | Convert newlines between network and serial port. Conversion is |
| 71 | normally disabled and can be enabled by --convert. |
| 72 | |
| 73 | -c, --convert enable newline conversion (default off) |
| 74 | --net-nl=NET_NEWLINE |
| 75 | type of newlines that are expected on the network |
| 76 | (default: LF) |
| 77 | --ser-nl=SER_NEWLINE |
| 78 | type of newlines that are expected on the serial port |
| 79 | (default: CR+LF) |
| 80 | |
| 81 | NOTE: no security measures are implemented. Anyone can remotely connect to |
| 82 | this service over the network. Only one connection at once is supported. When |
| 83 | the connection is terminated it waits for the next connect. |
| 84 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 85 | |
| 86 | tcp_serial_redirect.py_ |
| 87 | Main program. |
| 88 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 89 | .. _tcp_serial_redirect.py: https://github.com/pyserial/pyserial/blob/master/examples/tcp_serial_redirect.py |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 90 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 91 | |
cliechti | a2d8f96 | 2009-08-10 23:59:27 +0000 | [diff] [blame] | 92 | Single-port TCP/IP - serial bridge (RFC 2217) |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 93 | ============================================= |
| 94 | Simple cross platform :rfc:`2217` serial port server. It uses threads and is |
| 95 | portable (runs on POSIX, Windows, etc). |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 96 | |
cliechti | a2d8f96 | 2009-08-10 23:59:27 +0000 | [diff] [blame] | 97 | - The port settings and control lines (RTS/DTR) can be changed at any time |
| 98 | using :rfc:`2217` requests. The status lines (DSR/CTS/RI/CD) are polled every |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 99 | second and notifications are sent to the client. |
| 100 | - Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed |
| 101 | by an other value is interpreted as Telnet command sequence. |
| 102 | - Telnet negotiation commands are sent when connecting to the server. |
| 103 | - RTS/DTR are activated on client connect and deactivated on disconnect. |
| 104 | - Default port settings are set again when client disconnects. |
cliechti | a2d8f96 | 2009-08-10 23:59:27 +0000 | [diff] [blame] | 105 | |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 106 | :: |
| 107 | |
| 108 | Usage: rfc2217_server.py [options] port |
| 109 | |
| 110 | RFC 2217 Serial to Network (TCP/IP) redirector. |
| 111 | |
| 112 | Options: |
| 113 | -h, --help show this help message and exit |
| 114 | -p LOCAL_PORT, --localport=LOCAL_PORT |
| 115 | local TCP port |
| 116 | |
| 117 | NOTE: no security measures are implemented. Anyone can remotely connect to |
| 118 | this service over the network. Only one connection at once is supported. When |
| 119 | the connection is terminated it waits for the next connect. |
| 120 | |
| 121 | .. versionadded:: 2.5 |
| 122 | |
| 123 | rfc2217_server.py_ |
| 124 | Main program. |
| 125 | |
| 126 | setup-rfc2217_server-py2exe.py_ |
| 127 | This is a py2exe setup script for Windows. It can be used to create a |
| 128 | standalone ``rfc2217_server.exe``. |
| 129 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 130 | .. _rfc2217_server.py: https://github.com/pyserial/pyserial/blob/master/examples/rfc2217_server.py |
| 131 | .. _setup-rfc2217_server-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-rfc2217_server-py2exe.py |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 132 | |
| 133 | |
| 134 | Multi-port TCP/IP - serial bridge (RFC 2217) |
| 135 | ============================================ |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 136 | This example implements a TCP/IP to serial port service that works with |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 137 | multiple ports at once. It uses select, no threads, for the serial ports and |
| 138 | the network sockets and therefore runs on POSIX systems only. |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 139 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 140 | - Full control over the serial port with :rfc:`2217`. |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 141 | - Check existence of ``/tty/USB0...8``. This is done every 5 seconds using |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 142 | ``os.path.exists``. |
| 143 | - Send zeroconf announcements when port appears or disappears (uses |
| 144 | python-avahi and dbus). Service name: ``_serial_port._tcp``. |
| 145 | - Each serial port becomes available as one TCP/IP server. e.g. |
| 146 | ``/dev/ttyUSB0`` is reachable at ``<host>:7000``. |
| 147 | - Single process for all ports and sockets (not per port). |
| 148 | - The script can be started as daemon. |
| 149 | - Logging to stdout or when run as daemon to syslog. |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 150 | - Default port settings are set again when client disconnects. |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 151 | - modem status lines (CTS/DSR/RI/CD) are not polled periodically and the server |
| 152 | therefore does not send NOTIFY_MODEMSTATE on its own. However it responds to |
| 153 | request from the client (i.e. use the ``poll_modem`` option in the URL when |
| 154 | using a pySerial client.) |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 155 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 156 | Requirements: |
| 157 | |
| 158 | - Python (>= 2.4) |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 159 | - python-avahi |
| 160 | - python-dbus |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 161 | - python-serial (>= 2.5) |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 162 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 163 | Installation as daemon: |
| 164 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 165 | - Copy the script ``port_publisher.py`` to ``/usr/local/bin``. |
| 166 | - Copy the script ``port_publisher.sh`` to ``/etc/init.d``. |
| 167 | - Add links to the runlevels using ``update-rc.d port_publisher.sh defaults 99`` |
| 168 | - Thats it :-) the service will be started on next reboot. Alternatively run |
| 169 | ``invoke-rc.d port_publisher.sh start`` as root. |
| 170 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 171 | .. versionadded:: 2.5 new example |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 172 | |
| 173 | port_publisher.py_ |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 174 | Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments. |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 175 | |
| 176 | port_publisher.sh_ |
| 177 | Example init.d script. |
| 178 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 179 | .. _port_publisher.py: https://github.com/pyserial/pyserial/blob/master/examples/port_publisher.py |
| 180 | .. _port_publisher.sh: https://github.com/pyserial/pyserial/blob/master/examples/http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/port_publisher.sh |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 181 | |
| 182 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 183 | wxPython examples |
| 184 | ================= |
| 185 | A simple terminal application for wxPython and a flexible serial port |
| 186 | configuration dialog are shown here. |
| 187 | |
| 188 | wxTerminal.py_ |
| 189 | A simple terminal application. Note that the length of the buffer is |
| 190 | limited by wx and it may suddenly stop displaying new input. |
| 191 | |
| 192 | wxTerminal.wxg_ |
| 193 | A wxGlade design file for the terminal application. |
| 194 | |
| 195 | wxSerialConfigDialog.py_ |
| 196 | A flexible serial port configuration dialog. |
| 197 | |
| 198 | wxSerialConfigDialog.wxg_ |
| 199 | The wxGlade design file for the configuration dialog. |
| 200 | |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 201 | setup-wxTerminal-py2exe.py_ |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 202 | A py2exe setup script to package the terminal application. |
| 203 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 204 | .. _wxTerminal.py: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.py |
| 205 | .. _wxTerminal.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.wxg |
| 206 | .. _wxSerialConfigDialog.py: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.py |
| 207 | .. _wxSerialConfigDialog.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.wxg |
| 208 | .. _setup-wxTerminal-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-wxTerminal-py2exe.py |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 209 | |
| 210 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 211 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 212 | Unit tests |
| 213 | ========== |
| 214 | The project uses a number of unit test to verify the functionality. They all |
cliechti | 88c4684 | 2009-08-10 22:29:54 +0000 | [diff] [blame] | 215 | need a loop back connector. The scripts itself contain more information. All |
| 216 | test scripts are contained in the directory ``test``. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 217 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 218 | The unit tests are performed on port ``0`` unless a different device name or |
cliechti | 88c4684 | 2009-08-10 22:29:54 +0000 | [diff] [blame] | 219 | ``rfc2217://`` URL is given on the command line (argv[1]). |
| 220 | |
| 221 | run_all_tests.py_ |
| 222 | Collect all tests from all ``test*`` files and run them. By default, the |
| 223 | ``loop://`` device is used. |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 224 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 225 | test.py_ |
cliechti | 88c4684 | 2009-08-10 22:29:54 +0000 | [diff] [blame] | 226 | Basic tests (binary capabilities, timeout, control lines). |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 227 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 228 | test_advanced.py_ |
cliechti | 88c4684 | 2009-08-10 22:29:54 +0000 | [diff] [blame] | 229 | Test more advanced features (properties). |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 230 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 231 | test_high_load.py_ |
| 232 | Tests involving sending a lot of data. |
| 233 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 234 | test_readline.py_ |
| 235 | Tests involving readline. |
| 236 | |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 237 | test_iolib.py_ |
| 238 | Tests involving the :mod:`io` library. Only available for Python 2.6 and |
| 239 | newer. |
| 240 | |
cliechti | 4a63d6b | 2011-03-19 02:57:51 +0000 | [diff] [blame] | 241 | test_url.py_ |
| 242 | Tests involving the :ref:`URL <URLs>` feature. |
| 243 | |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 244 | .. _run_all_tests.py: https://github.com/pyserial/pyserial/blob/master/test/run_all_tests.py |
| 245 | .. _test.py: https://github.com/pyserial/pyserial/blob/master/test/test.py |
| 246 | .. _test_advanced.py: https://github.com/pyserial/pyserial/blob/master/test/test_advanced.py |
| 247 | .. _test_high_load.py: https://github.com/pyserial/pyserial/blob/master/test/test_high_load.py |
| 248 | .. _test_readline.py: https://github.com/pyserial/pyserial/blob/master/test/test_readline.py |
| 249 | .. _test_iolib.py: https://github.com/pyserial/pyserial/blob/master/test/test_iolib.py |
| 250 | .. _test_url.py: https://github.com/pyserial/pyserial/blob/master/test/test_url.py |