Difference between revisions of "PySerial Simulator"
(Created page with "--~~~~ ---- <bluebox> 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 developi...") |
|||
Line 7: | Line 7: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =PySerial= | ||
<br /> | <br /> | ||
+ | The first examples of a real PySerial program are taken from [http://pyserial.sourceforge.net/shortintro.html http://pyserial.sourceforge.net/shortintro.html], and are reproduced below: | ||
<br /> | <br /> | ||
+ | ==Example 1== | ||
<br /> | <br /> | ||
+ | <source lang="python"> | ||
+ | import serial | ||
+ | ser = serial.Serial(0) # open first serial port | ||
+ | print ser.name # check which port was really used | ||
+ | ser.write("hello") # write a string | ||
+ | ser.close() # close port | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Example 2== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | >>> 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() | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Example 3== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | >>> 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 | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Example 4 == | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | >>> 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 | ||
+ | </source> | ||
+ | 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 (fictitius) | ||
+ | # 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. | ||
+ | <br /> | ||
+ | Below is our first attempt at building this module. | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
[[Category:Tutorials]][[Category:Python]][[Category:Arduino]] | [[Category:Tutorials]][[Category:Python]][[Category:Arduino]] |
Revision as of 13:09, 21 April 2014
--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, but these functions either do not do anything, or returned canned, predefined data.
PySerial
The first examples of a real PySerial program are taken from http://pyserial.sourceforge.net/shortintro.html, and are reproduced below:
Example 1
import serial
ser = serial.Serial(0) # open first serial port
print ser.name # check which port was really used
ser.write("hello") # write a string
ser.close() # close port
Example 2
>>> 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()
Example 3
>>> 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
Example 4
>>> 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
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 (fictitius)
- 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.
Below is our first attempt at building this module.