Skip to content

Commit

Permalink
Add command-line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
kasemir committed Jun 22, 2022
1 parent 88118cf commit d89cd44
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.junit
Bundle-Vendor: Kay Kasemir <kasemirk@ornl.gov> - SNS
Main-Class: etherip.Main
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ for IOCs on top of the basic protocol library.
The Java implementation is currently only the basic read/write library
with unit tests to demonstrate the functionality.

For basic read/write, see test/etherip/EtherIPDemo.java
For basic read/write, see test/etherip/EtherIPDemo.java or Main.java for a simple command line tool.

For a 'scan list' that reads tags all the time, but also allows writing them, see test/etherip/scan/ScanListTest.java

Expand All @@ -55,3 +55,4 @@ Build with maven:
mvn -DskipTests=true clean package

Develop in Eclipse via File, Import, Maven, Existing Maven Projects.

7 changes: 7 additions & 0 deletions etherip
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
#
# Example for calling command line test tool,
# assuming 'java' is found on the PATH

JAR=`echo target/etherip*.jar`
java -jar $JAR "$@"
143 changes: 143 additions & 0 deletions src/main/java/etherip/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************************************************************
* Copyright (c) 2022 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package etherip;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import etherip.types.CIPData;
import etherip.types.CIPData.Type;

/** Command-line 'main' for basic read/write tests
* @author Kay Kasemir
*/
public class Main
{
private static String address = "127.0.0.1";
private static int slot = 0;
private static short array = 1;
private static String tag = "";
private static CIPData write = null;

private static void usage()
{
System.out.println("USAGE: etherip [options] <tag>");
System.out.println("");
System.out.println("Options:");
System.out.println("-h help");
System.out.println("-v verbose");
System.out.println("-i " + address + " IP address or DNS name of PLC");
System.out.println("-s " + slot + " Controller slot in ControlLogix crate");
System.out.println("-a " + array + " Number of array elements to read, 1 for scalar");
System.out.println("-w 3.14 CIPReal value to write");
}

public static void main(final String[] args) throws Exception
{

for (int i=0; i<args.length; ++i)
{
if ("-h".equals(args[i]))
{
usage();
return;
}
else if ("-v".equals(args[i]))
{
final Logger root = Logger.getLogger("");
root.setLevel(Level.ALL);
for (Handler handler : root.getHandlers())
handler.setLevel(root.getLevel());
}
else if ("-i".equals(args[i]))
{
if (i+1 < args.length)
address = args[++i];
else
{
System.out.println("Missing address for -i");
usage();
return;
}
}
else if ("-s".equals(args[i]))
{
if (i+1 < args.length)
slot = Integer.parseInt(args[++i]);
else
{
System.out.println("Missing slot for -s");
usage();
return;
}
}
else if ("-a".equals(args[i]))
{
if (i+1 < args.length)
array = Short.parseShort(args[++i]);
else
{
System.out.println("Missing array count for -a");
usage();
return;
}
}
else if ("-w".equals(args[i]))
{
if (i+1 < args.length)
{
write = new CIPData(Type.REAL, 1);
write.set(0, Double.parseDouble(args[++i]));
}
else
{
System.out.println("Missing number to write for -w");
usage();
return;
}
}
else if (args[i].startsWith("-"))
{
System.out.println("Unknown option '" + args[i] + "'");
usage();
return;
}
else if (tag.isBlank())
tag = args[i].trim();
else
{
System.out.println("Can only handle one tag");
usage();
return;
}
}

if (tag.isBlank())
{
System.out.println("Missing <tag>");
usage();
return;
}

try (final EtherNetIP plc = new EtherNetIP(address, slot))
{
plc.connectTcp();

if (write != null)
{
plc.writeTag(tag, write);
}
else
{
final CIPData data = plc.readTag(tag, array);
System.out.println(data);
}
}
}
}

0 comments on commit d89cd44

Please sign in to comment.