From bfd7c372c99f0c07da9a3bda4685dc33ee06cd91 Mon Sep 17 00:00:00 2001 From: cliechti Date: Thu, 22 Jul 2010 00:28:51 +0000 Subject: tag for release 2.5 --- documentation/shortintro.rst | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 documentation/shortintro.rst (limited to 'documentation/shortintro.rst') diff --git a/documentation/shortintro.rst b/documentation/shortintro.rst new file mode 100644 index 0000000..048f94d --- /dev/null +++ b/documentation/shortintro.rst @@ -0,0 +1,57 @@ +==================== + Short introduction +==================== + +Opening serial ports +==================== + +Open port 0 at "9600,8,N,1", no timeout:: + + >>> import serial + >>> ser = serial.Serial(0) # open first serial port + >>> print ser.portstr # check which port was really used + >>> ser.write("hello") # write a string + >>> ser.close() # close port + +Open named port at "19200,8,N,1", 1s timeout:: + + >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1) + >>> x = ser.read() # read one byte + >>> s = ser.read(10) # read up to ten bytes (timeout) + >>> line = ser.readline() # read a '\n' terminated line + >>> ser.close() + +Open second port at "38400,8,E,1", non blocking HW handshaking:: + + >>> ser = serial.Serial(1, 38400, timeout=0, + ... parity=serial.PARITY_EVEN, rtscts=1) + >>> s = ser.read(100) # read up to one hundred bytes + ... # or as much is in the buffer + +Configuring ports later +======================= + +Get a Serial instance and configure/open it later:: + + >>> ser = serial.Serial() + >>> ser.baudrate = 19200 + >>> ser.port = 0 + >>> ser + Serial(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0) + >>> ser.open() + >>> ser.isOpen() + True + >>> ser.close() + >>> ser.isOpen() + False + +Readline +======== +Be carefully when using "readline". Do specify a timeout when opening the +serial port otherwise it could block forever if no newline character is +received. Also note that "readlines" only works with a timeout. "readlines" +depends on having a timeout and interprets that as EOF (end of file). It raises +an exception if the port is not opened correctly. + +Do also have a look at the example files in the examples directory in the +source distribution or online. -- cgit v1.2.1