blob: 019ffc1576ac2cf9a59e1844d8523de3fc3ddaae [file] [log] [blame]
cliechti86844e82009-08-12 00:05:33 +00001.. _examples:
2
cliechti86e87872009-07-21 13:32:45 +00003==========
4 Examples
5==========
6
cliechti86844e82009-08-12 00:05:33 +00007
cliechti86e87872009-07-21 13:32:45 +00008Miniterm
9========
Chris Liechti589c92a2015-09-04 23:04:34 +020010Miniterm is now available as module instead of example.
11see :ref:`miniterm` for details.
cliechti86e87872009-07-21 13:32:45 +000012
13miniterm.py_
14 The miniterm program.
15
16setup-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 Liechti90570b92015-08-04 03:32:02 +020020.. _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
cliechti86e87872009-07-21 13:32:45 +000022
23
24TCP/IP - serial bridge
25======================
26This program opens a TCP/IP port. When a connection is made to that port (e.g.
27with telnet) it forwards all data to the serial port and vice versa.
28
cliechti3453f122009-08-10 22:56:34 +000029This example only exports a raw socket connection. The next example
30below gives the client much more control over the remote serial port.
cliechtidec243b2009-08-04 02:24:03 +000031
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
cliechti5134aab2009-07-21 19:47:59 +000037::
38
Chris Liechti7f593022015-09-07 21:04:39 +020039 usage: tcp_serial_redirect.py [-h] [-q] [--parity {N,E,O,S,M}] [--rtscts]
40 [--xonxoff] [--rts RTS] [--dtr DTR]
41 [-P LOCALPORT]
42 SERIALPORT [BAUDRATE]
cliechti5134aab2009-07-21 19:47:59 +000043
44 Simple Serial to Network (TCP/IP) redirector.
45
Chris Liechti7f593022015-09-07 21:04:39 +020046 positional arguments:
47 SERIALPORT serial port name
48 BAUDRATE set baud rate, default: 9600
49
50 optional arguments:
cliechti5134aab2009-07-21 19:47:59 +000051 -h, --help show this help message and exit
52 -q, --quiet suppress non error messages
cliechti5134aab2009-07-21 19:47:59 +000053
Chris Liechti7f593022015-09-07 21:04:39 +020054 serial port:
55 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
56 --rtscts enable RTS/CTS flow control (default off)
57 --xonxoff enable software flow control (default off)
58 --rts RTS set initial RTS line state (possible values: 0, 1)
59 --dtr DTR set initial DTR line state (possible values: 0, 1)
cliechti5134aab2009-07-21 19:47:59 +000060
Chris Liechti7f593022015-09-07 21:04:39 +020061 network settings:
62 -P LOCALPORT, --localport LOCALPORT
cliechti5134aab2009-07-21 19:47:59 +000063 local TCP port
cliechti5134aab2009-07-21 19:47:59 +000064
65 NOTE: no security measures are implemented. Anyone can remotely connect to
Chris Liechti7f593022015-09-07 21:04:39 +020066 this service over the network. Only one connection at once is supported. When
cliechti5134aab2009-07-21 19:47:59 +000067 the connection is terminated it waits for the next connect.
68
cliechti86e87872009-07-21 13:32:45 +000069
70tcp_serial_redirect.py_
71 Main program.
72
Chris Liechti90570b92015-08-04 03:32:02 +020073.. _tcp_serial_redirect.py: https://github.com/pyserial/pyserial/blob/master/examples/tcp_serial_redirect.py
cliechti4cb94662013-10-17 03:17:50 +000074
cliechti86e87872009-07-21 13:32:45 +000075
cliechtia2d8f962009-08-10 23:59:27 +000076Single-port TCP/IP - serial bridge (RFC 2217)
cliechti3453f122009-08-10 22:56:34 +000077=============================================
78Simple cross platform :rfc:`2217` serial port server. It uses threads and is
79portable (runs on POSIX, Windows, etc).
cliechti86e87872009-07-21 13:32:45 +000080
cliechtia2d8f962009-08-10 23:59:27 +000081- The port settings and control lines (RTS/DTR) can be changed at any time
82 using :rfc:`2217` requests. The status lines (DSR/CTS/RI/CD) are polled every
cliechti3453f122009-08-10 22:56:34 +000083 second and notifications are sent to the client.
84- Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed
85 by an other value is interpreted as Telnet command sequence.
86- Telnet negotiation commands are sent when connecting to the server.
87- RTS/DTR are activated on client connect and deactivated on disconnect.
88- Default port settings are set again when client disconnects.
cliechtia2d8f962009-08-10 23:59:27 +000089
cliechti3453f122009-08-10 22:56:34 +000090::
91
Chris Liechti7f593022015-09-07 21:04:39 +020092 usage: rfc2217_server.py [-h] [-p TCPPORT] [-v] SERIALPORT
cliechti3453f122009-08-10 22:56:34 +000093
94 RFC 2217 Serial to Network (TCP/IP) redirector.
95
Chris Liechti7f593022015-09-07 21:04:39 +020096 positional arguments:
97 SERIALPORT
98
99 optional arguments:
cliechti3453f122009-08-10 22:56:34 +0000100 -h, --help show this help message and exit
Chris Liechti7f593022015-09-07 21:04:39 +0200101 -p TCPPORT, --localport TCPPORT
102 local TCP port, default: 2217
103 -v, --verbose print more diagnostic messages (option can be given
104 multiple times)
cliechti3453f122009-08-10 22:56:34 +0000105
106 NOTE: no security measures are implemented. Anyone can remotely connect to
Chris Liechti7f593022015-09-07 21:04:39 +0200107 this service over the network. Only one connection at once is supported. When
cliechti3453f122009-08-10 22:56:34 +0000108 the connection is terminated it waits for the next connect.
109
110.. versionadded:: 2.5
111
112rfc2217_server.py_
113 Main program.
114
115setup-rfc2217_server-py2exe.py_
116 This is a py2exe setup script for Windows. It can be used to create a
117 standalone ``rfc2217_server.exe``.
118
Chris Liechti90570b92015-08-04 03:32:02 +0200119.. _rfc2217_server.py: https://github.com/pyserial/pyserial/blob/master/examples/rfc2217_server.py
120.. _setup-rfc2217_server-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-rfc2217_server-py2exe.py
cliechti3453f122009-08-10 22:56:34 +0000121
122
123Multi-port TCP/IP - serial bridge (RFC 2217)
124============================================
cliechtibb5c3c52009-07-23 02:29:27 +0000125This example implements a TCP/IP to serial port service that works with
cliechti3453f122009-08-10 22:56:34 +0000126multiple ports at once. It uses select, no threads, for the serial ports and
127the network sockets and therefore runs on POSIX systems only.
cliechtibb5c3c52009-07-23 02:29:27 +0000128
cliechti7aed8332009-08-05 14:19:31 +0000129- Full control over the serial port with :rfc:`2217`.
cliechti3453f122009-08-10 22:56:34 +0000130- Check existence of ``/tty/USB0...8``. This is done every 5 seconds using
cliechti7aed8332009-08-05 14:19:31 +0000131 ``os.path.exists``.
132- Send zeroconf announcements when port appears or disappears (uses
133 python-avahi and dbus). Service name: ``_serial_port._tcp``.
134- Each serial port becomes available as one TCP/IP server. e.g.
135 ``/dev/ttyUSB0`` is reachable at ``<host>:7000``.
136- Single process for all ports and sockets (not per port).
137- The script can be started as daemon.
138- Logging to stdout or when run as daemon to syslog.
cliechti3453f122009-08-10 22:56:34 +0000139- Default port settings are set again when client disconnects.
cliechti7cb78e82009-08-05 15:47:57 +0000140- modem status lines (CTS/DSR/RI/CD) are not polled periodically and the server
141 therefore does not send NOTIFY_MODEMSTATE on its own. However it responds to
142 request from the client (i.e. use the ``poll_modem`` option in the URL when
143 using a pySerial client.)
cliechtibb5c3c52009-07-23 02:29:27 +0000144
Chris Liechtia4cde702015-09-08 01:11:52 +0200145::
146
147 usage: port_publisher.py [options]
148
149 Announce the existence of devices using zeroconf and provide
150 a TCP/IP <-> serial port gateway (implements RFC 2217).
151
152 If running as daemon, write to syslog. Otherwise write to stdout.
153
154 optional arguments:
155 -h, --help show this help message and exit
156
157 serial port settings:
158 --ports-regex REGEX specify a regex to search against the serial devices
159 and their descriptions (default: /dev/ttyUSB[0-9]+)
160
161 network settings:
162 --tcp-port PORT specify lowest TCP port number (default: 7000)
163
164 daemon:
165 -d, --daemon start as daemon
166 --pidfile FILE specify a name for the PID file
167
168 diagnostics:
169 -o FILE, --logfile FILE
170 write messages file instead of stdout
171 -q, --quiet suppress most diagnostic messages
172 -v, --verbose increase diagnostic messages
173
174 NOTE: no security measures are implemented. Anyone can remotely connect to
175 this service over the network. Only one connection at once, per port, is
176 supported. When the connection is terminated, it waits for the next connect.
177
cliechti7aed8332009-08-05 14:19:31 +0000178Requirements:
179
180- Python (>= 2.4)
cliechtibb5c3c52009-07-23 02:29:27 +0000181- python-avahi
182- python-dbus
cliechti7aed8332009-08-05 14:19:31 +0000183- python-serial (>= 2.5)
cliechtibb5c3c52009-07-23 02:29:27 +0000184
cliechti7aed8332009-08-05 14:19:31 +0000185Installation as daemon:
186
cliechtibb5c3c52009-07-23 02:29:27 +0000187- Copy the script ``port_publisher.py`` to ``/usr/local/bin``.
188- Copy the script ``port_publisher.sh`` to ``/etc/init.d``.
189- Add links to the runlevels using ``update-rc.d port_publisher.sh defaults 99``
190- Thats it :-) the service will be started on next reboot. Alternatively run
191 ``invoke-rc.d port_publisher.sh start`` as root.
192
cliechti7aed8332009-08-05 14:19:31 +0000193.. versionadded:: 2.5 new example
cliechtibb5c3c52009-07-23 02:29:27 +0000194
195port_publisher.py_
cliechti7aed8332009-08-05 14:19:31 +0000196 Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
cliechtibb5c3c52009-07-23 02:29:27 +0000197
198port_publisher.sh_
199 Example init.d script.
200
Chris Liechti90570b92015-08-04 03:32:02 +0200201.. _port_publisher.py: https://github.com/pyserial/pyserial/blob/master/examples/port_publisher.py
202.. _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
cliechtibb5c3c52009-07-23 02:29:27 +0000203
204
cliechti86e87872009-07-21 13:32:45 +0000205wxPython examples
206=================
207A simple terminal application for wxPython and a flexible serial port
208configuration dialog are shown here.
209
210wxTerminal.py_
211 A simple terminal application. Note that the length of the buffer is
212 limited by wx and it may suddenly stop displaying new input.
213
214wxTerminal.wxg_
215 A wxGlade design file for the terminal application.
216
217wxSerialConfigDialog.py_
218 A flexible serial port configuration dialog.
219
220wxSerialConfigDialog.wxg_
221 The wxGlade design file for the configuration dialog.
222
cliechti3453f122009-08-10 22:56:34 +0000223setup-wxTerminal-py2exe.py_
cliechti86e87872009-07-21 13:32:45 +0000224 A py2exe setup script to package the terminal application.
225
Chris Liechti90570b92015-08-04 03:32:02 +0200226.. _wxTerminal.py: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.py
227.. _wxTerminal.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.wxg
228.. _wxSerialConfigDialog.py: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.py
229.. _wxSerialConfigDialog.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.wxg
230.. _setup-wxTerminal-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-wxTerminal-py2exe.py
cliechti86e87872009-07-21 13:32:45 +0000231
232
cliechti86e87872009-07-21 13:32:45 +0000233
cliechti86e87872009-07-21 13:32:45 +0000234Unit tests
235==========
236The project uses a number of unit test to verify the functionality. They all
cliechti88c46842009-08-10 22:29:54 +0000237need a loop back connector. The scripts itself contain more information. All
238test scripts are contained in the directory ``test``.
cliechti86e87872009-07-21 13:32:45 +0000239
cliechti8611bf42009-08-03 00:34:03 +0000240The unit tests are performed on port ``0`` unless a different device name or
cliechti88c46842009-08-10 22:29:54 +0000241``rfc2217://`` URL is given on the command line (argv[1]).
242
243run_all_tests.py_
244 Collect all tests from all ``test*`` files and run them. By default, the
245 ``loop://`` device is used.
cliechti8611bf42009-08-03 00:34:03 +0000246
cliechti86e87872009-07-21 13:32:45 +0000247test.py_
cliechti88c46842009-08-10 22:29:54 +0000248 Basic tests (binary capabilities, timeout, control lines).
cliechti86e87872009-07-21 13:32:45 +0000249
cliechti86e87872009-07-21 13:32:45 +0000250test_advanced.py_
cliechti88c46842009-08-10 22:29:54 +0000251 Test more advanced features (properties).
cliechti86e87872009-07-21 13:32:45 +0000252
cliechtibb5c3c52009-07-23 02:29:27 +0000253test_high_load.py_
254 Tests involving sending a lot of data.
255
cliechti25375b52010-07-21 23:27:13 +0000256test_readline.py_
257 Tests involving readline.
258
cliechtidaf47ba2009-07-28 01:28:16 +0000259test_iolib.py_
260 Tests involving the :mod:`io` library. Only available for Python 2.6 and
261 newer.
262
cliechti4a63d6b2011-03-19 02:57:51 +0000263test_url.py_
264 Tests involving the :ref:`URL <URLs>` feature.
265
Chris Liechti90570b92015-08-04 03:32:02 +0200266.. _run_all_tests.py: https://github.com/pyserial/pyserial/blob/master/test/run_all_tests.py
267.. _test.py: https://github.com/pyserial/pyserial/blob/master/test/test.py
268.. _test_advanced.py: https://github.com/pyserial/pyserial/blob/master/test/test_advanced.py
269.. _test_high_load.py: https://github.com/pyserial/pyserial/blob/master/test/test_high_load.py
270.. _test_readline.py: https://github.com/pyserial/pyserial/blob/master/test/test_readline.py
271.. _test_iolib.py: https://github.com/pyserial/pyserial/blob/master/test/test_iolib.py
272.. _test_url.py: https://github.com/pyserial/pyserial/blob/master/test/test_url.py