1#include <open62541/client_config_default.h>
2#include <open62541/client_highlevel.h>
3#include <open62541/plugin/log_stdout.h>
6static UA_Client* client =
nullptr;
8static bool opcuaCreateClient(
const std::string& addr )
10 if( client !=
nullptr )
13 client = UA_Client_new();
15 if( client ==
nullptr )
18 UA_StatusCode retval = UA_ClientConfig_setDefault(UA_Client_getConfig(client));
20 if( retval != UA_STATUSCODE_GOOD )
22 UA_Client_delete(client);
27 retval = UA_Client_connect(client, addr.c_str());
29 if( retval != UA_STATUSCODE_GOOD )
31 UA_Client_delete(client);
49static long opcuaRead( uint16_t nodeId,
const std::string& attrName )
51 if( client ==
nullptr )
55 UA_Variant* val = UA_Variant_new();
56 UA_StatusCode retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), val);
58 if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
59 val->type == &UA_TYPES[UA_TYPES_INT32])
61 value = *(UA_Int32*)val->data;
64 UA_Variant_delete(val);
68static bool opcuaWrite( uint16_t nodeId,
const std::string& attrName,
long value )
70 if( client ==
nullptr )
73 UA_Variant* myVariant = UA_Variant_new();
74 UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
75 UA_StatusCode retval = UA_Client_writeValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), myVariant);
76 UA_Variant_delete(myVariant);
77 return retval == UA_STATUSCODE_GOOD;
80static bool opcuaWriteBool( uint16_t nodeId,
const std::string& attrName,
bool set )
82 if( client ==
nullptr )
85 UA_Variant* myVariant = UA_Variant_new();
86 UA_Variant_setScalarCopy(myVariant, &set, &UA_TYPES[UA_TYPES_BOOLEAN]);
87 UA_StatusCode retval = UA_Client_writeValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), myVariant);
88 UA_Variant_delete(myVariant);
89 return retval == UA_STATUSCODE_GOOD;
92static bool opcuaReadBool( uint16_t nodeId,
const std::string& attrName )
94 if( client ==
nullptr )
98 UA_Variant* val = UA_Variant_new();
99 UA_StatusCode retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), val);
101 if(retval == UA_STATUSCODE_GOOD && val->type == &UA_TYPES[UA_TYPES_BOOLEAN])
103 value = *(UA_Boolean*)val->data;
106 UA_Variant_delete(val);
111static float opcuaReadFloat( uint16_t nodeId,
const std::string& attrName )
113 if( client ==
nullptr )
117 UA_Variant* val = UA_Variant_new();
118 UA_StatusCode retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), val);
120 if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
121 val->type == &UA_TYPES[UA_TYPES_FLOAT])
123 value = *(UA_Float*)val->data;
126 UA_Variant_delete(val);
130static bool opcuaWriteFloat( uint16_t nodeId,
const std::string& attrName,
float value )
132 if( client ==
nullptr )
135 UA_Variant* myVariant = UA_Variant_new();
136 UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_FLOAT]);
137 UA_StatusCode retval = UA_Client_writeValueAttribute(client, UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()), myVariant);
138 UA_Variant_delete(myVariant);
139 return retval == UA_STATUSCODE_GOOD;
142static UA_StatusCode opcuaCallBool( uint16_t nodeId,
const std::string& objName,
const std::string& attrName,
bool set )
144 UA_Variant* myVariant = UA_Variant_new();
145 UA_Variant_setScalarCopy(myVariant, &set, &UA_TYPES[UA_TYPES_BOOLEAN]);
147 UA_StatusCode retval = UA_Client_call(client,
148 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)objName.c_str()),
149 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()),
153 UA_Variant_delete(myVariant);
158static UA_StatusCode opcuaCallInt32( uint16_t nodeId,
const std::string& objName,
const std::string& attrName,
long value )
161 UA_Variant* myVariant = UA_Variant_new();
162 UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
164 UA_StatusCode retval = UA_Client_call(client,
165 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)objName.c_str()),
166 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()),
170 UA_Variant_delete(myVariant);
175static UA_StatusCode opcuaCallFloat( uint16_t nodeId,
const std::string& objName,
const std::string& attrName,
float value )
178 UA_Variant* myVariant = UA_Variant_new();
179 UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_FLOAT]);
181 UA_StatusCode retval = UA_Client_call(client,
182 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)objName.c_str()),
183 UA_NODEID_STRING((UA_UInt16)nodeId, (
char*)attrName.c_str()),
187 UA_Variant_delete(myVariant);