Isaac Barona (email@example.com)
USPP Library is a multi-platform Python module to access serial ports. At the moment, it only works in Windows, Linux and MacOS but as it is written entirely in Python (doesn't wrap any C/C++ library) I hope you can extend it to support any other platforms.
I like very much to make electronic widgets with microcontrollers, specially those that can be connected to the computer to send and receive data.
Some months ago, I discovered Python and inmediatelly liked it a lot. I started playing with it and saw that I could use it to make prototypes of comunication protocols between computer and microcontrollers really fast and easily than using C. At the same time, I was interested in working over different platforms.
I started looking for Python modules to access serial port and I found the following projects:
- win32comport_demo from the win32 extension module
- win32comm module of firstname.lastname@example.org
- Sio Module of Roger Rurnham (email@example.com)
- pyxal (Python X10 Abstraction Layer) of Les Smithson (firstname.lastname@example.org)
but they were not multi-platform, were just a wrap of propietary libraries or were just simple examples of serial port access.
For these reasons and also for learning more Python, I decided to start this project. Of course, I have used all this projects as reference to my module and so, I want to thanks the authors for their excellent work and for allowing us to study the code. I hope you enjoy using the uspp module as much as I am enjoying doing it.
This module has the following features:
- hight level access to serial port under several platforms.
- autodetects the platform in which it is working and exports the correct classes for that platform.
- object oriented approach.
- file object semantic operations over the ports.
- allows using the serial port with different speeds and characteristics.
- RS-232 and RS-485 modes (now only RS-232). In RS-485 mode the communication is half-duplex and uses the RTS line to control the direction of the transference.
- blocking, non-blocking or configurable timeout reads.
You need the following to use the library:
- Python 2.1 or better
- In windows you need the win32 extension modules. You can get it from here.
You only have to import in your program the uspp module and automatically it loads the correct classes for the platform in which you are running the program.
First of all you have to create a SerialPort object with the settings you want. If a SerialPortException is not generated then you just can use the read and write methods of the object to read and write to the serial port.
>>> from uspp import *
>>> tty=SerialPort("COM2", 1000, 9600)
>>> # Opens COM2 at 9600 bps and with a read timeout of 1 second.
>>> tty.write("a") # Writes a character to the COM2 port
>>> # Now suppose we receive the string "abc"
Documentation of the different classes and methods can be found on uspp module docstring.
The library has been tested in Windows 95, Windows XP and Windows 2000 machines with Python 2.0+ and in a Linux (2.0.34 and 2.4.20 kernels) machine with Python 2.1+.
If you want to port the library to other platforms you only have to follow these steps:
- Create a new python file called SerialPort_XXXX.py in which you implement the same public classes and methods found in the SerialPort_win and SerialPort_linux modules.
- Append the new platform to the uspp.py file.
This code is released under the "LGPL" that can be found in http://www.gnu.org/copyleft/lesser.html or in the lesser.txt file that is with the library.
If you use this software, I'd like to know about it.
This library has been created by Isaac Barona Martínez (ibarona at gmail.com)
Lot to time after 0.1 version I have enouth time to publish the final version of the library. This version correct some bugs of 0.1 version, works in MacOS and have been extended.
The library contains more functions in Linux and MacOS platforms than in Windows platform.
I have to thanks a lot of people his contribution to the library, especially:
- Damien Geranton <dgeranton at voila.fr>
- Douglas Jones <dfj23 at drexel.edu>
- J.Grauheding <juergen.grauheding at a-city.de>