Skip to content

Instantly share code, notes, and snippets.

@milgner
Last active February 17, 2022 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milgner/f017746ff0aba67d9af16155e9d876ef to your computer and use it in GitHub Desktop.
Save milgner/f017746ff0aba67d9af16155e9d876ef to your computer and use it in GitHub Desktop.
PLC4X Kotlin
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.re-digital</groupId>
<artifactId>plc4x-workshop</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.6.10</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<junit-jupiter.version>5.8.2</junit-jupiter.version>
<plc4j.version>0.9.1</plc4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-api</artifactId>
<version>${plc4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-modbus</artifactId>
<version>${plc4j.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals> <goal>single</goal> </goals>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
fun main() {
PlcDriverManager().getConnection("modbus://localhost:5002").use { conn ->
if (!conn.metadata.canRead()) {
println("Cannot read!!")
return
}
val builder = conn.readRequestBuilder()
builder.addItem("value-1", "coil:1")
builder.addItem("value-2", "coil:3[4]")
builder.addItem("value-3", "holding-register:1")
builder.addItem("value-4", "holding-register:3[4]")
val readRequest = builder.build()
val response = readRequest.execute().get()
for (fieldName in response.fieldNames) {
if (response.getResponseCode(fieldName) === PlcResponseCode.OK) {
val numValues = response.getNumberOfValues(fieldName)
// If it's just one element, output just one single line.
if (numValues == 1) {
println("Value[$fieldName]: ${response.getObject(fieldName)}")
} else {
println("Value[$fieldName]:")
for (i in 0 until numValues) {
println(" - " + response.getObject(fieldName, i))
}
}
} else {
println("Error[$fieldName]: ${response.getResponseCode(fieldName).name}")
}
}
}
// TODO: find out why it's required
// without explicitly exiting here, the JVM will remain stuck here
exitProcess(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment