Using gRPC communication in PLCnext Technology

Short introduction to gRPC

gRPC is a modern open source high performance Remote Procedural Call (RPC) framework. Initially developed at Google and now lead by the Cloud Native Computing Foundation. It is very flexible and user-friendly, it can easily put in communication different services independently on the programming language used. For more information visit grpc.io

As a communication layer gRPC uses HTTP2, while it uses protocol buffers as serialization/deserialization and interface definition language.

A client can initiate a read request and a write request. Also a subscription to server push on variable change is available. The read/write request and response are defined in the .proto files that you can download from our gRPC GitHub repository.

The .proto files are used to automatically generate the code needed for the communication in almost any programming language.

gRPC in PLCnext Technology

In PLCnext Technology we have some interfaces to communicate, control, and exchange data with the PLCnext Runtime System. But some of them are proprietary, others are limited in functionality, and some are limited to a single programming language.

With gRPC we add another interface used for inter process communication (IPC), and in future also for remote procedural call (RPC) without all the limitations.

Using a full-featured RPC framework brings a small latency overhead for local IPC. But that overhead is outweighed by the benefits:

  • using a single library for IPC and RPC only (RPC being an upcoming feature)
  • having well-defined, strongly-typed interfaces. 

Those interfaces are checked for consistency at compile-time and can also be used to communicate with your containerized application.

gRPC extends the C++ RSC service interface by an open source and programming-language-independent protocol. This implements the widest range of functionalities in PLCnext Technology and will get further enhancements in future.

How to use gRPC

In the PLCnext firmware a locally accessible server is running by default.

Clients can connect via a Unix Domain Socket file path to it. Following are some examples how to establish a communication channel. More information for all supported languages can be found on grpc.io/docs/languages.

Python example:

domain_socket="unix:/var/run/plcnext/grpc.sock"
channel=grpc.insecure_channel(domain_socket)

For reading and writing process data with Python, see the proposal in the PLCnext Community's Maker's Blog.

 

C# example:

var udsEndPoint = new UnixDomainSocketEndPoint("/run/plcnext/grpc.sock");
var connectionFactory = new UnixDomainSocketConnectionFactory(udsEndPoint);
var socketsHttpHandler = new SocketsHttpHandler
{
	ConnectCallback = connectionFactory.ConnectAsync
};
using var channel = GrpcChannel.ForAddress("http://localhost", newGrpcChannelOptions
{
	HttpHandler = socketsHttpHandler
});

 

OCI containers:

For the communication between the host and a client inside a container (e.g. Docker), the socket file just needs to be volume-mounted, and then used accordingly from the client inside the container.

Quick start

Quick start guides for all supported programming languages are provided at the official gRPC website.

A first PLCnext Technology related example can be found in the PLCnext Community's Makers' Blog article How to create a client for the PLCnext Control gRPC server in C#.

PLCnext C++ RSC API adaptation

Find the protobuf interface description files on our gRPC GitHub repository.

Tip: For a quick overview of available RSC .proto files, see the third column in the RSC documentation reference in this Info Center.

The gRPC interface is similar to the C++ RSC interface and does accept the same parameters.  Read more in the RSC (Remote Service Calls) branch or in the API documentation.

RSC services supported

See the .proto column in the RSC documentation reference table. More services will be implemented over time.

 


• Published/reviewed: 2024-02-27   ★  Revision 065 •