blob: 6e96cf30730c629f06acd68ec403c02145ee9dca [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>PLC4X &#x2013; </title>
<script src="../js/jquery.slim.min.js" type="text/javascript"></script>
<!--script src="../js/popper.min.js" type="javascript"></script-->
<script src="../js/bootstrap.bundle.min.js" type="text/javascript"></script>
<!-- The tooling for adding images and links to Apache events -->
<script src="https://www.apachecon.com/event-images/snippet.js" type="text/javascript"></script>
<!-- FontAwesome -->
<link rel="stylesheet" href="../css/all.min.css" type="text/css"/>
<!-- Bootstrap -->
<link rel="stylesheet" href="../css/bootstrap.min.css" type="text/css"/>
<!-- Some Maven Site defaults -->
<link rel="stylesheet" href="../css/maven-base.css" type="text/css"/>
<link rel="stylesheet" href="../css/maven-theme.css" type="text/css"/>
<!-- The PLC4X version of a bootstrap theme -->
<link rel="stylesheet" href="../css/themes/plc4x.css" type="text/css" id="pagestyle"/>
<!-- A custom style for printing content -->
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print"/>
<meta http-equiv="Content-Language" content="en"/>
</head>
<body class="composite">
<nav class="navbar navbar-light navbar-expand-md bg-faded justify-content-center border-bottom">
<!--a href="/" class="navbar-brand d-flex w-50 mr-auto">Navbar 3</a-->
<a href="https://plc4x.apache.org/" id="bannerLeft"><img src="../images/apache_plc4x_logo_small.png" alt="Apache PLC4X"/></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar3">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse w-100" id="collapsingNavbar3">
<ul class="navbar-nav w-100 justify-content-center">
<li class="nav-item">
<a class="nav-link" href="../index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../users/index.html">Users</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../developers/index.html">Developers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../apache/index.html">Apache</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto justify-content-end">
<li class="nav-item row valign-middle">
<a class="acevent" data-format="wide" data-mode="light" data-event="random" style="width:240px;height:60px;"></a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="row h-100">
<main role="main" class="ml-sm-auto px-4 w-100 h-100">
<div class="sect1">
<h2 id="about_plc4j">About PLC4J</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>PLC4J</code> is a sub-project of <code>PLC4X</code> with implementations based on <code>Java</code> targeted at runtimes for <code>Java 1.8</code> or above.</p>
</div>
<div class="paragraph">
<p>One of PLC4X&#8217;s core principals is, that an application using PLC4X should be independent of the PLC or protocol being used.</p>
</div>
<div class="paragraph">
<p>When addressing a resource on a remote there are two parts that are dependent on the protocol and the type of PLC:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Addressing the PLC itself</p>
</li>
<li>
<p>Addressing a resource on the PLC</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Providing this independence to addressing the PLC itself is completely handled by the <code>PlcDriverManager</code> the application requests a connection from.</p>
</div>
<div class="paragraph">
<p>Hereby the design of the <code>url string</code> passed to the <code>getConnection</code> method is greatly inspired by <code>JDBC</code>.</p>
</div>
<div class="paragraph">
<p>The protocol prefix of the url specifies the type of driver being used.</p>
</div>
<div class="paragraph">
<p>For example, when connecting to a Siemens PLC using the S7/Step7 Protocol, the url: <code>s7://192.42.0.98/1/2</code> causes the driver manager to create a S7 connection instance.
The part behind the <code>:</code> is hereby used by the driver implementation to configure that particular connection.</p>
</div>
<div class="paragraph">
<p>For a S7 connection, for example, this is <code>IP address/host name</code>/<code>rack number</code>/<code>slot number</code>. For different types of connections this url structure will greatly differ.</p>
</div>
<div class="paragraph">
<p>As mentioned above, the second platform dependent information is the address of resources on a PLC.
The format of an address greatly depends on the type of connection. Therefore <code>parseAddress</code> is one of the only methods defined in the <code>PlcConnection</code> interface any connection has to implement.</p>
</div>
<div class="paragraph">
<p>This method returns an object implementing the <code>Address</code> interface which then can be used by the same connection to identify remote resources.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../images/plc4x-architecture.png" alt="plc4x architecture" width="1000" height="574"/>
</div>
</div>
<div class="sect2">
<h3 id="usage">Usage</h3>
<div class="paragraph">
<p>Below code example connects to a remote Siemens S7 PLC using the S7/Step7 protocol and then reads the state of the <code>inputs</code> and <code>outputs</code> from this.</p>
</div>
<div class="paragraph">
<p>It also demonstrates two ways this information can be accessed:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Synchronously (The application pauses, till the response is received)</p>
</li>
<li>
<p>Asynchronously (The application continues and can</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">package org.apache.plc4x.java.examples.helloplc4x;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.PlcValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
public class HelloPlc4x {
private static final Logger logger = LoggerFactory.getLogger(HelloPlc4x.class);
/**
* Example code do demonstrate using PLC4X.
*
* @param args ignored.
*/
public static void main(String[] args) throws Exception {
CliOptions options = CliOptions.fromArgs(args);
if (options == null) {
CliOptions.printHelp();
// Could not parse.
System.exit(1);
}
// Establish a connection to the plc using the url provided as first argument
try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getConnectionString())) {
// Check if this connection support reading of data.
if (!plcConnection.getMetadata().canRead()) {
logger.error("This connection doesn't support reading.");
return;
}
// Create a new read request:
// - Give the single item requested the alias name "value"
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
for (int i = 0; i &lt; options.getFieldAddress().length; i++) {
builder.addItem("value-" + options.getFieldAddress()[i], options.getFieldAddress()[i]);
}
PlcReadRequest readRequest = builder.build();
//////////////////////////////////////////////////////////
// Read synchronously ...
// NOTICE: the ".get()" immediately lets this thread pause until
// the response is processed and available.
logger.info("Synchronous request ...");
PlcReadResponse syncResponse = readRequest.execute().get();
// Simply iterating over the field names returned in the response.
printResponse(syncResponse);
/*PlcValue asPlcValue = syncResponse.getAsPlcValue();
System.out.println(asPlcValue.toString());*/
//////////////////////////////////////////////////////////
// Read asynchronously ...
// Register a callback executed as soon as a response arrives.
/*logger.info("Asynchronous request ...");
CompletionStage&lt;? extends PlcReadResponse&gt; asyncResponse = readRequest.execute();
asyncResponse.whenComplete((readResponse, throwable) -&gt; {
if (readResponse != null) {
printResponse(readResponse);
} else {
logger.error("An error occurred: " + throwable.getMessage(), throwable);
}
});*/
// Give the async request a little time...
TimeUnit.MILLISECONDS.sleep(1000);
plcConnection.close();
System.exit(0);
}
}
private static void printResponse(PlcReadResponse response) {
for (String fieldName : response.getFieldNames()) {
if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(fieldName);
// If it's just one element, output just one single line.
if (numValues == 1) {
logger.info("Value[{}]: {}", fieldName, response.getObject(fieldName));
}
// If it's more than one element, output each in a single row.
else {
logger.info("Value[{}]:", fieldName);
for (int i = 0; i &lt; numValues; i++) {
logger.info(" - {}", response.getObject(fieldName, i));
}
}
}
// Something went wrong, to output an error message instead.
else {
logger.error("Error[{}]: {}", fieldName, response.getResponseCode(fieldName).name());
}
}
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="pt-4 my-md-5 pt-md-5 w-100 border-top">
<div class="row justify-content-md-center" style="font-size: 13px">
<div class="col col-6 text-center">
Copyright &#169; 2017&#x2013;2022 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
All rights reserved.<br/>
Apache PLC4X, PLC4X, Apache, the Apache feather logo, and the Apache PLC4X project logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
<br/><div style="text-align:center;">Home screen image taken from <a
href="https://flic.kr/p/chEftd">Flickr</a>, "Tesla Robot Dance" by Steve Jurvetson, licensed
under <a href="https://creativecommons.org/licenses/by/2.0/">CC BY 2.0 Generic</a>, image cropped
and blur effect added.</div>
</div>
</div>
</footer>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../js/jquery.slim.min.js"></script>
<script src="../js/popper.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script type="text/javascript">
$('.carousel .carousel-item').each(function(){
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
for (let i = 0; i < 3; i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
</script>
</body>
</html>