Newer
Older
TestStandRepository / Software / Arduino / libraries / pyserial-2.7 / documentation / examples.rst
  1. .. _examples:
  2.  
  3. ==========
  4. Examples
  5. ==========
  6.  
  7. .. _miniterm:
  8.  
  9. Miniterm
  10. ========
  11. This is a console application that provides a small terminal application.
  12. Miniterm itself does not implement any terminal features such as VT102
  13. compatibility. However it inherits these features from the terminal it is run.
  14. For example on GNU/Linux running from an xterm it will support the escape
  15. sequences of the xterm. On Windows the typical console window is dumb and does
  16. not support any escapes. When ANSI.sys is loaded it supports some escapes.
  17.  
  18. Miniterm::
  19.  
  20. --- Miniterm on /dev/ttyS0: 9600,8,N,1 ---
  21. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  22.  
  23. Command line options can be given so that binary data including escapes for
  24. terminals are escaped or output as hex.
  25.  
  26. Miniterm supports :rfc:`2217` remote serial ports and raw sockets using :ref:`URLs`
  27. such as ``rfc2217:://<host>:<port>`` respectively ``socket://<host>:<port>`` as
  28. *port* argument when invoking.
  29.  
  30. Command line options ``python -m serial.tools.miniterm -h``::
  31.  
  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
  38.  
  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
  44. set baud rate, default 9600
  45. --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)
  56. --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
  60.  
  61. Hotkeys:
  62. --exit-char=EXIT_CHAR
  63. ASCII code of special character that is used to exit
  64. the application
  65. --menu-char=MENU_CHAR
  66. ASCII code of special character that is used to
  67. control miniterm (menu)
  68.  
  69. Diagnostics:
  70. -q, --quiet suppress non-error messages
  71.  
  72.  
  73. Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is
  74. running 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:
  81. --- 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)
  85. --- 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):
  91. --- p change port
  92. --- 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.  
  99. .. versionchanged:: 2.5
  100. 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`.
  103.  
  104. miniterm.py_
  105. The miniterm program.
  106.  
  107. setup-miniterm-py2exe.py_
  108. This is a py2exe setup script for Windows. It can be used to create a
  109. standalone ``miniterm.exe``.
  110.  
  111. .. _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
  113.  
  114.  
  115. TCP/IP - serial bridge
  116. ======================
  117. This program opens a TCP/IP port. When a connection is made to that port (e.g.
  118. with telnet) it forwards all data to the serial port and vice versa.
  119.  
  120. This example only exports a raw socket connection. The next example
  121. below gives the client much more control over the remote serial port.
  122.  
  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.  
  128. ::
  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
  158. --rfc2217 allow control commands with Telnet extension RFC-2217
  159.  
  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.  
  176.  
  177. tcp_serial_redirect.py_
  178. Main program.
  179.  
  180. .. _tcp_serial_redirect.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/tcp_serial_redirect.py
  181.  
  182.  
  183. Single-port TCP/IP - serial bridge (RFC 2217)
  184. =============================================
  185. Simple cross platform :rfc:`2217` serial port server. It uses threads and is
  186. portable (runs on POSIX, Windows, etc).
  187.  
  188. - 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
  190. 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.
  196.  
  197. ::
  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.  
  214. rfc2217_server.py_
  215. Main program.
  216.  
  217. setup-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.  
  221. .. _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
  223.  
  224.  
  225. Multi-port TCP/IP - serial bridge (RFC 2217)
  226. ============================================
  227. This example implements a TCP/IP to serial port service that works with
  228. multiple ports at once. It uses select, no threads, for the serial ports and
  229. the network sockets and therefore runs on POSIX systems only.
  230.  
  231. - Full control over the serial port with :rfc:`2217`.
  232. - Check existence of ``/tty/USB0...8``. This is done every 5 seconds using
  233. ``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.
  241. - Default port settings are set again when client disconnects.
  242. - 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.)
  246.  
  247. Requirements:
  248.  
  249. - Python (>= 2.4)
  250. - python-avahi
  251. - python-dbus
  252. - python-serial (>= 2.5)
  253.  
  254. Installation as daemon:
  255.  
  256. - 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.  
  262. .. versionadded:: 2.5 new example
  263.  
  264. port_publisher.py_
  265. Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
  266.  
  267. port_publisher.sh_
  268. Example init.d script.
  269.  
  270. .. _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
  272.  
  273.  
  274. wxPython examples
  275. =================
  276. A simple terminal application for wxPython and a flexible serial port
  277. configuration dialog are shown here.
  278.  
  279. wxTerminal.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.  
  283. wxTerminal.wxg_
  284. A wxGlade design file for the terminal application.
  285.  
  286. wxSerialConfigDialog.py_
  287. A flexible serial port configuration dialog.
  288.  
  289. wxSerialConfigDialog.wxg_
  290. The wxGlade design file for the configuration dialog.
  291.  
  292. setup-wxTerminal-py2exe.py_
  293. A py2exe setup script to package the terminal application.
  294.  
  295. .. _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
  300.  
  301.  
  302. Wrapper class
  303. =============
  304. This example provides a subclass based on ``Serial`` that has an alternative
  305. implementation of ``readline()``
  306.  
  307. enhancedserial.py_
  308. A class with alternative ``readline()`` implementation.
  309.  
  310. .. _enhancedserial.py: http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/enhancedserial.py
  311.  
  312.  
  313. Unit tests
  314. ==========
  315. The project uses a number of unit test to verify the functionality. They all
  316. need a loop back connector. The scripts itself contain more information. All
  317. test scripts are contained in the directory ``test``.
  318.  
  319. The unit tests are performed on port ``0`` unless a different device name or
  320. ``rfc2217://`` URL is given on the command line (argv[1]).
  321.  
  322. run_all_tests.py_
  323. Collect all tests from all ``test*`` files and run them. By default, the
  324. ``loop://`` device is used.
  325.  
  326. test.py_
  327. Basic tests (binary capabilities, timeout, control lines).
  328.  
  329. test_advanced.py_
  330. Test more advanced features (properties).
  331.  
  332. test_high_load.py_
  333. Tests involving sending a lot of data.
  334.  
  335. test_readline.py_
  336. Tests involving readline.
  337.  
  338. test_iolib.py_
  339. Tests involving the :mod:`io` library. Only available for Python 2.6 and
  340. newer.
  341.  
  342. test_url.py_
  343. Tests involving the :ref:`URL <URLs>` feature.
  344.  
  345. .. _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