Skip to content

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.

  1. 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
    
  2. Install the following packages:

    • librpiplc: contains the shared library binaries used by the Python wrapper.
    • python3-librpiplc: provides the Python module.
    sudo apt install librpiplc python3-librpiplc
    

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:

from librpiplc import rpiplc

rpiplc.init("PLC_VERSION", "PLC_MODEL")

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_V4
    • RPIPLC_V6
    • UPSAFEPI_V6
    • GATEBERRY_V9
    • TOUCHBERRY_PI_V1
  • PLC_MODEL:
    • RPIPLC_CPU
    • RPIPLC_19R
    • RPIPLC_21
    • RPIPLC_38AR
    • RPIPLC_38R
    • RPIPLC_42
    • RPIPLC_50RRA
    • RPIPLC_53ARR
    • RPIPLC_54ARA
    • RPIPLC_57AAR
    • RPIPLC_57R
    • RPIPLC_58
    • UPSAFEPI
    • GATEBERRY
    • TOUCHBERRY_PI

Initialization example

To initialize the library on a Raspberry PLC 38AR V6:

rpiplc.init("RPIPLC_V6", "RPIPLC_38AR")

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
from librpiplc import rpiplc

rpiplc.init("RPIPLC_V6", "RPIPLC_38AR")

rpiplc.pin_mode("Q0.0", rpiplc.OUTPUT)

try:
    while True:
        rpiplc.digital_write("Q0.0", rpiplc.HIGH)
        rpiplc.delay(1000)
        rpiplc.digital_write("Q0.0", rpiplc.LOW)
        rpiplc.delay(1000)
except KeyboardInterrupt:
    rpiplc.deinit()

Run it on the device with this command:

python3 blink.py

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:

inputs.py
from librpiplc import rpiplc

rpiplc.init("RPIPLC_V6", "RPIPLC_38AR")

rpiplc.pin_mode("I0.0", rpiplc.INPUT)
rpiplc.pin_mode("I0.7", rpiplc.INPUT)
rpiplc.pin_mode("A0.5", rpiplc.OUTPUT)

try:
    while True:
        digital = rpiplc.digital_read("I0.0")
        analog = rpiplc.analog_read("I0.7")

        print(f"I0.0: {digital}    I0.7: {analog}")

        rpiplc.analog_write("A0.5", analog)

        rpiplc.delay(500)
except KeyboardInterrupt:
    rpiplc.deinit()

Run it on the device with this command:

python3 inputs.py

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