Python programming¶
Industrial Shields Raspberry Pi-based devices can also be programmed using the Python 3 programming language.
This is achieved through the python3-librpiplc library, a Python wrapper of the C library librpiplc, which provides APIs for accessing inputs/outputs.
Library Install¶
Default OS image
The Python library is already preinstalled in the default OS images provided by Industrial Shields. No additional installation is required when using these images.
Requirements
Before installing the library, ensure that:
- You are using a Raspberry Piābased Industrial Shields device.
- The device is running Raspberry Pi OS.
- You have internet access on the PLC.
The Python library is a wrapper of the C one, so both of them must be installed. The recommended way of installing them is using APT.
- Add the Industrial Shields repository as a source:
# Set the debian repository URL as apt source printf "\n\ndeb https://apps.industrialshields.com/main/DebRepo/ ./" | sudo tee -a /etc/apt/sources.list > /dev/null # Download and install the GPG key wget -O - https://apps.industrialshields.com/main/DebRepo/PublicKey.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/IndustrialShieldsDebian.gpg > /dev/null # Update repositories sudo apt update -
Install the following packages:
librpiplc: contains the shared library binaries used by the Python wrapper.python3-librpiplc: provides the Python module.
Installing the library from source
The python3-librpiplc library can also be installed from its source code, using pip. Follow the instructions provided in the official repository: https://github.com/Industrial-Shields/python3-librpiplc
Usage¶
Importing the Library¶
The library must be imported and initialized with the characteristics of the target device before using any input or output. This is done once, at the beginning of the program:
Where PLC_VERSION and PLC_MODEL are the version and model of the
target device. The available options are:
PLC_VERSION:RPIPLC_V3(deprecated)RPIPLC_V4RPIPLC_V6UPSAFEPI_V6GATEBERRY_V9TOUCHBERRY_PI_V1
PLC_MODEL:RPIPLC_CPURPIPLC_19RRPIPLC_21RPIPLC_38ARRPIPLC_38RRPIPLC_42RPIPLC_50RRARPIPLC_53ARRRPIPLC_54ARARPIPLC_57AARRPIPLC_57RRPIPLC_58UPSAFEPIGATEBERRYTOUCHBERRY_PI
Initialization example
To initialize the library on a Raspberry PLC 38AR V6:
Basic Examples¶
The python3-librpiplc API is intentionally close to the Arduino one:
pins are read and written with digital_read(), digital_write(),
analog_read() and analog_write(). Each pin is identified by the
name printed on the device, given as a string ("I0.0", "Q0.0",
"A0.5", ...), and the available pins depend on the version and the
model set on initialization.
Blinking a digital output
Program that toggles the digital output Q0.0 every second:
| blink.py | |
|---|---|
Run it on the device with this command:
Reading inputs and writing an analog output
Program that reads the digital input I0.0 and the analog input I0.7, and mirrors the analog value on the analog output A0.5:
Run it on the device with this command:
Notes and Best Practices¶
- Ensure the library is initialized with the correct PLC version and model names.
- Initialize the library only once, at the beginning of the program, before using any pin.
- Make sure to properly initialize the used pins with the
rpiplc.pin_mode()function. - You can check the value returned by the library functions to detect I/O errors.
- Deinitialize the library with
rpiplc.deinit()before exiting, so the outputs of the device are left in a known state. - Run only one program controlling the I/O of the device at a time, since the library is not thread-safe.
References¶
-
Source code, python3-librpiplc: https://github.com/Industrial-Shields/python3-librpiplc
The Python library: it wraps the C library and exposes its functions as the
rpiplcmodule. -
Source code, librpiplc: https://github.com/Industrial-Shields/librpiplc
The C library used underneath, which defines the pin names of every device and performs the actual reads and writes.