From d89cd44bdac932ddbd28d15183f32dd60629fc61 Mon Sep 17 00:00:00 2001 From: Kay Kasemir Date: Wed, 22 Jun 2022 15:49:04 -0400 Subject: [PATCH] Add command-line tool --- META-INF/MANIFEST.MF | 1 + README.md | 3 +- etherip | 7 ++ src/main/java/etherip/Main.java | 143 ++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100755 etherip create mode 100644 src/main/java/etherip/Main.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index c9d9b29..f71cef6 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -6,3 +6,4 @@ Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Require-Bundle: org.junit Bundle-Vendor: Kay Kasemir - SNS +Main-Class: etherip.Main diff --git a/README.md b/README.md index 1f830c8..dbc7117 100644 --- a/README.md +++ b/README.md @@ -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 @@ -55,3 +55,4 @@ Build with maven: mvn -DskipTests=true clean package Develop in Eclipse via File, Import, Maven, Existing Maven Projects. + diff --git a/etherip b/etherip new file mode 100755 index 0000000..604d39d --- /dev/null +++ b/etherip @@ -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 "$@" diff --git a/src/main/java/etherip/Main.java b/src/main/java/etherip/Main.java new file mode 100644 index 0000000..df4b5f1 --- /dev/null +++ b/src/main/java/etherip/Main.java @@ -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] "); + 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"); + 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); + } + } + } +}