blob: 2d9dd1b3837cc9598c2a4788aa92f49cf2dddbaf [file] [log] [blame]
cliechti22978142009-07-21 01:58:11 +00001====================
2 Short introduction
3====================
4
cliechti86e87872009-07-21 13:32:45 +00005Opening serial ports
6====================
7
cliechti22978142009-07-21 01:58:11 +00008Open port 0 at "9600,8,N,1", no timeout::
9
10 >>> import serial
11 >>> ser = serial.Serial(0) # open first serial port
cliechti4cb94662013-10-17 03:17:50 +000012 >>> print ser.name # check which port was really used
cliechti22978142009-07-21 01:58:11 +000013 >>> ser.write("hello") # write a string
14 >>> ser.close() # close port
15
16Open named port at "19200,8,N,1", 1s timeout::
17
18 >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
19 >>> x = ser.read() # read one byte
20 >>> s = ser.read(10) # read up to ten bytes (timeout)
21 >>> line = ser.readline() # read a '\n' terminated line
22 >>> ser.close()
23
24Open second port at "38400,8,E,1", non blocking HW handshaking::
25
26 >>> ser = serial.Serial(1, 38400, timeout=0,
27 ... parity=serial.PARITY_EVEN, rtscts=1)
28 >>> s = ser.read(100) # read up to one hundred bytes
29 ... # or as much is in the buffer
30
cliechti86e87872009-07-21 13:32:45 +000031Configuring ports later
32=======================
33
cliechti22978142009-07-21 01:58:11 +000034Get a Serial instance and configure/open it later::
35
36 >>> ser = serial.Serial()
37 >>> ser.baudrate = 19200
38 >>> ser.port = 0
39 >>> ser
40 Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
41 >>> ser.open()
42 >>> ser.isOpen()
43 True
44 >>> ser.close()
45 >>> ser.isOpen()
46 False
47
cliechti86e87872009-07-21 13:32:45 +000048Readline
49========
cliechtifbe5ae72011-08-05 00:50:21 +000050Be carefully when using :meth:`readline`. Do specify a timeout when opening the
cliechti22978142009-07-21 01:58:11 +000051serial port otherwise it could block forever if no newline character is
cliechtifbe5ae72011-08-05 00:50:21 +000052received. Also note that :meth:`readlines` only works with a timeout.
53:meth:`readlines` depends on having a timeout and interprets that as EOF (end
54of file). It raises an exception if the port is not opened correctly.
cliechti86e87872009-07-21 13:32:45 +000055
56Do also have a look at the example files in the examples directory in the
57source distribution or online.
cliechtifbe5ae72011-08-05 00:50:21 +000058
59.. note::
60
61 The ``eol`` parameter for :meth:`readline` is no longer supported when
62 pySerial is run with newer Python versions (V2.6+) where the module
63 :mod:`io` is available.
64
65EOL
66---
67To specify the EOL character for :meth:`readline` or to use universal newline
68mode, it is advised to use io.TextIOWrapper_::
69
70 import serial
71 import io
72 ser = serial.serial_for_url('loop://', timeout=1)
73 sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
74
75 sio.write(unicode("hello\n"))
76 sio.flush() # it is buffering. required to get the data out *now*
77 hello = sio.readline()
78 print hello == unicode("hello\n")
79
80
81.. _io.TextIOWrapper: http://docs.python.org/library/io.html#io.TextIOWrapper
cliechti7bb58832011-08-05 03:55:12 +000082
83
84Testing ports
85=============
86Listing ports
87-------------
88``python -m serial.tools.list_ports`` will print a list of available ports. It
89is also possible to add a regexp as first argument and the list will only
90include entries that matched.
91
92.. note::
93
94 The enumeration may not work on all operating systems. It may be
95 incomplete, list unavailable ports or may lack detailed descriptions of the
96 ports.
97
98.. versionadded: 2.6
99
100Accessing ports
101---------------
cliechti4cb94662013-10-17 03:17:50 +0000102pySerial includes a small console based terminal program called
cliechti7bb58832011-08-05 03:55:12 +0000103:ref:`miniterm`. It ca be started with ``python -m serial.tools.miniterm <port name>``
104(use option ``-h`` to get a listing of all options).