A quick and easy introduction to how to use the RevPi with the Python programming language.
Why Python?
Python is simple and straightforward
IEEE study # 1 programming language 2018 worldwide
Documentation
Start interactive Python shell
My recommendation use uses the ipython IPython interactive shell as it supports tabbed auto completition
Code Block |
---|
sudo apt install ipython3
ipython3 |
LED switch with Python
Works like writing a "1" to the location in the process image where the RevPiLED is located
Where is the RevPiLED located in the process image?
Run the following command
Code Block |
---|
piTest -v RevPiLED |
Example with Python
Code Block |
---|
|
# ledswitch1.py
f=open("/dev/piControl0","wb+",0)
f.seek(119)
f.write(b'1')
f.seek(119)
f.write(b'0') |
Read or write strings
The devices in PiCtory usually provide byte access to the data. You can test this example by using a Virtual Device 32 Bytes in PiCtory.
...
Info |
---|
We read out the offset of “Output_1” variable. This may vary on your configuration, so be careful here. |
Code Block |
---|
|
pi@RevPi40302:~ $ piTest -v Output_1
variable name: Output_1
offset: 43
length: 8
bit: 0 |
Python example code
Code Block |
---|
|
def writeString(offset, myString):
f=open("/dev/piControl0","wb+",0)
f.seek(offset)
f.write(str.encode(myString))
def readString(offset, length):
f=open("/dev/piControl0","wb+",0)
f.seek(offset)
my_string_as_bytes = f.read(length)
return my_string_as_bytes.decode()
writeString(43,'Hello World')
readString(43,11)
' Hello World |
Verify the contents of the process image with piTest
Code Block |
---|
|
pi@RevPi40302:~ $ piTest -1 -r 43,11
72 101 108 108 111 32 87 111 114 108
100
pi@RevPi40302:~ $ piTest -1 -r 43,11,h
48 65 6c 6c 6f 20 57 6f 72 6c 64 |
RevPiModIO
Library developed from the RevPi community
Allows easy use of symbolic names from PiCtory
Installation not necessary, already on the device
Documentation
Switch the LED with RevPiModIO
Code Block |
---|
|
import revpimodio2
rpi = revpimodio2.RevPiModIO(autorefresh=True)
rpi.io.RevPiLED.value = 1
rpi.io.RevPiLED.value = 0
rpi.cleanup() |
Python I/O Bit Mirror
Code Block |
---|
|
import revpimodio2
import time
rpi = revpimodio2.RevPiModIO(autorefresh=True)
while True:
rpi.io.O_1.value = rpi.io.I_1.value
time.sleep(0.02)
revpi.cleanup() |
Video Tutorials
Also have , take a look at our video tutorials about Python
Also have Besides, take a look at the following video tutorials
Verwandte Artikel
Filter by label (Content by label) |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | com.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@968 | showSpace | false |
---|
sort | modified |
---|
typeshowSpace | pagefalse |
---|
reverse | true | labels |
---|
kb-how-to-articletype | page | cql | label = "kb-how-to-article" and type = "page" and space = "KS" |
---|
labels | kb-how-to-article |
---|
|
Page Properties |
---|
|
Verwandte Vorgänge | Jira Legacy |
---|
serverId | 6a616e0c-dd66-30e3-95b5-0fa763638e21 |
---|
key | SUP-1136 |
---|
|
|
---|
|