blob: 5c51ef609be47ab012fa1a4ee0a0d3ff7b0fe28d [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
Chris Liechtic8c51832015-08-26 23:58:17 +020013compatibility. However it may inherit these features from the terminal it is run.
cliechti86e87872009-07-21 13:32:45 +000014For 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
Chris Liechtic8c51832015-08-26 23:58:17 +020032 usage: miniterm.py [-h] [--parity {N,E,O,S,M}] [--rtscts] [--xonxoff]
33 [--rts RTS] [--dtr DTR] [-e] [--encoding CODEC] [-f NAME]
34 [--eol {CR,LF,CRLF}] [--raw] [--exit-char NUM]
35 [--menu-char NUM] [-q] [--develop]
36 [port] [baudrate]
cliechti5134aab2009-07-21 19:47:59 +000037
38 Miniterm - A simple terminal program for the serial port.
39
Chris Liechtic8c51832015-08-26 23:58:17 +020040 positional arguments:
41 port serial port name
42 baudrate set baud rate, default: 9600
43
44 optional arguments:
cliechti5134aab2009-07-21 19:47:59 +000045 -h, --help show this help message and exit
cliechti5370cee2013-10-13 03:08:19 +000046
Chris Liechtic8c51832015-08-26 23:58:17 +020047 port settings:
48 --parity {N,E,O,S,M} set parity, one of {N E O S M}, default: N
49 --rtscts enable RTS/CTS flow control (default off)
50 --xonxoff enable software flow control (default off)
51 --rts RTS set initial RTS line state (possible values: 0, 1)
52 --dtr DTR set initial DTR line state (possible values: 0, 1)
cliechti5370cee2013-10-13 03:08:19 +000053
Chris Liechtic8c51832015-08-26 23:58:17 +020054 data handling:
55 -e, --echo enable local echo (default off)
56 --encoding CODEC set the encoding for the serial port (e.g. hexlify,
57 Latin1, UTF-8), default: UTF-8
58 -f NAME, --filter NAME
59 add text transformation
60 --eol {CR,LF,CRLF} end of line mode
61 --raw Do no apply any encodings/transformations
cliechti5370cee2013-10-13 03:08:19 +000062
Chris Liechtic8c51832015-08-26 23:58:17 +020063 hotkeys:
64 --exit-char NUM Unicode of special character that is used to exit the
65 application, default: 29
66 --menu-char NUM Unicode code of special character that is used to
67 control miniterm (menu), default: 20
cliechti5134aab2009-07-21 19:47:59 +000068
Chris Liechtic8c51832015-08-26 23:58:17 +020069 diagnostics:
70 -q, --quiet suppress non-error messages
71 --develop show Python traceback on error
cliechti5370cee2013-10-13 03:08:19 +000072
cliechti5134aab2009-07-21 19:47:59 +000073
cliechti44eb98f2011-03-21 21:41:10 +000074Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is
cliechti5134aab2009-07-21 19:47:59 +000075running shows the help text::
76
Chris Liechtic8c51832015-08-26 23:58:17 +020077 --- pySerial (3.0a) - miniterm - help
cliechti5134aab2009-07-21 19:47:59 +000078 ---
79 --- Ctrl+] Exit program
80 --- Ctrl+T Menu escape key, followed by:
81 --- Menu keys:
Chris Liechtic8c51832015-08-26 23:58:17 +020082 --- Ctrl+T Send the menu character itself to remote
83 --- Ctrl+] Send the exit character itself to remote
84 --- Ctrl+I Show info
85 --- Ctrl+U Upload file (prompt will be shown)
86 --- Ctrl+A encoding
87 --- Ctrl+F edit filters
cliechti5134aab2009-07-21 19:47:59 +000088 --- Toggles:
Chris Liechtic8c51832015-08-26 23:58:17 +020089 --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK
90 --- Ctrl+E echo Ctrl+L EOL
cliechti5134aab2009-07-21 19:47:59 +000091 ---
92 --- Port settings (Ctrl+T followed by the following):
Chris Liechtic8c51832015-08-26 23:58:17 +020093 --- p change port
94 --- 7 8 set data bits
95 --- N E O S M change parity (None, Even, Odd, Space, Mark)
96 --- 1 2 3 set stop bits (1, 2, 1.5)
97 --- b change baud rate
98 --- x X disable/enable software flow control
99 --- r R disable/enable hardware flow control
cliechti5134aab2009-07-21 19:47:59 +0000100
cliechti8611bf42009-08-03 00:34:03 +0000101.. versionchanged:: 2.5
cliechti4a63d6b2011-03-19 02:57:51 +0000102 Added :kbd:`Ctrl+T` menu and added support for opening URLs.
103.. versionchanged:: 2.6
104 File moved from the examples to :mod:`serial.tools.miniterm`.
Chris Liechtic8c51832015-08-26 23:58:17 +0200105.. versionchanged:: 3.0
106 Apply encoding on serial port, convert to Unicode for console.
107 Added new filters, default to stripping terminal control sequences.
cliechti86e87872009-07-21 13:32:45 +0000108
109miniterm.py_
110 The miniterm program.
111
112setup-miniterm-py2exe.py_
113 This is a py2exe setup script for Windows. It can be used to create a
114 standalone ``miniterm.exe``.
115
Chris Liechti90570b92015-08-04 03:32:02 +0200116.. _miniterm.py: https://github.com/pyserial/pyserial/blob/master/serial/tools/miniterm.py
117.. _setup-miniterm-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-miniterm-py2exe.py
cliechti86e87872009-07-21 13:32:45 +0000118
119
120TCP/IP - serial bridge
121======================
122This program opens a TCP/IP port. When a connection is made to that port (e.g.
123with telnet) it forwards all data to the serial port and vice versa.
124
cliechti3453f122009-08-10 22:56:34 +0000125This example only exports a raw socket connection. The next example
126below gives the client much more control over the remote serial port.
cliechtidec243b2009-08-04 02:24:03 +0000127
128- The serial port settings are set on the command line when starting the
129 program.
130- There is no possibility to change settings from remote.
131- All data is passed through as-is.
132
cliechti5134aab2009-07-21 19:47:59 +0000133::
134
135 Usage: tcp_serial_redirect.py [options] [port [baudrate]]
136
137 Simple Serial to Network (TCP/IP) redirector.
138
139 Options:
140 -h, --help show this help message and exit
141 -q, --quiet suppress non error messages
142 --spy peek at the communication and print all data to the
143 console
144
145 Serial Port:
146 Serial port settings
147
148 -p PORT, --port=PORT
149 port, a number (default 0) or a device name
150 -b BAUDRATE, --baud=BAUDRATE
151 set baud rate, default: 9600
152 --parity=PARITY set parity, one of [N, E, O], default=N
153 --rtscts enable RTS/CTS flow control (default off)
154 --xonxoff enable software flow control (default off)
155 --rts=RTS_STATE set initial RTS line state (possible values: 0, 1)
156 --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1)
157
158 Network settings:
159 Network configuration.
160
161 -P LOCAL_PORT, --localport=LOCAL_PORT
162 local TCP port
cliechtidec243b2009-08-04 02:24:03 +0000163 --rfc2217 allow control commands with Telnet extension RFC-2217
cliechti5134aab2009-07-21 19:47:59 +0000164
165 Newline Settings:
166 Convert newlines between network and serial port. Conversion is
167 normally disabled and can be enabled by --convert.
168
169 -c, --convert enable newline conversion (default off)
170 --net-nl=NET_NEWLINE
171 type of newlines that are expected on the network
172 (default: LF)
173 --ser-nl=SER_NEWLINE
174 type of newlines that are expected on the serial port
175 (default: CR+LF)
176
177 NOTE: no security measures are implemented. Anyone can remotely connect to
178 this service over the network. Only one connection at once is supported. When
179 the connection is terminated it waits for the next connect.
180
cliechti86e87872009-07-21 13:32:45 +0000181
182tcp_serial_redirect.py_
183 Main program.
184
Chris Liechti90570b92015-08-04 03:32:02 +0200185.. _tcp_serial_redirect.py: https://github.com/pyserial/pyserial/blob/master/examples/tcp_serial_redirect.py
cliechti4cb94662013-10-17 03:17:50 +0000186
cliechti86e87872009-07-21 13:32:45 +0000187
cliechtia2d8f962009-08-10 23:59:27 +0000188Single-port TCP/IP - serial bridge (RFC 2217)
cliechti3453f122009-08-10 22:56:34 +0000189=============================================
190Simple cross platform :rfc:`2217` serial port server. It uses threads and is
191portable (runs on POSIX, Windows, etc).
cliechti86e87872009-07-21 13:32:45 +0000192
cliechtia2d8f962009-08-10 23:59:27 +0000193- The port settings and control lines (RTS/DTR) can be changed at any time
194 using :rfc:`2217` requests. The status lines (DSR/CTS/RI/CD) are polled every
cliechti3453f122009-08-10 22:56:34 +0000195 second and notifications are sent to the client.
196- Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed
197 by an other value is interpreted as Telnet command sequence.
198- Telnet negotiation commands are sent when connecting to the server.
199- RTS/DTR are activated on client connect and deactivated on disconnect.
200- Default port settings are set again when client disconnects.
cliechtia2d8f962009-08-10 23:59:27 +0000201
cliechti3453f122009-08-10 22:56:34 +0000202::
203
204 Usage: rfc2217_server.py [options] port
205
206 RFC 2217 Serial to Network (TCP/IP) redirector.
207
208 Options:
209 -h, --help show this help message and exit
210 -p LOCAL_PORT, --localport=LOCAL_PORT
211 local TCP port
212
213 NOTE: no security measures are implemented. Anyone can remotely connect to
214 this service over the network. Only one connection at once is supported. When
215 the connection is terminated it waits for the next connect.
216
217.. versionadded:: 2.5
218
219rfc2217_server.py_
220 Main program.
221
222setup-rfc2217_server-py2exe.py_
223 This is a py2exe setup script for Windows. It can be used to create a
224 standalone ``rfc2217_server.exe``.
225
Chris Liechti90570b92015-08-04 03:32:02 +0200226.. _rfc2217_server.py: https://github.com/pyserial/pyserial/blob/master/examples/rfc2217_server.py
227.. _setup-rfc2217_server-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-rfc2217_server-py2exe.py
cliechti3453f122009-08-10 22:56:34 +0000228
229
230Multi-port TCP/IP - serial bridge (RFC 2217)
231============================================
cliechtibb5c3c52009-07-23 02:29:27 +0000232This example implements a TCP/IP to serial port service that works with
cliechti3453f122009-08-10 22:56:34 +0000233multiple ports at once. It uses select, no threads, for the serial ports and
234the network sockets and therefore runs on POSIX systems only.
cliechtibb5c3c52009-07-23 02:29:27 +0000235
cliechti7aed8332009-08-05 14:19:31 +0000236- Full control over the serial port with :rfc:`2217`.
cliechti3453f122009-08-10 22:56:34 +0000237- Check existence of ``/tty/USB0...8``. This is done every 5 seconds using
cliechti7aed8332009-08-05 14:19:31 +0000238 ``os.path.exists``.
239- Send zeroconf announcements when port appears or disappears (uses
240 python-avahi and dbus). Service name: ``_serial_port._tcp``.
241- Each serial port becomes available as one TCP/IP server. e.g.
242 ``/dev/ttyUSB0`` is reachable at ``<host>:7000``.
243- Single process for all ports and sockets (not per port).
244- The script can be started as daemon.
245- Logging to stdout or when run as daemon to syslog.
cliechti3453f122009-08-10 22:56:34 +0000246- Default port settings are set again when client disconnects.
cliechti7cb78e82009-08-05 15:47:57 +0000247- modem status lines (CTS/DSR/RI/CD) are not polled periodically and the server
248 therefore does not send NOTIFY_MODEMSTATE on its own. However it responds to
249 request from the client (i.e. use the ``poll_modem`` option in the URL when
250 using a pySerial client.)
cliechtibb5c3c52009-07-23 02:29:27 +0000251
cliechti7aed8332009-08-05 14:19:31 +0000252Requirements:
253
254- Python (>= 2.4)
cliechtibb5c3c52009-07-23 02:29:27 +0000255- python-avahi
256- python-dbus
cliechti7aed8332009-08-05 14:19:31 +0000257- python-serial (>= 2.5)
cliechtibb5c3c52009-07-23 02:29:27 +0000258
cliechti7aed8332009-08-05 14:19:31 +0000259Installation as daemon:
260
cliechtibb5c3c52009-07-23 02:29:27 +0000261- Copy the script ``port_publisher.py`` to ``/usr/local/bin``.
262- Copy the script ``port_publisher.sh`` to ``/etc/init.d``.
263- Add links to the runlevels using ``update-rc.d port_publisher.sh defaults 99``
264- Thats it :-) the service will be started on next reboot. Alternatively run
265 ``invoke-rc.d port_publisher.sh start`` as root.
266
cliechti7aed8332009-08-05 14:19:31 +0000267.. versionadded:: 2.5 new example
cliechtibb5c3c52009-07-23 02:29:27 +0000268
269port_publisher.py_
cliechti7aed8332009-08-05 14:19:31 +0000270 Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
cliechtibb5c3c52009-07-23 02:29:27 +0000271
272port_publisher.sh_
273 Example init.d script.
274
Chris Liechti90570b92015-08-04 03:32:02 +0200275.. _port_publisher.py: https://github.com/pyserial/pyserial/blob/master/examples/port_publisher.py
276.. _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 +0000277
278
cliechti86e87872009-07-21 13:32:45 +0000279wxPython examples
280=================
281A simple terminal application for wxPython and a flexible serial port
282configuration dialog are shown here.
283
284wxTerminal.py_
285 A simple terminal application. Note that the length of the buffer is
286 limited by wx and it may suddenly stop displaying new input.
287
288wxTerminal.wxg_
289 A wxGlade design file for the terminal application.
290
291wxSerialConfigDialog.py_
292 A flexible serial port configuration dialog.
293
294wxSerialConfigDialog.wxg_
295 The wxGlade design file for the configuration dialog.
296
cliechti3453f122009-08-10 22:56:34 +0000297setup-wxTerminal-py2exe.py_
cliechti86e87872009-07-21 13:32:45 +0000298 A py2exe setup script to package the terminal application.
299
Chris Liechti90570b92015-08-04 03:32:02 +0200300.. _wxTerminal.py: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.py
301.. _wxTerminal.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxTerminal.wxg
302.. _wxSerialConfigDialog.py: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.py
303.. _wxSerialConfigDialog.wxg: https://github.com/pyserial/pyserial/blob/master/examples/wxSerialConfigDialog.wxg
304.. _setup-wxTerminal-py2exe.py: https://github.com/pyserial/pyserial/blob/master/examples/setup-wxTerminal-py2exe.py
cliechti86e87872009-07-21 13:32:45 +0000305
306
cliechti86e87872009-07-21 13:32:45 +0000307
cliechti86e87872009-07-21 13:32:45 +0000308Unit tests
309==========
310The project uses a number of unit test to verify the functionality. They all
cliechti88c46842009-08-10 22:29:54 +0000311need a loop back connector. The scripts itself contain more information. All
312test scripts are contained in the directory ``test``.
cliechti86e87872009-07-21 13:32:45 +0000313
cliechti8611bf42009-08-03 00:34:03 +0000314The unit tests are performed on port ``0`` unless a different device name or
cliechti88c46842009-08-10 22:29:54 +0000315``rfc2217://`` URL is given on the command line (argv[1]).
316
317run_all_tests.py_
318 Collect all tests from all ``test*`` files and run them. By default, the
319 ``loop://`` device is used.
cliechti8611bf42009-08-03 00:34:03 +0000320
cliechti86e87872009-07-21 13:32:45 +0000321test.py_
cliechti88c46842009-08-10 22:29:54 +0000322 Basic tests (binary capabilities, timeout, control lines).
cliechti86e87872009-07-21 13:32:45 +0000323
cliechti86e87872009-07-21 13:32:45 +0000324test_advanced.py_
cliechti88c46842009-08-10 22:29:54 +0000325 Test more advanced features (properties).
cliechti86e87872009-07-21 13:32:45 +0000326
cliechtibb5c3c52009-07-23 02:29:27 +0000327test_high_load.py_
328 Tests involving sending a lot of data.
329
cliechti25375b52010-07-21 23:27:13 +0000330test_readline.py_
331 Tests involving readline.
332
cliechtidaf47ba2009-07-28 01:28:16 +0000333test_iolib.py_
334 Tests involving the :mod:`io` library. Only available for Python 2.6 and
335 newer.
336
cliechti4a63d6b2011-03-19 02:57:51 +0000337test_url.py_
338 Tests involving the :ref:`URL <URLs>` feature.
339
Chris Liechti90570b92015-08-04 03:32:02 +0200340.. _run_all_tests.py: https://github.com/pyserial/pyserial/blob/master/test/run_all_tests.py
341.. _test.py: https://github.com/pyserial/pyserial/blob/master/test/test.py
342.. _test_advanced.py: https://github.com/pyserial/pyserial/blob/master/test/test_advanced.py
343.. _test_high_load.py: https://github.com/pyserial/pyserial/blob/master/test/test_high_load.py
344.. _test_readline.py: https://github.com/pyserial/pyserial/blob/master/test/test_readline.py
345.. _test_iolib.py: https://github.com/pyserial/pyserial/blob/master/test/test_iolib.py
346.. _test_url.py: https://github.com/pyserial/pyserial/blob/master/test/test_url.py