Skip to content

C/C++ programming

Industrial Shields Raspberry Pi-based devices can be programmed using the C/C++ language programming.

This is achieved through the librpiplc library, which provides APIs for accessing inputs/outputs. It also contains some adaptations based on the code structure of the Arduino IDE to make it more familiar for users used to its environment.

Library Install

Default OS image

The C 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 first step to programming the PLC is installing the necessary library: librpiplc. The recommended way of installing it 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.
    • librpiplc-dev provides header files and symlinks required for compilation
    sudo apt install librpiplc librpiplc-dev
    

Building the library

The librpiplc library can also be built from source. Follow the instructions provided in the official repository: https://github.com/Industrial-Shields/librpiplc

Usage

Program Compilation

Programs using the librpiplc must be compiled with g++, linking the library and specifying the characteristics of the target device like this:

g++ -o file file.cpp -l rpiplc -D PLC_VERSION -D 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

Compilation example

To compile a program for the Raspberry PLC 38AR V6:

g++ -o file file.cpp -l rpiplc -D RPIPLC_V6 -D RPIPLC_38AR

Basic Examples

The librpiplc API is intentionally close to the Arduino one: pins are configured with pinMode() and then read or written with digitalRead(), digitalWrite(), analogRead() and analogWrite(). The pin names (I0_0, Q0_0, A0_5, ...) match the labels printed on the device and are provided by the mapping selected with the PLC_VERSION and PLC_MODEL macros described above.

There are two ways of structuring a program:

  • A regular C/C++ program with its own main(), which must call the function initExpandedGPIO() at the start, before using any I/O.
  • An Arduino-like program, obtained by defining the __ARDUINO_FUNCTIONS__ macro before including <rpiplc.h>. The library then provides the main() function, which initializes the I/O peripherals, calls setup() once and then calls loop() repeatedly.

Blinking a digital output

Arduino-like program that toggles the digital output Q0.0 every second:

blink.cpp
#define __ARDUINO_FUNCTIONS__
#include <rpiplc.h>

void setup() {
    pinMode(Q0_0, OUTPUT);
}

void loop() {
    digitalWrite(Q0_0, HIGH);
    delay(1000);
    digitalWrite(Q0_0, LOW);
    delay(1000);
}

Use these commands to compile it and run it on a Raspberry PLC, in this case a 38AR V6:

g++ -o blink blink.cpp -l rpiplc -D RPIPLC_V6 -D RPIPLC_38AR
./blink

Reading inputs and writing an analog output

Program with its own main(), which reads the digital input I0.0 and the analog input I0.7, and mirrors the analog value on the analog output A0.5:

inputs.cpp
#include <cstdio>
#include <rpiplc.h>

int main() {
    if (initExpandedGPIO(false) < 0) {
        fprintf(stderr, "initExpandedGPIO failed\n");
        return -1;
    }

    pinMode(I0_0, INPUT);
    pinMode(I0_7, INPUT);
    pinMode(A0_5, OUTPUT);

    while (true) {
        int digital = digitalRead(I0_0);
        uint16_t analog = analogRead(I0_7);

        printf("I0.0: %d    I0.7: %u\n", digital, analog);

        analogWrite(A0_5, analog);

        delay(500);
    }

    return 0;
}
Use these commands to compile it and run it on a Raspberry PLC, in this case a 38AR V6:

g++ -o inputs inputs.cpp -l rpiplc -D RPIPLC_V6 -D RPIPLC_38AR
./inputs

Notes and Best Practices

  • Ensure your application is compiled with the correct PLC version and model macros.
  • Initialize the I/O only once, at the beginning of the program, before using any pin.
  • Pins must always be configured with the according mode at the start of the program, using the pinMode() function.
  • You can check the value returned by the library functions to detect I/O errors.
  • Run only one program controlling the I/O of the device at a time, since the library is not thread-safe.

References