The Python module--pyserial of serial communication

Source: Internet
Author: User
Tags byte sizes flush posix readline svn
pyserial Overview

This module encapsulates the access for the serial port. It provides backends for Python running in Windows, Linux, BSD (possibly any POSIX compliant system), Jython and Ironpytho N (. NET and Mono). The module named "Serial" automatically selects the appropriate backend.

It is released under a free software license, and you'll have to LICENSE.txt for more details.
(C) 2001-2008 Chris Liechti cliechti@gmx.net

The project page on SourceForge and are the SVN repository and the Download page.
The homepage is on http://pyserial.sf.net/
Features Same class based interface on all supported platforms access to the port settings through Python 2.2+ properties Port num Bering starts at zero, no need to know the "port name in" User program port string (device name) can be specified if ACC ESS through numbering is inappropriate support for different, bytesizes, stopbits and flow control with parity and /or Xon/xoff working with or without receive timeout the file like APIs with "read" and "Write" ("ReadLine" etc. also supported The files in this package are 100% pure Python. They depend on non standard but common packages on Windows (PYWIN32) and Jython (Javacomm). POSIX (Linux, BSD) uses only modules from the standard Python distribution) The port is set up for binary transmission. No NULL byte stripping, cr-lf translation etc. (which are many times is enabled for POSIX.) This makes this module universally useful.

Requirements Python 2.2 or newer Pywin32 extensions on Windows "Java Communications" (JAVACOMM) or compatible extension for Java/jython

installation

From source

Extract files from the archive, open a shell/console into that directory and let Distutils do the rest:
Python setup.py Install

The files get installed in the "Lib/site-packages" directory.
Easy_install

An EGG are available from the Python Package index:http://pypi.python.org/pypi/pyserial
Easy_install pyserial
Windows Installer

There is also a Windows installer to end users. It is located in the Download Page
Developers May is interested to get the source archive, because it contains examples and the README.
Short Introduction

Open Port 0 at "9600,8,n,1", no timeout. Text. imp {font-weight:bold; color:red;}

>>> Import serial
>>> ser = serial. Serial (0)  # Open a serial port
>>> print ser.portstr       # Check which port was really used
>& Gt;> ser.write ("Hello")      # Write a string
>>> ser.close ()             # Close Port
Open named port at "19200,8,n,1", 1s timeout
. text. Imp {font-weight:bold; color:red;}
>>> 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
>>& Gt Ser.close ()
Open second port at "38400,8,e,1", non blocking HW handshaking
. text. Imp {font-weight:bold; color:red;}
>>> 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 was in the buffer
Get a serial instance and configure/open it later
. text. Imp {font-weight:bold; color:red;}
>>> 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
is carefully when using "ReadLine". Do specify a timeout when opening the serial port otherwise it could blocks forever if no newline character is received. Also Note "ReadLines" is only works with a timeout. "ReadLines" depends on has a timeout and interprets 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.

examplesPlease look in the SVN Repository. There is a example directory where you can find a simple terminal and more.
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/examples/

Parameters for the serial class. text. Imp {font-weight:bold; color:red;}
Ser = serial. Serial (
Port=none,              # Number of device, numbering starts at
# zero. If everything fails, the user
# can SPE Cify A device string, note
# "that" isn ' t portable anymore
# If no port is specified a unconfigured
# a C Losed serial Port object is created
baudrate=9600,          # baud rate bytesize=eightbits
,     # Number of DataBits
Parity=parity_none,     # Enable parity checking
Stopbits=stopbits_one, # Number of  stopbits
Timeout=none,           # Set a timeout value, None for waiting Forever
xonxoff=0,              # Enable software flow Control
  rtscts=0,               # Enable Rts/cts flow control
interchartimeout=none   # inter-character timeout, None to Disable
)
The port is immediately opened on object creation, if a port is given. It isn't opened if Port is None.
Options for Read timeout:
. text. Imp {font-weight:bold; color:red;}
Timeout=none            # Wait Forever
timeout=0               # non-blocking mode (return immediately on read)
timeout=x               # Set timeout to x seconds (float allowed)

Methods of serial instances. text. Imp {font-weight:bold; color:red;}
Open ()                  # Open port close
()                 # Close Port immediately
setbaudrate (baudrate)   # change baud rate on ' an op En port
inwaiting ()             # Return the number of chars in the receive Buffer
read (size=1)            # read ' size ' characte Rs
Write (s)                # Write the string s
to the Port Flushinput ()            # Flush Input buffer, discarding all it ' s con Tents
Flushoutput ()           # flush Output buffer, abort output
sendbreak ()             # send break condition
Setrts (level=1)         # set RTS line to specified logic level
SETDTR (level=1)         # set DTR line to specified logic L Evel Getcts () "Return" the state of the                CTS line
getdsr ()                #
return the state of the DSR line Getr I () # Return the ' state of the '                 RI line
Getcd () # Return the ' state of the '                 CD line

Attributes of serial instancesRead only:
. text. Imp {font-weight:bold; color:red;}
PORTSTR                 # Device name
baudrates               # List of valid baudrates
bytesizes               # List of valid byte sizes
PA Rities                # List of valid parities
stopbits                # List of valid stop bit widths
New values can be assigned to the following attributes, the port'll be reconfigured, even if it's opened at this time:

. text. Imp {font-weight:bold; color:red;}
Port                    # port Name/number as set by the user
baudrate                # current baud rate setting
bytesize                # byte size I N bits
parity                  # parity setting
stopbits                # Stop bit with (1,2)
Timeout                 # timeout setting
Xonxoff                 # If Xon/xoff flow control are enabled
rtscts                  # If hardware flow control is enabled

Exceptions. text. Imp {font-weight:bold; color:red;}
Serial. Serialexception

ConstantsParity
. text. Imp {font-weight:bold; color:red;}
    Serial. Parity_none
Serial. Parity_even
Serial. Parity_odd
StopBits:
. text. Imp {font-weight:bold; color:red;}
    Serial. Stopbits_one
Serial. Stopbits_two
ByteSize:
. text. Imp {font-weight:bold; color:red;}
    Serial. Fivebits
Serial. Sixbits
Serial. Sevenbits
Serial. Eightbits
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.