|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2021 by Kyle Hayes * |
| 3 | + * Author Kyle Hayes kyle.hayes@gmail.com * |
| 4 | + * * |
| 5 | + * This software is available under either the Mozilla Public License * |
| 6 | + * version 2.0 or the GNU LGPL version 2 (or later) license, whichever * |
| 7 | + * you choose. * |
| 8 | + * * |
| 9 | + * MPL 2.0: * |
| 10 | + * * |
| 11 | + * This Source Code Form is subject to the terms of the Mozilla Public * |
| 12 | + * License, v. 2.0. If a copy of the MPL was not distributed with this * |
| 13 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. * |
| 14 | + * * |
| 15 | + * * |
| 16 | + * LGPL 2: * |
| 17 | + * * |
| 18 | + * This program is free software; you can redistribute it and/or modify * |
| 19 | + * it under the terms of the GNU Library General Public License as * |
| 20 | + * published by the Free Software Foundation; either version 2 of the * |
| 21 | + * License, or (at your option) any later version. * |
| 22 | + * * |
| 23 | + * This program is distributed in the hope that it will be useful, * |
| 24 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 25 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 26 | + * GNU General Public License for more details. * |
| 27 | + * * |
| 28 | + * You should have received a copy of the GNU Library General Public * |
| 29 | + * License along with this program; if not, write to the * |
| 30 | + * Free Software Foundation, Inc., * |
| 31 | + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 32 | + ***************************************************************************/ |
| 33 | + |
| 34 | +import io.github.libplctag.Tag; |
| 35 | + |
| 36 | +public class TagExample { |
| 37 | + static final int TIMEOUT = 5000; |
| 38 | + static int lastState = -1; |
| 39 | + |
| 40 | + public static void main(String argv[]) { |
| 41 | + int rc = Tag.PLCTAG_STATUS_OK; |
| 42 | + |
| 43 | + // check the library version. |
| 44 | + if(!Tag.checkLibraryVersion(2, 3, 4)) { |
| 45 | + System.err.println("This version of the library does not support the required version of 2.3.4!"); |
| 46 | + System.exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + // set the debug level. |
| 50 | + Tag.setDebugLevel(Tag.PLCTAG_DEBUG_DETAIL); |
| 51 | + |
| 52 | + // get the library version |
| 53 | + int version_major = Tag.getLibraryIntAttribute("version_major", 0); |
| 54 | + int version_minor = Tag.getLibraryIntAttribute("version_minor", 0); |
| 55 | + int version_patch = Tag.getLibraryIntAttribute("version_patch", 0); |
| 56 | + |
| 57 | + System.err.println("Using library version " + version_major + "." + version_minor + "." + version_patch + "."); |
| 58 | + |
| 59 | + // read a PLC-5 tag |
| 60 | + Tag tag = new Tag("protocol=ab-eip&gateway=10.206.1.38&plc=PLC5&elem_size=2&elem_count=1&name=N7:0", TIMEOUT); |
| 61 | + rc = tag.getStatus(); |
| 62 | + if(rc != Tag.PLCTAG_STATUS_OK) { |
| 63 | + System.err.println("Unable to create the tag, got error " + Tag.decodeError(rc) + "!"); |
| 64 | + System.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + // FIXME - one of the two callbacks should show a lambda example. |
| 68 | + Tag.EventCallbackInterface eventCallback = new Tag.EventCallbackInterface(){ |
| 69 | + public void invoke(int tag_id, int event, int status) { |
| 70 | + String eventMsg; |
| 71 | + |
| 72 | + switch(event) { |
| 73 | + case Tag.PLCTAG_EVENT_ABORTED: |
| 74 | + eventMsg = "operation aborted"; |
| 75 | + lastState = Tag.PLCTAG_EVENT_ABORTED; |
| 76 | + break; |
| 77 | + |
| 78 | + case Tag.PLCTAG_EVENT_DESTROYED: |
| 79 | + eventMsg = "being destroyed"; |
| 80 | + lastState = Tag.PLCTAG_EVENT_DESTROYED; |
| 81 | + break; |
| 82 | + |
| 83 | + case Tag.PLCTAG_EVENT_READ_COMPLETED: |
| 84 | + eventMsg = "read completed"; |
| 85 | + lastState = Tag.PLCTAG_EVENT_READ_COMPLETED; |
| 86 | + break; |
| 87 | + |
| 88 | + case Tag.PLCTAG_EVENT_READ_STARTED: |
| 89 | + eventMsg = "read started"; |
| 90 | + lastState = Tag.PLCTAG_EVENT_READ_STARTED; |
| 91 | + break; |
| 92 | + |
| 93 | + case Tag.PLCTAG_EVENT_WRITE_COMPLETED: |
| 94 | + eventMsg = "write completed"; |
| 95 | + lastState = Tag.PLCTAG_EVENT_WRITE_COMPLETED; |
| 96 | + break; |
| 97 | + |
| 98 | + case Tag.PLCTAG_EVENT_WRITE_STARTED: |
| 99 | + eventMsg = "write started"; |
| 100 | + lastState = Tag.PLCTAG_EVENT_WRITE_STARTED; |
| 101 | + break; |
| 102 | + |
| 103 | + default: |
| 104 | + eventMsg = "unknown event!"; |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + System.err.println("Tag " + tag_id + " " + eventMsg + " with status " + Tag.decodeError(status) + "."); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + rc = tag.registerEventCallback(eventCallback); |
| 113 | + if(rc != Tag.PLCTAG_STATUS_OK) { |
| 114 | + System.err.println("Unable to register a callback on the tag, got error " + Tag.decodeError(rc) + "!"); |
| 115 | + System.exit(1); |
| 116 | + } |
| 117 | + |
| 118 | + // read the tag. |
| 119 | + rc = tag.read(TIMEOUT); |
| 120 | + if(rc != Tag.PLCTAG_STATUS_OK) { |
| 121 | + System.err.println("Unable to read the tag, got error " + Tag.decodeError(rc) + "!"); |
| 122 | + System.exit(1); |
| 123 | + } |
| 124 | + |
| 125 | + // get the tag element size and count. |
| 126 | + int elem_size = tag.getIntAttribute("elem_size", -1); |
| 127 | + if(elem_size == -1) { |
| 128 | + System.err.println("Unable to get the tag's element size!"); |
| 129 | + System.exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + int elem_count = tag.getIntAttribute("elem_count", -1); |
| 133 | + if(elem_size == -1) { |
| 134 | + System.err.println("Unable to get the tag's element count!"); |
| 135 | + System.exit(1); |
| 136 | + } |
| 137 | + |
| 138 | + // print out the values. |
| 139 | + for(int index = 0; index < elem_count; index++) { |
| 140 | + int val = 0; |
| 141 | + |
| 142 | + switch (elem_size) { |
| 143 | + case 1: |
| 144 | + val = tag.getInt8(index * elem_size); |
| 145 | + System.err.println("data[" + index + "] = " + val); |
| 146 | + break; |
| 147 | + |
| 148 | + case 2: |
| 149 | + val = tag.getInt16(index * elem_size); |
| 150 | + System.err.println("data[" + index + "] = " + val); |
| 151 | + break; |
| 152 | + |
| 153 | + case 4: |
| 154 | + val = tag.getInt32(index * elem_size); |
| 155 | + System.err.println("data[" + index + "] = " + val); |
| 156 | + break; |
| 157 | + |
| 158 | + default: |
| 159 | + System.err.println("Unsupported data size " + elem_size + "!"); |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // done with the tag. |
| 165 | + tag.unregisterEventCallback(); |
| 166 | + tag.close(); |
| 167 | + |
| 168 | + // read a Logix tag |
| 169 | + tag = new Tag("protocol=ab-eip&gateway=10.206.1.39&path=1,5&plc=ControlLogix&elem_count=1&name=TestStringArray", TIMEOUT); |
| 170 | + rc = tag.getStatus(); |
| 171 | + if(rc != Tag.PLCTAG_STATUS_OK) { |
| 172 | + System.err.println("Unable to create the tag, got error " + Tag.decodeError(rc) + "!"); |
| 173 | + System.exit(1); |
| 174 | + } |
| 175 | + |
| 176 | + // get the string length |
| 177 | + int str_length = tag.getStringLength(0); |
| 178 | + if(str_length < 0) { |
| 179 | + System.err.println("Unable to get the string length, got error " + Tag.decodeError(str_length) + "!"); |
| 180 | + System.exit(1); |
| 181 | + } |
| 182 | + |
| 183 | + System.err.println("String length " + str_length); |
| 184 | + |
| 185 | + // get the string |
| 186 | + String str = tag.getString(0); |
| 187 | + if(str == null) { |
| 188 | + rc = tag.getStatus(); |
| 189 | + System.err.println("Unable to get the string, got error " + Tag.decodeError(rc) + "!"); |
| 190 | + System.exit(1); |
| 191 | + } |
| 192 | + |
| 193 | + System.err.println("String: \"" + str + "\"."); |
| 194 | + |
| 195 | + // done with the tag |
| 196 | + tag.close(); |
| 197 | + |
| 198 | + System.err.println("Done."); |
| 199 | + } |
| 200 | +} |
0 commit comments