In contemporary industrial automation systems, real-time monitoring and data analysis have become crucial. This article introduces how to achieve efficient data collection and storage from PLC devices in industrial control systems by integrating Apache PLC4X and CnosDB, providing engineers with more powerful data analysis and monitoring tools.
Definition of PLC
PLC stands for Programmable Logic Controller, an industrial computer used for automation control. It is widely used in the industrial field to monitor and control mechanical equipment and processes during production. It replaces traditional relay logic control systems and executes various control tasks programmatically.
Main Characteristics and Functions of PLC
- Programmability: Implement different control logics through programming to adapt to various industrial processes.
- Real-time: Monitor and respond to changes in the production process in real time.
- Reliability: Durable, stable, and capable of working in harsh environments.
- Versatility: Suitable for different types of control tasks such as logic control, motion control, and process control.
Introduction of PLC4X:
PLC4X (Apache PLC4X) is an open-source project aimed at providing a flexible, scalable toolkit for communication with various Programmable Logic Controllers (PLC) in the field of industrial automation. It simplifies the complexity of interacting with PLCs of different brands and models, making it easier for developers to integrate PLC data into their applications.
The project is supported by the Apache Software Foundation, written in Java, and offers APIs in multiple languages, including Java, Python, and JavaScript. PLC4X supports a variety of communication protocols, such as Modbus, OPC UA, Siemens S7, etc., enabling it to communicate with various PLC devices.
Overall, PLC4X aims to provide an open, standardized interface for industrial automation systems, making it easier to integrate and manage different types of PLCs.
Introduction of CnosDB:
CnosDB is an open-source database focused on time-series data storage. Its high performance and flexible query language make it an ideal data storage solution for industrial control systems, especially in scenarios requiring real-time monitoring and data analysis.
Here is an example of implementing PLC4X integration with CnosDB using the Java language. In the example, we will use the PLC4X library to read data from a virtual PLC device and then write it to CnosDB.
First, configure the connection to CnosDB:
// Configure the connection to CnosDB
String cnosDBUrl = "http://127.0.0.1:8902";
String tenant = "cnosdb";
String databaseName = "public";
String username = "root";
String password = "";
String measurement = "plc4x";
String apiUrl = cnosDBUrl + "/api/v1/write?db=" + databaseName + "&tenant=" + tenant + "&pretty=true";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
String authHeaderValue = "Basic " + new String(encodedAuth);
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", authHeaderValue);
connection.setDoOutput(true);
Next, choose the connection address of the PLC device to be read, using the virtual address provided by the PLC4X library in the example:
String plcConnectionString = "simulated://127.0.0.1";
Read random data from the virtual address, replacing it with the real PLC device's address in actual projects. Use a Read request to read a random integer from that address, then write it into the CnosDB database:
try (PlcConnection plcConnection = PlcDriverManager.getDefault().getConnectionManager().getConnection(plcConnectionString)) {
String field = "foo";
PlcReadRequest readRequest = plcConnection.readRequestBuilder()
.addTagAddress(field, "RANDOM/foo:INT") // Replace with your PLC address
.build();
PlcReadResponse response = readRequest
.execute()
.get();
int fieldValue = response.getInteger(field);
String line = String.format("%s %s=%di",measurement, field, fieldValue);
System.out.println(line);
connection.getOutputStream().write(line.getBytes());
System.out.println(connection.getResponseMessage());
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
After running the example code, you can query the CnosDB database to confirm that the data has been successfully written.
public ❯ show tables;
+------------+
| table_name |
+------------+
| plc4x |
+------------+
Query took 0.089 seconds.
public ❯ select * from plc4x;
+----------------------------+------+
| time | foo |
+----------------------------+------+
| 2024-01-04T08:27:23.566444 | 3625 |
+----------------------------+------+
Query took 0.024 seconds.
public ❯
By integrating PLC4X with CnosDB, engineers can more easily establish efficient real-time industrial control systems. This integration not only simplifies data collection but also provides users with a powerful time-series database, laying a solid foundation for the next step in the development of industrial automation.
The code examples and integration steps provided in this article aim to help community members use PLC4X and CnosDB more easily, promoting the widespread application of open-source tools in the field of industrial automation. Through this integration, we believe it is possible to drive innovation in industrial control systems, enhancing production efficiency and data analysis capabilities.