blob: 181aa2cd24b191a347cd562cea9de3741ed4cf40 [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.. _miniterm:
8
cliechti86e87872009-07-21 13:32:45 +00009Miniterm
10========
11This is a console application that provides a small terminal application.
cliechti44eb98f2011-03-21 21:41:10 +000012Miniterm itself does not implement any terminal features such as VT102
cliechti86e87872009-07-21 13:32:45 +000013compatibility. However it inherits these features from the terminal it is run.
14For example on GNU/Linux running from an xterm it will support the escape
15sequences of the xterm. On Windows the typical console window is dumb and does
16not support any escapes. When ANSI.sys is loaded it supports some escapes.
17
cliechti44eb98f2011-03-21 21:41:10 +000018Miniterm::
cliechti5134aab2009-07-21 19:47:59 +000019
20 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
21 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
22
cliechti86e87872009-07-21 13:32:45 +000023Command line options can be given so that binary data including escapes for
24terminals are escaped or output as hex.
25
cliechti44eb98f2011-03-21 21:41:10 +000026Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
cliechti3453f122009-08-10 22:56:34 +000027such as ``rfc2217:://<host>:<port>`` respectively ``socket://<host>:<port>`` as
28*port* argument when invoking.
29
cliechti1fa54072011-03-04 02:28:08 +000030Command line options ``python -m serial.tools.miniterm -h``::
cliechti5134aab2009-07-21 19:47:59 +000031
32 Usage: miniterm.py [options] [port [baudrate]]
33
34 Miniterm - A simple terminal program for the serial port.
35
36 Options:
37 -h, --help show this help message and exit
cliechti5370cee2013-10-13 03:08:19 +000038
39 Port settings:
40 -p PORT, --port=PORT
41 port, a number or a device name. (deprecated option,
42 use parameter instead)
43 -b BAUDRATE, --baud=BAUDRATE
cliechti5134aab2009-07-21 19:47:59 +000044 set baud rate, default 9600
cliechti5370cee2013-10-13 03:08:19 +000045 --parity=PARITY set parity, one of [N, E, O, S, M], default=N
46 --rtscts enable RTS/CTS flow control (default off)
47 --xonxoff enable software flow control (default off)
48 --rts=RTS_STATE set initial RTS line state (possible values: 0, 1)
49 --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1)
50
51 Data handling:
52 -e, --echo enable local echo (default off)
53 --cr do not send CR+LF, send CR only
54 --lf do not send CR+LF, send LF only
55 -D, --debug debug received data (escape non-printable chars)
cliechti5134aab2009-07-21 19:47:59 +000056 --debug can be given multiple times: 0: just print
57 what is received 1: escape non-printable characters,
58 do newlines as unusual 2: escape non-printable
59 characters, newlines too 3: hex dump everything
cliechti5370cee2013-10-13 03:08:19 +000060
61 Hotkeys:
62 --exit-char=EXIT_CHAR
cliechti5134aab2009-07-21 19:47:59 +000063 ASCII code of special character that is used to exit
64 the application
cliechti5370cee2013-10-13 03:08:19 +000065 --menu-char=MENU_CHAR
cliechti5134aab2009-07-21 19:47:59 +000066 ASCII code of special character that is used to
67 control miniterm (menu)
68
cliechti5370cee2013-10-13 03:08:19 +000069 Diagnostics:
70 -q, --quiet suppress non-error messages
71
cliechti5134aab2009-07-21 19:47:59 +000072
cliechti44eb98f2011-03-21 21:41:10 +000073Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is
cliechti5134aab2009-07-21 19:47:59 +000074running shows the help text::
75
76 --- pySerial - miniterm - help
77 ---
78 --- Ctrl+] Exit program
79 --- Ctrl+T Menu escape key, followed by:
80 --- Menu keys:
cliechti44eb98f2011-03-21 21:41:10 +000081 --- Ctrl+T Send the menu character itself to remote
82 --- Ctrl+] Send the exit character to remote
83 --- Ctrl+I Show info
84 --- Ctrl+U Upload file (prompt will be shown)
cliechti5134aab2009-07-21 19:47:59 +000085 --- Toggles:
86 --- Ctrl+R RTS Ctrl+E local echo
87 --- Ctrl+D DTR Ctrl+B BREAK
88 --- Ctrl+L line feed Ctrl+A Cycle repr mode
89 ---
90 --- Port settings (Ctrl+T followed by the following):
cliechti4a63d6b2011-03-19 02:57:51 +000091 --- p change port
cliechti5134aab2009-07-21 19:47:59 +000092 --- 7 8 set data bits
93 --- n e o s m change parity (None, Even, Odd, Space, Mark)
94 --- 1 2 3 set stop bits (1, 2, 1.5)
95 --- b change baud rate
96 --- x X disable/enable software flow control
97 --- r R disable/enable hardware flow control
98
cliechti8611bf42009-08-03 00:34:03 +000099.. versionchanged:: 2.5
cliechti4a63d6b2011-03-19 02:57:51 +0000100 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
101.. versionchanged:: 2.6
102 File moved from the examples to :mod:`serial.tools.miniterm`.
cliechti86e87872009-07-21 13:32:45 +0000103
104miniterm.py_
105 The miniterm program.
106
107setup-miniterm-py2exe.py_
108 This is a py2exe setup script for Windows. It can be used to create a
109 standalone ``miniterm.exe``.
110
cliechti4cb94662013-10-17 03:17:50 +0000111.. _miniterm.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/serial/tools/miniterm.py
112.. _setup-miniterm-py2exe.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/setup-miniterm-py2exe.py
cliechti86e87872009-07-21 13:32:45 +0000113
114
115TCP/IP - serial bridge
116======================
117This program opens a TCP/IP port. When a connection is made to that port (e.g.
118with telnet) it forwards all data to the serial port and vice versa.
119
cliechti3453f122009-08-10 22:56:34 +0000120This example only exports a raw socket connection. The next example
121below gives the client much more control over the remote serial port.
cliechtidec243b2009-08-04 02:24:03 +0000122
123- The serial port settings are set on the command line when starting the
124 program.
125- There is no possibility to change settings from remote.
126- All data is passed through as-is.
127
cliechti5134aab2009-07-21 19:47:59 +0000128::
129
130 Usage: tcp_serial_redirect.py [options] [port [baudrate]]
131
132 Simple Serial to Network (TCP/IP) redirector.
133
134 Options:
135 -h, --help show this help message and exit
136 -q, --quiet suppress non error messages
137 --spy peek at the communication and print all data to the
138 console
139
140 Serial Port:
141 Serial port settings
142
143 -p PORT, --port=PORT
144 port, a number (default 0) or a device name
145 -b BAUDRATE, --baud=BAUDRATE
146 set baud rate, default: 9600
147 --parity=PARITY set parity, one of [N, E, O], default=N
148 --rtscts enable RTS/CTS flow control (default off)
149 --xonxoff enable software flow control (default off)
150 --rts=RTS_STATE set initial RTS line state (possible values: 0, 1)
151 --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1)
152
153 Network settings:
154 Network configuration.
155
156 -P LOCAL_PORT, --localport=LOCAL_PORT
157 local TCP port
cliechtidec243b2009-08-04 02:24:03 +0000158 --rfc2217 allow control commands with Telnet extension RFC-2217
cliechti5134aab2009-07-21 19:47:59 +0000159
160 Newline Settings:
161 Convert newlines between network and serial port. Conversion is
162 normally disabled and can be enabled by --convert.
163
164 -c, --convert enable newline conversion (default off)
165 --net-nl=NET_NEWLINE
166 type of newlines that are expected on the network
167 (default: LF)
168 --ser-nl=SER_NEWLINE
169 type of newlines that are expected on the serial port
170 (default: CR+LF)
171
172 NOTE: no security measures are implemented. Anyone can remotely connect to
173 this service over the network. Only one connection at once is supported. When
174 the connection is terminated it waits for the next connect.
175
cliechti86e87872009-07-21 13:32:45 +0000176
177tcp_serial_redirect.py_
178 Main program.
179
cliechti4cb94662013-10-17 03:17:50 +0000180.. _tcp_serial_redirect.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/tcp_serial_redirect.py
181
cliechti86e87872009-07-21 13:32:45 +0000182
cliechtia2d8f962009-08-10 23:59:27 +0000183Single-port TCP/IP - serial bridge (RFC 2217)
cliechti3453f122009-08-10 22:56:34 +0000184=============================================
185Simple cross platform :rfc:`2217` serial port server. It uses threads and is
186portable (runs on POSIX, Windows, etc).
cliechti86e87872009-07-21 13:32:45 +0000187
cliechtia2d8f962009-08-10 23:59:27 +0000188- The port settings and control lines (RTS/DTR) can be changed at any time
189 using :rfc:`2217` requests. The status lines (DSR/CTS/RI/CD) are polled every
cliechti3453f122009-08-10 22:56:34 +0000190 second and notifications are sent to the client.
191- Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed
192 by an other value is interpreted as Telnet command sequence.
193- Telnet negotiation commands are sent when connecting to the server.
194- RTS/DTR are activated on client connect and deactivated on disconnect.
195- Default port settings are set again when client disconnects.
cliechtia2d8f962009-08-10 23:59:27 +0000196
cliechti3453f122009-08-10 22:56:34 +0000197::
198
199 Usage: rfc2217_server.py [options] port
200
201 RFC 2217 Serial to Network (TCP/IP) redirector.
202
203 Options:
204 -h, --help show this help message and exit
205 -p LOCAL_PORT, --localport=LOCAL_PORT
206 local TCP port
207
208 NOTE: no security measures are implemented. Anyone can remotely connect to
209 this service over the network. Only one connection at once is supported. When
210 the connection is terminated it waits for the next connect.
211
212.. versionadded:: 2.5
213
214rfc2217_server.py_
215 Main program.
216
217setup-rfc2217_server-py2exe.py_
218 This is a py2exe setup script for Windows. It can be used to create a
219 standalone ``rfc2217_server.exe``.
220
cliechti4cb94662013-10-17 03:17:50 +0000221.. _rfc2217_server.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/rfc2217_server.py
222.. _setup-rfc2217_server-py2exe.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/setup-rfc2217_server-py2exe.py
cliechti3453f122009-08-10 22:56:34 +0000223
224
225Multi-port TCP/IP - serial bridge (RFC 2217)
226============================================
cliechtibb5c3c52009-07-23 02:29:27 +0000227This example implements a TCP/IP to serial port service that works with
cliechti3453f122009-08-10 22:56:34 +0000228multiple ports at once. It uses select, no threads, for the serial ports and
229the network sockets and therefore runs on POSIX systems only.
cliechtibb5c3c52009-07-23 02:29:27 +0000230
cliechti7aed8332009-08-05 14:19:31 +0000231- Full control over the serial port with :rfc:`2217`.
cliechti3453f122009-08-10 22:56:34 +0000232- Check existence of ``/tty/USB0...8``. This is done every 5 seconds using
cliechti7aed8332009-08-05 14:19:31 +0000233 ``os.path.exists``.
234- Send zeroconf announcements when port appears or disappears (uses
235 python-avahi and dbus). Service name: ``_serial_port._tcp``.
236- Each serial port becomes available as one TCP/IP server. e.g.
237 ``/dev/ttyUSB0`` is reachable at ``<host>:7000``.
238- Single process for all ports and sockets (not per port).
239- The script can be started as daemon.
240- Logging to stdout or when run as daemon to syslog.
cliechti3453f122009-08-10 22:56:34 +0000241- Default port settings are set again when client disconnects.
cliechti7cb78e82009-08-05 15:47:57 +0000242- modem status lines (CTS/DSR/RI/CD) are not polled periodically and the server
243 therefore does not send NOTIFY_MODEMSTATE on its own. However it responds to
244 request from the client (i.e. use the ``poll_modem`` option in the URL when
245 using a pySerial client.)
cliechtibb5c3c52009-07-23 02:29:27 +0000246
cliechti7aed8332009-08-05 14:19:31 +0000247Requirements:
248
249- Python (>= 2.4)
cliechtibb5c3c52009-07-23 02:29:27 +0000250- python-avahi
251- python-dbus
cliechti7aed8332009-08-05 14:19:31 +0000252- python-serial (>= 2.5)
cliechtibb5c3c52009-07-23 02:29:27 +0000253
cliechti7aed8332009-08-05 14:19:31 +0000254Installation as daemon:
255
cliechtibb5c3c52009-07-23 02:29:27 +0000256- Copy the script ``port_publisher.py`` to ``/usr/local/bin``.
257- Copy the script ``port_publisher.sh`` to ``/etc/init.d``.
258- Add links to the runlevels using ``update-rc.d port_publisher.sh defaults 99``
259- Thats it :-) the service will be started on next reboot. Alternatively run
260 ``invoke-rc.d port_publisher.sh start`` as root.
261
cliechti7aed8332009-08-05 14:19:31 +0000262.. versionadded:: 2.5 new example
cliechtibb5c3c52009-07-23 02:29:27 +0000263
264port_publisher.py_
cliechti7aed8332009-08-05 14:19:31 +0000265 Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
cliechtibb5c3c52009-07-23 02:29:27 +0000266
267port_publisher.sh_
268 Example init.d script.
269
cliechti4cb94662013-10-17 03:17:50 +0000270.. _port_publisher.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/port_publisher.py
271.. _port_publisher.sh: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/port_publisher.sh
cliechtibb5c3c52009-07-23 02:29:27 +0000272
273
cliechti86e87872009-07-21 13:32:45 +0000274wxPython examples
275=================
276A simple terminal application for wxPython and a flexible serial port
277configuration dialog are shown here.
278
279wxTerminal.py_
280 A simple terminal application. Note that the length of the buffer is
281 limited by wx and it may suddenly stop displaying new input.
282
283wxTerminal.wxg_
284 A wxGlade design file for the terminal application.
285
286wxSerialConfigDialog.py_
287 A flexible serial port configuration dialog.
288
289wxSerialConfigDialog.wxg_
290 The wxGlade design file for the configuration dialog.
291
cliechti3453f122009-08-10 22:56:34 +0000292setup-wxTerminal-py2exe.py_
cliechti86e87872009-07-21 13:32:45 +0000293 A py2exe setup script to package the terminal application.
294
cliechti4cb94662013-10-17 03:17:50 +0000295.. _wxTerminal.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/wxTerminal.py
296.. _wxTerminal.wxg: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/wxTerminal.wxg
297.. _wxSerialConfigDialog.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/wxSerialConfigDialog.py
298.. _wxSerialConfigDialog.wxg: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/wxSerialConfigDialog.wxg
299.. _setup-wxTerminal-py2exe.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/setup-wxTerminal-py2exe.py
cliechti86e87872009-07-21 13:32:45 +0000300
301
302Wrapper class
303=============
304This example provides a subclass based on ``Serial`` that has an alternative
305implementation of ``readline()``
306
307enhancedserial.py_
308 A class with alternative ``readline()`` implementation.
309
cliechti4cb94662013-10-17 03:17:50 +0000310.. _enhancedserial.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/enhancedserial.py
cliechti86e87872009-07-21 13:32:45 +0000311
312
cliechti86e87872009-07-21 13:32:45 +0000313Unit tests
314==========
315The project uses a number of unit test to verify the functionality. They all
cliechti88c46842009-08-10 22:29:54 +0000316need a loop back connector. The scripts itself contain more information. All
317test scripts are contained in the directory ``test``.
cliechti86e87872009-07-21 13:32:45 +0000318
cliechti8611bf42009-08-03 00:34:03 +0000319The unit tests are performed on port ``0`` unless a different device name or
cliechti88c46842009-08-10 22:29:54 +0000320``rfc2217://`` URL is given on the command line (argv[1]).
321
322run_all_tests.py_
323 Collect all tests from all ``test*`` files and run them. By default, the
324 ``loop://`` device is used.
cliechti8611bf42009-08-03 00:34:03 +0000325
cliechti86e87872009-07-21 13:32:45 +0000326test.py_
cliechti88c46842009-08-10 22:29:54 +0000327 Basic tests (binary capabilities, timeout, control lines).
cliechti86e87872009-07-21 13:32:45 +0000328
cliechti86e87872009-07-21 13:32:45 +0000329test_advanced.py_
cliechti88c46842009-08-10 22:29:54 +0000330 Test more advanced features (properties).
cliechti86e87872009-07-21 13:32:45 +0000331
cliechtibb5c3c52009-07-23 02:29:27 +0000332test_high_load.py_
333 Tests involving sending a lot of data.
334
cliechti25375b52010-07-21 23:27:13 +0000335test_readline.py_
336 Tests involving readline.
337
cliechtidaf47ba2009-07-28 01:28:16 +0000338test_iolib.py_
339 Tests involving the :mod:`io` library. Only available for Python 2.6 and
340 newer.
341
cliechti4a63d6b2011-03-19 02:57:51 +0000342test_url.py_
343 Tests involving the :ref:`URL <URLs>` feature.
344
cliechti4cb94662013-10-17 03:17:50 +0000345.. _run_all_tests.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/run_all_tests.py
346.. _test.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test.py
347.. _test_advanced.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test_advanced.py
348.. _test_high_load.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test_high_load.py
349.. _test_readline.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test_readline.py
350.. _test_iolib.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test_iolib.py
351.. _test_url.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/test/test_url.py