Skip to content

Commit 3e56fe0

Browse files
committed
Not working yet. Added code to test.c to test the API.
1 parent 8764cd1 commit 3e56fe0

File tree

5 files changed

+449
-67
lines changed

5 files changed

+449
-67
lines changed

src/examples/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ project(plc_comm_examples VERSION 0.1.0 LANGUAGES C)
55
include_directories(../include)
66

77
# example executables
8-
add_executable(example1 example1.c)
9-
add_executable(example2 example2.c)
10-
add_executable(example3 example3.c)
11-
add_executable(example4 example4.c)
12-
add_executable(example5 example5.c)
8+
add_executable(test test.c)
9+
# add_executable(example1 example1.c)
10+
# add_executable(example2 example2.c)
11+
# add_executable(example3 example3.c)
12+
# add_executable(example4 example4.c)
13+
# add_executable(example5 example5.c)

src/examples/test.c

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
}

src/lib/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ include_directories(..)
66

77
# set up paths to get included libplctag header and library
88
include_directories(../../res/include)
9-
set(CMAKE_PREFIX_PATH "../../res/bin;${CMAKE_PREFIX_PATH}")
10-
add_library(libplctag STATIC IMPORTED)
9+
# set(CMAKE_PREFIX_PATH "../../res/bin;${CMAKE_PREFIX_PATH}")
10+
# add_library(libplctag STATIC IMPORTED)
1111

12-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fms-extensions ")
12+
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fms-extensions ")
1313

1414
# library
15-
add_library(plc_comm_lib
15+
add_library(plc_comm_lib SHARED
1616
plc_comm.c
1717
utils.c
18+
../../res/bin/libplctag.a
1819
)
1920

2021
# require at least C11
2122
set_property(TARGET plc_comm_lib PROPERTY C_STANDARD 11)
23+
24+
target_compile_options(plc_comm_lib PUBLIC --std=c11)
25+
target_compile_options(plc_comm_lib PUBLIC -fms-extensions)
26+
target_compile_options(plc_comm_lib PUBLIC -Wno-microsoft-anon-tag)
27+
28+
# target_link_options(plc_comm_lib PUBLIC "LINKER:../../res/bin/libplctag.a")
29+
30+
# target_link_libraries(plc_comm_lib libplctag)

0 commit comments

Comments
 (0)