SimpleComm library for Raspberry Pi PLC

Use our SimpleComm library to easily communicate multiple PLCs
January 2, 2024 by
SimpleComm library for Raspberry Pi PLC
Boot & Work Corp. S.L., Joan Vilardaga Castro

SimpleComm: Bridging Arduino & Raspberry Pi in PLCs

One of the libraries we provide with our PLCs is the C++ SimpleComm library. This library allows you to send data via any data stream: RS-485, RS-232, Ethernet... The flexibility of this library allows it to adapt to different communication typologies such as Ad-Hoc, Master-Slave and Client-Server, all with an intuitive and user-friendly API for Arduino programmers.

However, this library was originally designed for Arduino Streams, which meant that it was not directly compatible with our Raspberry Pi projects. To overcome this limitation, we took the initiative to adapt the Stream Class to the Linux environment. This allowed us to successfully integrate the library into our Raspberry range with minimal modification, and now users can take full advantage of the library for seamless data transfer between our PLCs.

Woman using SimpleComm on a computer

How to install SimpleComm for Raspberry Pi PLC?

1. Download

the library from the GitHub repository as a "ZIP" file.

2. Decompress

the file and open the directory with the terminal.

3. Type

the command 'make install', and you are done.

SimpleComm usage

SimpleComm usage

To store data in a packet you can use the setData function, which supports a wide range of data types, including bool, char, unsigned char, int, unsigned int, long, unsigned long, double, string, and even custom data types. Additionally, the SimplePacket class includes "getter" functions designed to retrieve the data stored within the packet, catering to specific data types. If maximum length is specified, it returns the data length in bytes.

The SimpleComm singleton is the interface for sending and receiving packets through the desired data stream. The communication system is initiated and configured using the begin(address) function. This function not only activates the communication process but also establishes a unique identifier or address for each device. Each device has its own unique address, allowing it to selectively receive packets destined for its address, while disregarding packets meant for other devices. This address-based mechanism ensures secure and efficient data exchange within the system. 

The following methods can be used to send a packet to a destination:

	​SimpleComm.send(stream, packet, destination);
	SimpleComm.​send(stream, packet, destination, type);

The function also accepts a packet type, which serves the purpose of informing the receiver about how to interpret and read the contents of the packet accurately.

The receive function is responsible for fetching a packet from another device through the designated stream:

	SimpleComm.​r​eceive(stream, rxPacket);

If a packet is indeed received, the function returns true, indicating successful reception; otherwise, it returns false.

What is Stream class?

The Stream class is fundamental for communication in Arduino, as it is an abstraction that allows standardised handling of data streams across different communication interfaces. Because SimpleComm was designed for Arduino, it relies heavily on the Stream Class as its core communication abstraction. To make the library compatible with Linux and retain its functionality seamlessly, we focused on adapting the Stream class from the Arduino world into the Linux world.

Basically, the Stream class is an abstract class that implements three virtual functions:

  • std::size_t write(const uint8_t* data, std::size_t count)
  • int read(void)
  • std::size_t available(void)

These functions have the same signature as the ones used by the Stream Class, so the SimpleComm library can use all the derived classes from it without any modifications.

Data Streams across different communication

FileStream: Elevating file communication with advanced stream capabilities  

The FileStream class is a specialized class that extends the capabilities of the Stream class, specifically designed to implement essential functions like "write()", "read()", and "available()" for C++ fstream objects. This adaptation enables users to use file-based communication methods, such as pipes (IPC) or other devices like UART and RS-485.

A FileStream object can be created using either a C-String or a String Object that contains the name of the file:

FileStream stream = FileStream("/dev/ttySC0");
// ...
// ...
uint8_t destination = 0;
SimpleComm.send(stream, packet, destination);

SockStream: Enhancing network communication with Stream class extension

SockStream is a specialized class that extends the Stream class to implement the "write()", "read()", and "available()" functions for C Sockets. This class makes possible using SimpleComm with sockets, like for example TCP/IP or UNIX sockets.

A SockStream object can only be created using the socket file descriptor:

int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
// connect(...)
SockStream stream = SockStream(socket_desc);
// ..
uint8_t destination = 0;
SimpleComm.send(stream, packet, destination);

Compatibility between architectures

Communicate different CPU architectures

This library relies on standard C++ types (e.g., unsigned long, int), which can function correctly if the communicating architectures maintain consistent type sizes. However, problems may arise if you try to communicate different CPU architectures, such as ESP32 and Raspberry. The C++ types that are defined in each architecture have different sizes, which will cause communication errors.

To ensure proper communication between different architectures and to address potential type size issues, the library provides a solution through the "SimplePacketConfig.h" header file. This file allows users to customize the types used in the library, thereby fixing the size of the types for proper communication:

  • If you uncomment "#define UNIVERSAL_CPP" the types used will be the minimum size according to the C++ standard.
  • If you uncomment "#define CUSTOM_TYPES", the types used will be the size of what you define.

SimpleComm: A Comprehensive Solution for PLC Communication with Raspberry Pi and Arduino

The SimpleComm library stands out as a comprehensive tool for PLC communication, combining Raspberry Pi and Arduino compatibility with easy installation and an intuitive API. Its adaptation of the Stream Class to the Linux environment, together with the extended FileStream and SockStream classes, ensures flexible and secure data transfer over multiple protocols. This library is positioned as a key solution for programmers in the field of automation and process control, offering efficiency and versatility in communication between different CPU architectures.

​Search in our Blog

SimpleComm library for Raspberry Pi PLC
Boot & Work Corp. S.L., Joan Vilardaga Castro January 2, 2024
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

Industrial PLC comparison >>>