To simplify accessing I/O when you write your own code, Opto 22 engineers wrote a Python script that your code can reference.
-
If you’re using a browser, click here for the script.
-
To download the script to the current directory using a terminal, enter
wget http://developer.opto22.com/downloads/Opto22.PI.py
To print the working directory, enter
pwd
To view the script, enter
more Opto22.PI.py
The script references an I/O module’s rack position to read and write to its point, and to configure its associated GPIO pin. It also contains comments to help you understand what it does.
How to run Opto22.PI.py
Open a terminal, and then run the script using this syntax:
sudo python Opto22.PI.py [module number] [command] [value]
- [module number] : Rack position of the target I/O module (0 through 15).
-
[command]
:
- blink makes the GPIO pin blink.
- config configures the I/O module's associated GPIO pin as either input or output.
- read reads the status of the I/O point.
- write turns an output point On or Off.
-
[value]
:
-
If [command] is blink, for [value], type the number of seconds to blink. The minimum value is 2 (blink on for 1 second; blink off for 1 second).
- If [command] is config, [value] is
input
oroutput
.
You must configure the GPIO pin to match the I/O module's type.- If the GPIO pin is mapped to an input module, enter
input
for the value. - If the pin is mapped to an output module, enter
output
for the value.
- If the GPIO pin is mapped to an input module, enter
-
If [command] is read, you don't need the [value] argument. Leave it blank.
- If [command] is write, [value] is
1
for On or0
for Off.
Important: The Opto22.PI.pi script is coded to use 1 for On and 0 for Off. -
Examples for LXTerminal
config
option to configure the GPIO pin’s direction to match the type (input or output) of its associated the I/O module.Configure the GPIO pin mapped to rack position 0 as input
sudo python Opto22.PI.py 0 config input
Read the I/O module at rack position 3
sudo python Opto22.PI.py 3 read
Turn Off the I/O module at rack position 15
(Remember that in Opto22.PI.py, 1 = On and 0 = Off)
sudo python Opto22.PI.py 15 write 0