blob: e5901faa3923edcd8f8fa2e2866b6b6e1cb3b0ca [file] [log] [blame]
cliechti86e87872009-07-21 13:32:45 +00001==========
2 Examples
3==========
4
5Miniterm
6========
7This is a console application that provides a small terminal application.
8miniterm itself does not implement any terminal features such as VT102
9compatibility. However it inherits these features from the terminal it is run.
10For example on GNU/Linux running from an xterm it will support the escape
11sequences of the xterm. On Windows the typical console window is dumb and does
12not support any escapes. When ANSI.sys is loaded it supports some escapes.
13
cliechti5134aab2009-07-21 19:47:59 +000014miniterm::
15
16 --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
17 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
18
cliechti86e87872009-07-21 13:32:45 +000019Command line options can be given so that binary data including escapes for
20terminals are escaped or output as hex.
21
cliechti5134aab2009-07-21 19:47:59 +000022Command line options ``miniterm.py -h``::
23
24 Usage: miniterm.py [options] [port [baudrate]]
25
26 Miniterm - A simple terminal program for the serial port.
27
28 Options:
29 -h, --help show this help message and exit
30 -p PORT, --port=PORT port, a number (default 0) or a device name
31 (deprecated option)
32 -b BAUDRATE, --baud=BAUDRATE
33 set baud rate, default 9600
34 --parity=PARITY set parity, one of [N, E, O, S, M], default=N
35 -e, --echo enable local echo (default off)
36 --rtscts enable RTS/CTS flow control (default off)
37 --xonxoff enable software flow control (default off)
38 --cr do not send CR+LF, send CR only
39 --lf do not send CR+LF, send LF only
40 -D, --debug debug received data (escape non-printable chars)
41 --debug can be given multiple times: 0: just print
42 what is received 1: escape non-printable characters,
43 do newlines as unusual 2: escape non-printable
44 characters, newlines too 3: hex dump everything
45 --rts=RTS_STATE set initial RTS line state (possible values: 0, 1)
46 --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1)
47 -q, --quiet suppress non error messages
48 --exit-char=EXIT_CHAR
49 ASCII code of special character that is used to exit
50 the application
51 --menu-char=MENU_CHAR
52 ASCII code of special character that is used to
53 control miniterm (menu)
54
55
56miniterm supports some control functions. Typing :kbd:`Control+t Control+h` when it is
57running shows the help text::
58
59 --- pySerial - miniterm - help
60 ---
61 --- Ctrl+] Exit program
62 --- Ctrl+T Menu escape key, followed by:
63 --- Menu keys:
64 --- Ctrl+T Send the menu character itself to remote
65 --- Ctrl+] Send the exit character to remote
66 --- Ctrl+I Show info
67 --- Ctrl+U Upload file (prompt will be shown)
68 --- Toggles:
69 --- Ctrl+R RTS Ctrl+E local echo
70 --- Ctrl+D DTR Ctrl+B BREAK
71 --- Ctrl+L line feed Ctrl+A Cycle repr mode
72 ---
73 --- Port settings (Ctrl+T followed by the following):
74 --- 7 8 set data bits
75 --- n e o s m change parity (None, Even, Odd, Space, Mark)
76 --- 1 2 3 set stop bits (1, 2, 1.5)
77 --- b change baud rate
78 --- x X disable/enable software flow control
79 --- r R disable/enable hardware flow control
80
cliechti8611bf42009-08-03 00:34:03 +000081.. versionchanged:: 2.5
82 Added :kbd:`Control+t` menu and added support for opening :rfc:`2217` ports
83 (use ``rfc2217:://<host>:<port>`` as *port* argument when invoking).
cliechti86e87872009-07-21 13:32:45 +000084
85miniterm.py_
86 The miniterm program.
87
88setup-miniterm-py2exe.py_
89 This is a py2exe setup script for Windows. It can be used to create a
90 standalone ``miniterm.exe``.
91
92.. _miniterm.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/miniterm.py
93.. _setup-miniterm-py2exe.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/setup-miniterm-py2exe.py
94
95
96TCP/IP - serial bridge
97======================
98This program opens a TCP/IP port. When a connection is made to that port (e.g.
99with telnet) it forwards all data to the serial port and vice versa.
100
cliechtidec243b2009-08-04 02:24:03 +0000101There are two modes of operation:
102
103raw socket (default):
104
105- The serial port settings are set on the command line when starting the
106 program.
107- There is no possibility to change settings from remote.
108- All data is passed through as-is.
109
110:rfc:`2217` (use ``--rfc2217`` command line option):
111
112- The initial serial port settings are set on the command line when starting
113 the program.
114- The port settings and control lines (RTS/DTR) can changed at any time using
115 :rfc:`2217` requests. The status lines (DSR/CTS/RI/CD) are polled every
116 second and notifications are sent to the client.
117- Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed
118 by an other value is interpreted as telnet command sequence.
119- Telnet negotiation commands are sent when connecting to the server.
120
cliechti5134aab2009-07-21 19:47:59 +0000121::
122
123 Usage: tcp_serial_redirect.py [options] [port [baudrate]]
124
125 Simple Serial to Network (TCP/IP) redirector.
126
127 Options:
128 -h, --help show this help message and exit
129 -q, --quiet suppress non error messages
130 --spy peek at the communication and print all data to the
131 console
132
133 Serial Port:
134 Serial port settings
135
136 -p PORT, --port=PORT
137 port, a number (default 0) or a device name
138 -b BAUDRATE, --baud=BAUDRATE
139 set baud rate, default: 9600
140 --parity=PARITY set parity, one of [N, E, O], default=N
141 --rtscts enable RTS/CTS flow control (default off)
142 --xonxoff enable software flow control (default off)
143 --rts=RTS_STATE set initial RTS line state (possible values: 0, 1)
144 --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1)
145
146 Network settings:
147 Network configuration.
148
149 -P LOCAL_PORT, --localport=LOCAL_PORT
150 local TCP port
cliechtidec243b2009-08-04 02:24:03 +0000151 --rfc2217 allow control commands with Telnet extension RFC-2217
cliechti5134aab2009-07-21 19:47:59 +0000152
153 Newline Settings:
154 Convert newlines between network and serial port. Conversion is
155 normally disabled and can be enabled by --convert.
156
157 -c, --convert enable newline conversion (default off)
158 --net-nl=NET_NEWLINE
159 type of newlines that are expected on the network
160 (default: LF)
161 --ser-nl=SER_NEWLINE
162 type of newlines that are expected on the serial port
163 (default: CR+LF)
164
165 NOTE: no security measures are implemented. Anyone can remotely connect to
166 this service over the network. Only one connection at once is supported. When
167 the connection is terminated it waits for the next connect.
168
cliechtidec243b2009-08-04 02:24:03 +0000169.. versionchanged:: 2.5 added ``--rfc2217`` option
170
cliechti86e87872009-07-21 13:32:45 +0000171
172tcp_serial_redirect.py_
173 Main program.
174
175.. _tcp_serial_redirect.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/tcp_serial_redirect.py
176
177
cliechtibb5c3c52009-07-23 02:29:27 +0000178Multi-port TCP/IP - serial bridge
179=================================
180This example implements a TCP/IP to serial port service that works with
181multiple ports at once. It uses select, no threads, and runs on POSIX systems
182only.
183
cliechti7aed8332009-08-05 14:19:31 +0000184- Full control over the serial port with :rfc:`2217`.
185- Check existence of ``/tty/USB0...9``. This is done every 5 seconds using
186 ``os.path.exists``.
187- Send zeroconf announcements when port appears or disappears (uses
188 python-avahi and dbus). Service name: ``_serial_port._tcp``.
189- Each serial port becomes available as one TCP/IP server. e.g.
190 ``/dev/ttyUSB0`` is reachable at ``<host>:7000``.
191- Single process for all ports and sockets (not per port).
192- The script can be started as daemon.
193- Logging to stdout or when run as daemon to syslog.
cliechti7cb78e82009-08-05 15:47:57 +0000194- modem status lines (CTS/DSR/RI/CD) are not polled periodically and the server
195 therefore does not send NOTIFY_MODEMSTATE on its own. However it responds to
196 request from the client (i.e. use the ``poll_modem`` option in the URL when
197 using a pySerial client.)
cliechtibb5c3c52009-07-23 02:29:27 +0000198
cliechti7aed8332009-08-05 14:19:31 +0000199Requirements:
200
201- Python (>= 2.4)
cliechtibb5c3c52009-07-23 02:29:27 +0000202- python-avahi
203- python-dbus
cliechti7aed8332009-08-05 14:19:31 +0000204- python-serial (>= 2.5)
cliechtibb5c3c52009-07-23 02:29:27 +0000205
206
cliechti7aed8332009-08-05 14:19:31 +0000207Installation as daemon:
208
cliechtibb5c3c52009-07-23 02:29:27 +0000209- Copy the script ``port_publisher.py`` to ``/usr/local/bin``.
210- Copy the script ``port_publisher.sh`` to ``/etc/init.d``.
211- Add links to the runlevels using ``update-rc.d port_publisher.sh defaults 99``
212- Thats it :-) the service will be started on next reboot. Alternatively run
213 ``invoke-rc.d port_publisher.sh start`` as root.
214
cliechti7aed8332009-08-05 14:19:31 +0000215.. versionadded:: 2.5 new example
cliechtibb5c3c52009-07-23 02:29:27 +0000216
217port_publisher.py_
cliechti7aed8332009-08-05 14:19:31 +0000218 Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
cliechtibb5c3c52009-07-23 02:29:27 +0000219
220port_publisher.sh_
221 Example init.d script.
222
223.. _port_publisher.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/port_publisher.py
224.. _port_publisher.sh: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/port_publisher.sh
225
226
cliechti86e87872009-07-21 13:32:45 +0000227wxPython examples
228=================
229A simple terminal application for wxPython and a flexible serial port
230configuration dialog are shown here.
231
232wxTerminal.py_
233 A simple terminal application. Note that the length of the buffer is
234 limited by wx and it may suddenly stop displaying new input.
235
236wxTerminal.wxg_
237 A wxGlade design file for the terminal application.
238
239wxSerialConfigDialog.py_
240 A flexible serial port configuration dialog.
241
242wxSerialConfigDialog.wxg_
243 The wxGlade design file for the configuration dialog.
244
245setup_demo.py_
246 A py2exe setup script to package the terminal application.
247
248.. _wxTerminal.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/wxTerminal.py
249.. _wxTerminal.wxg: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/wxTerminal.wxg
250.. _wxSerialConfigDialog.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/wxSerialConfigDialog.py
251.. _wxSerialConfigDialog.wxg: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/wxSerialConfigDialog.wxg
252.. _setup_demo.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/setup_demo.py
253
254
255Wrapper class
256=============
257This example provides a subclass based on ``Serial`` that has an alternative
258implementation of ``readline()``
259
260enhancedserial.py_
261 A class with alternative ``readline()`` implementation.
262
263.. _enhancedserial.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/enhancedserial.py
264
265
266Finding serial ports
267====================
268scan.py_
269 A simple loop that probes serial ports by number.
270
cliechti6d1e9d12009-07-22 01:06:29 +0000271scanlinux.py_
272 A Linux only version looking at the entries in ``/dev``. It works best with
273 on systems with devfs or udev that only create those entries that represent
274 devices. On older installations a lot of pre-created device files are found
275 and an additional open check should be added to ensure that the device is
276 real.
277
cliechti86e87872009-07-21 13:32:45 +0000278scanwin32.py_
279 A Windows only version that returns a list of serial ports with information
280 from the registry.
281
282.. _scan.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/scan.py
cliechti6d1e9d12009-07-22 01:06:29 +0000283.. _scanlinux.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/scanlinux.py
cliechti86e87872009-07-21 13:32:45 +0000284.. _scanwin32.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/examples/scanwin32.py
285
286
287Unit tests
288==========
289The project uses a number of unit test to verify the functionality. They all
cliechti88c46842009-08-10 22:29:54 +0000290need a loop back connector. The scripts itself contain more information. All
291test scripts are contained in the directory ``test``.
cliechti86e87872009-07-21 13:32:45 +0000292
cliechti8611bf42009-08-03 00:34:03 +0000293The unit tests are performed on port ``0`` unless a different device name or
cliechti88c46842009-08-10 22:29:54 +0000294``rfc2217://`` URL is given on the command line (argv[1]).
295
296run_all_tests.py_
297 Collect all tests from all ``test*`` files and run them. By default, the
298 ``loop://`` device is used.
cliechti8611bf42009-08-03 00:34:03 +0000299
cliechti86e87872009-07-21 13:32:45 +0000300test.py_
cliechti88c46842009-08-10 22:29:54 +0000301 Basic tests (binary capabilities, timeout, control lines).
cliechti86e87872009-07-21 13:32:45 +0000302
cliechti86e87872009-07-21 13:32:45 +0000303test_advanced.py_
cliechti88c46842009-08-10 22:29:54 +0000304 Test more advanced features (properties).
cliechti86e87872009-07-21 13:32:45 +0000305
cliechtibb5c3c52009-07-23 02:29:27 +0000306test_high_load.py_
307 Tests involving sending a lot of data.
308
cliechtidaf47ba2009-07-28 01:28:16 +0000309test_iolib.py_
310 Tests involving the :mod:`io` library. Only available for Python 2.6 and
311 newer.
312
cliechti88c46842009-08-10 22:29:54 +0000313.. _run_all_tests.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/test/run_all_tests.py
314.. _test.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/test/test.py
315.. _test_advanced.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/test/test_advanced.py
316.. _test_high_load.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/test/test_high_load.py
317.. _test_iolib.py: http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/test/test_iolib.py