blob: 5e117ca1333b2c6013529ae9de816ddd09e10901 [file] [log] [blame]
cliechti22978142009-07-21 01:58:11 +00001====================
2 Short introduction
3====================
4
5Open port 0 at "9600,8,N,1", no timeout::
6
7 >>> import serial
8 >>> ser = serial.Serial(0) # open first serial port
9 >>> print ser.portstr # check which port was really used
10 >>> ser.write("hello") # write a string
11 >>> ser.close() # close port
12
13Open named port at "19200,8,N,1", 1s timeout::
14
15 >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
16 >>> x = ser.read() # read one byte
17 >>> s = ser.read(10) # read up to ten bytes (timeout)
18 >>> line = ser.readline() # read a '\n' terminated line
19 >>> ser.close()
20
21Open second port at "38400,8,E,1", non blocking HW handshaking::
22
23 >>> ser = serial.Serial(1, 38400, timeout=0,
24 ... parity=serial.PARITY_EVEN, rtscts=1)
25 >>> s = ser.read(100) # read up to one hundred bytes
26 ... # or as much is in the buffer
27
28Get a Serial instance and configure/open it later::
29
30 >>> ser = serial.Serial()
31 >>> ser.baudrate = 19200
32 >>> ser.port = 0
33 >>> ser
34 Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
35 >>> ser.open()
36 >>> ser.isOpen()
37 True
38 >>> ser.close()
39 >>> ser.isOpen()
40 False
41
42Be carefully when using "readline". Do specify a timeout when opening the
43serial port otherwise it could block forever if no newline character is
44received. Also note that "readlines" only works with a timeout. "readlines"
45depends on having a timeout and interprets that as EOF (end of file). It raises
46an exception if the port is not opened correctly. Do also have a look at the
47example files in the examples directory in the source distribution or online.