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.
- 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.librpiplc-devprovides header files and symlinks required for compilation
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:
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
Compilation example
To compile a program for the Raspberry PLC 38AR V6:
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 functioninitExpandedGPIO()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 themain()function, which initializes the I/O peripherals, callssetup()once and then callsloop()repeatedly.
Blinking a digital output
Arduino-like program that toggles the digital output Q0.0 every second:
| blink.cpp | |
|---|---|
Use these commands to compile it and run it on a Raspberry PLC, in this case a 38AR V6:
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:
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¶
-
Source code, librpiplc: https://github.com/Industrial-Shields/librpiplc
The library itself, it defines the pin names of every device and provides the functions used to read and write its inputs and outputs.
-
Source code, plc-peripherals: https://github.com/Industrial-Shields/plc-peripherals
The lower level layer used by librpiplc to communicate with the I/O peripherals of the device.