This example outline how to interface any external rs-232 device from python
using the pyserial module. Before using this python extension module your
computer must be configured properly. To use pyserial the following software
components must be installed after installing Scorpion.
- Install Python
- Install Python for Windows Extensions
- Install pyserial
Scorpion 3.0 is compatible with Python 2.3.3 which is compatible with
win32all-163 again compatible with pyserial-1.20.
All compatible set of components are located on the Scorpion CD -
Python section.
It is also available at the Scorpion support web -
http://scorpion.tordivel.no
Example:
The following code may be tested from IDLE or PythonWin
PythonWin is installed with
Python for Windows extensions, - very useful for debugging and testing of
scripts.
from serial import *
c=Serial(0)
c.write('com port support from Python')
if c.inWaiting():
print c.read( c.inWaiting() ),'characters read'
else:
print 'no reply'
c.close()
The code opened COM1 @ default setting 9600,8,N,0
from serial import *
c=Serial(0,19200,8,'E',1)
# com port opened at 19200 baud, 8 databits, even parity, 1 stopbit
c.close()
Type help(Serial) in PytonWin or IDLE and you will get help on the
syntax.
Hint : ASCII data and Python
A few words on ASCII data and Python.
use '\x03' for generating control characters or the chr() function.
make up your string to be
str='\x0a'+'\x0b'+chr(13)
or whatever.
Integrating pyserial in Scorpion
The serial script are normally added to the Central section of Scorpion
in Central.Start: # initialise
from serial import *
com=Serial(0)
in Central.Stop: # terminate
com.close()
in a PythonTool or user defined script
mystr = 'something'
com.write( mystr )
|