|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2024 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 | +#include <inttypes.h> |
| 35 | +#include <stdio.h> |
| 36 | +#include <plc_comm.h> |
| 37 | + |
| 38 | +#define NUM_ELEMENTS (10) |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief This tests the simplest use of the library |
| 42 | + * |
| 43 | + * This function shows an example of how to read a single tag. All memory and other resourcess |
| 44 | + * are managed by the library. Only the basic functions are used: |
| 45 | + * |
| 46 | + * plc_comm_id_t plc_comm_conn_open(plc_comm_plc_type_t plc_type, const char *address, plc_comm_config_t *config, int32_t timeout_ms); |
| 47 | + * plc_comm_id_t plc_comm_conn_do_request(plc_comm_id_t conn_id, const char *tag_name, int32_t num_elements, plc_comm_request_type_t op, void *translated_data, int32_t translated_data_size, plc_comm_config_t *config, int32_t timeout_ms); |
| 48 | + * int32_t plc_comm_result_batch_get_status(plc_comm_id_t result_batch_id); |
| 49 | + * int32_t plc_comm_conn_dispose(plc_comm_id_t conn_id, int32_t timeout_ms); |
| 50 | + * |
| 51 | + * @return int - status |
| 52 | + */ |
| 53 | +int main(void) |
| 54 | +{ |
| 55 | + value_status_t rc = VAL_OK; |
| 56 | + value_p plc = NULL; |
| 57 | + do { |
| 58 | + value_status_t rc = VAL_OK; |
| 59 | + uint32_t num_tags = 0; |
| 60 | + |
| 61 | + rc = value_open_device("protocol=ab-eip&gateway=10.206.1.39&path=1,0&plc=ControlLogix", &plc); |
| 62 | + if(rc != VAL_OK) { |
| 63 | + info("WARN: Unable to open connection to the PLC!"); |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + rc = value_structure_get_num_fields(plc, &num_tags); |
| 68 | + if(rc != VAL_OK) { |
| 69 | + info("WARN: Unable to get the number of tags!"); |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + for(uint32_t i=0; i < num_tags && rc = VAL_OK; i++) { |
| 74 | + const char *tag_name = NULL; |
| 75 | + value_p tag_val; |
| 76 | + value_type_t tag_type = VAL_TYPE_NONE; |
| 77 | + |
| 78 | + rc = value_structure_get_field_name(plc, i, &tag_name); |
| 79 | + if(rc != VAL_OK) { |
| 80 | + info("WARN: Unable to get the number of tags!"); |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + rc = value_structure_get_field_by_index(plc, i, &tag_val); |
| 85 | + if(rc != VAL_OK) { |
| 86 | + info("WARN: Unable to get the field %u by index!", i); |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + rc = value_get_type(tag_val, &tag_type); |
| 91 | + if(rc != VAL_OK) { |
| 92 | + info("WARN: Unable to get the number of tags!"); |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + printf("Tag %s is of type "); |
| 97 | + switch(tag_type) { |
| 98 | + case VAL_TYPE_BOOL: |
| 99 | + printf(" Bool.\n"); |
| 100 | + break; |
| 101 | + |
| 102 | + case VAL_TYPE_U8: |
| 103 | + printf(" U8.\n"); |
| 104 | + break; |
| 105 | + |
| 106 | + case VAL_TYPE_U16: |
| 107 | + printf(" U16.\n"); |
| 108 | + break; |
| 109 | + |
| 110 | + case VAL_TYPE_U32: |
| 111 | + printf(" U32.\n"); |
| 112 | + break; |
| 113 | + |
| 114 | + case VAL_TYPE_U64: |
| 115 | + printf(" U64.\n"); |
| 116 | + break; |
| 117 | + |
| 118 | + case VAL_TYPE_I8: |
| 119 | + printf(" I8.\n"); |
| 120 | + break; |
| 121 | + |
| 122 | + case VAL_TYPE_I16: |
| 123 | + printf(" I16.\n"); |
| 124 | + break; |
| 125 | + |
| 126 | + case VAL_TYPE_I32: |
| 127 | + printf(" I32.\n"); |
| 128 | + break; |
| 129 | + |
| 130 | + case VAL_TYPE_I64: |
| 131 | + printf(" I64.\n"); |
| 132 | + break; |
| 133 | + |
| 134 | + case VAL_TYPE_F32: |
| 135 | + printf(" Float32.\n"); |
| 136 | + break; |
| 137 | + |
| 138 | + case VAL_TYPE_F64: |
| 139 | + printf(" Float64.\n"); |
| 140 | + break; |
| 141 | + |
| 142 | + case VAL_TYPE_ARRAY: { |
| 143 | + uint32_t num_elements = 0; |
| 144 | + value_type_t element_type = VAL_TYPE_NONE; |
| 145 | + |
| 146 | + printf(" Array"); |
| 147 | + |
| 148 | + rc = value_array_get_num_elements(tag_val, &num_elements); |
| 149 | + if(rc != VAL_OK) { |
| 150 | + info("WARN: Unable to get the number of tags!"); |
| 151 | + break; |
| 152 | + } |
| 153 | + |
| 154 | + printf("[%u]", num_elements); |
| 155 | + |
| 156 | + rc = value_array_get_element_type(tag_val, &element_type); |
| 157 | + if(rc != VAL_OK) { |
| 158 | + info("WARN: Unable to get the element type of the array!"); |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + printf(" elements of type %u.\n", element_type); |
| 163 | + } |
| 164 | + |
| 165 | + break; |
| 166 | + |
| 167 | + case VAL_TYPE_STRUCTURE: { |
| 168 | + uint32_t num_fields = 0; |
| 169 | + |
| 170 | + printf(" Structure "); |
| 171 | + |
| 172 | + rc = value_structure_get_num_fields(tag_val, &num_fields); |
| 173 | + if(rc != VAL_OK) { |
| 174 | + info("WARN: Unable to get the number of tags!"); |
| 175 | + break; |
| 176 | + } |
| 177 | + |
| 178 | + printf("of %u fields.\n", num_fields); |
| 179 | + } |
| 180 | + |
| 181 | + break; |
| 182 | + |
| 183 | + default: |
| 184 | + printf("Unknown type!\n"); |
| 185 | + break; |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + } while(0); |
| 190 | + |
| 191 | + return rc; |
| 192 | +} |
0 commit comments