Pyserial
Introduced
The serial communication module is encapsulated to support both Jython (Java) and Iconpython (. NET and Mono) for Linux, Windows, BSD (which may support all POSIX-enabled operating systems).
Home http://pyserial.sf.net/
Characteristics
All platforms use the same class interface
Port number default starting from 0, the program does not need to know the ports name
Api,read, write (ReadLine, and so on) are also supported as file reads and writes
All programs are completed by Python and do not rely on other packages except the standard library, except Pywin32 (Windows), Javacomm (Jython). POSIX (Linux, BSD) relies only on the Python standard library.
Dependent environment
Python2.2 or later
Pywin32 extensions on Windows
"Java Communications" (javacomm) or Compatibility Pack on Java/jython
Installation
Pip/easy_install
Pip Install Pyserial
Easy_install pyserial
Windows
: http://sourceforge.net/project/showfiles.php?group_id=46487
Get started quickly
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 (Ten) # 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 (+) # read up to one hundred bytes
... # or as much is in the buffer
Get a Serial instance and configure/open it later
>>> ser = serial. Serial ()
>>> ser.baudrate = 19200
>>> ser.port = 0
>>> ser
SERIAL<ID=0XA81C10, open=false> (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
If a port is given, the port opens immediately after the object is created, and if no port is given, the optional timeout parameter
Timeout=none # Wait Forever
Timeout=0 # non-blocking Mode (return immediately on read)
TIMEOUT=X # Set Timeout to x seconds (float allowed)
Available methods for serial instances
Open () # opening port
Close () # Close Port now
Setbaudrate (baudrate) # baud rate in an open port
Inwaiting () # Return the number of chars in the receive buffer
Read (size=1) # read "size" characters
Write (s) # writes the string s to the port
Flushinput () # Clears input buffers, discards all content
Flushoutput () # Clears the output buffer and discards the output
Sendbreak () # Send interrupt condition
Setrts (level=1) # set RTS line to specified logic level
SETDTR (level=1) # set DTR line to specified logic level
Getcts () # Return the state of the CTS line
GETDSR () # Return the state of the DSR line
Getri () # Return the state of the RI line
GETCD () # Return the state of the CD line
Serial properties of an instance
Read-only
PORTSTR # Device Name
Baudrates # List of valid baudrates
bytesizes # List of valid byte sizes
parities # List of valid parities
StopBits # List of valid stop bit widths
The following property values are changed after the port is reconfigured, even if the port is already open
Port # Port Name/number as set by the user
BaudRate # baud Rate setting
ByteSize # byte size in bits
Parity # Parity setting
StopBits # Stop bit with (UP)
Timeout # timeout setting
Xonxoff # If Xon/xoff flow control is enabled
Rtscts # If Hardware flow control is enabled
Abnormal
Serial. Serialexception
Constant
Parity
Serial. Parity_none
Serial. Parity_even
Serial. Parity_odd
StopBits
Serial. Stopbits_one
Al. Stopbits_two
ByteSize:
Serial. Fivebits
Serial. Sixbits
Serial. Sevenbits
Serial. Eightbits
Translation (with limitation) for reference only
Original address: http://blog.csdn.net/dainiao01/article/details/5885122
Official Document: http://pyserial.sf.net/
Pyserial Operation Serial Port