Welcome to pySerial’s documentation¶. This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. Recently I needed to reverse-engineer the serial communication between a solar inverter and a computer. Socat definitely works well for communicating with serial ports, dumping data from serial ports to files, etc. I needed to also write tests for a piece of software that I was writing in python.
Overview
This module encapsulates the access for the serial port. It provides backendsfor Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliantsystem) and IronPython. The module named 'serial' automatically selects theappropriate backend.
- Project Homepage: https://github.com/pyserial/pyserial
- Download Page: https://pypi.python.org/pypi/pyserial
BSD license, (C) 2001-2017 Chris Liechti <[email protected]>
Documentation
For API documentation, usage and examples see files in the 'documentation'directory. The '.rst' files can be read in any text editor or being converted toHTML or PDF using Sphinx. An HTML version is online athttps://pythonhosted.org/pyserial/
Examples
Examples and unit tests are in the directory examples.
Installation
pip install pyserial
should work for most users.Detailed information can be found in documentation/pyserial.rst.
The usual setup.py for Python libraries is used for the source distribution.Windows installers are also available (see download link above).
or
To install this package with conda run:
conda install -c conda-forge pyserial
conda builds are available for linux, mac and windows.
--D. Thiebaut (talk) 13:45, 21 April 2014 (EDT)
The purpose of this tutorial is to illustrate the basic steps required to build a Python module that can be used as a PySerial replacement while developing a Python application that interfaces with an Arduino. The module presented here supports the basic PySerial functions one needs to call from a Python app to talk to an Arduino. These functions, however, either do not do anything, or return canned and predefined data.
|
First we look at how the real PySerial module behaves when supporting a Python program that interacts with an Arduino. We pick several examples from the PySerial documentation.
These examples of a real PySerial program are taken from http://pyserial.sourceforge.net/shortintro.html, and are reproduced below for completeness:
Example 1
Example 2
Example 3
Example 4
So, all we have to do is create a module called fakeSerial.py that will contain
- a class called Serial() that can be initialized with various amount of arguments
- a member variable of this class should be called name and return the name of a port.
- a method called write( ) which receives a string and passes it to the fake Arduino
- a method called read() which will read some number of bytes from the Arduino
- a method called close() that closes the port and make all further operations with the Arduino impossible.
- a method called isOpen() which will return True or False depending on whether the port to the fake Arduino is opened or closed.
- a method called readline() that will return characters until a n is found.
Below is our first attempt at building this module.
Below is our test program that uses the simulator and runs the same tests we listed at the top of the page. Note that the output is the same as with a real Arduino!
The output of the program is shown below:
We have developed a module that can be used as an Arduino simulator and that will mimic how an Arduino would respond to various commands. This simulator is useful for developing Python programs that will eventually communicate with Arduino hardware. The program won't have to worry about connecting hardware to the development computer until the very last step of the development process.
With such a simulator the Python program can be quickly developed, debugged, and fine-tuned before connecting to the real thing. If more sophisticated communication with the Arduino simulator is required, then the write(), read() and readline() methods of the simulator can be modified to yield more complex behavior.
Retrieved from 'http://www.science.smith.edu/dftwiki/index.php?title=PySerial_Simulator&oldid=21354'