fixed the bug of UaGetInfo

This commit is contained in:
wlyu 2022-01-10 18:22:44 +08:00
parent 51de336db1
commit bb0425d1b2
3 changed files with 40 additions and 33 deletions

View File

@ -102,7 +102,7 @@ void *test_ua_get_server_info(void *param)
{ {
UA_Client *client = UA_Client_new(); UA_Client *client = UA_Client_new();
ua_print("ua: [%s] start ...\n", __func__); ua_pr_info("ua: [%s] start ...\n", __func__);
if (client == NULL) if (client == NULL)
{ {
@ -121,12 +121,15 @@ void *test_ua_get_server_info(void *param)
} }
ua_print("ua: [%s] connect ok!\n", __func__); ua_print("ua: [%s] connect ok!\n", __func__);
ua_print("--- start read time ---\n", __func__); ua_pr_info("--- start read time ---\n", __func__);
ua_read_time(client); ua_read_time(client);
ua_print("--- get server info ---\n", __func__); ua_pr_info("--- get server info ---\n", __func__);
ua_get_server_info(client); ua_browser_objects(client);
/* Same thing, this time using the node iterator... */
ua_browser_nodes(client);
/* Clean up */ /* Clean up */
UA_Client_disconnect(client); UA_Client_disconnect(client);

View File

@ -17,11 +17,14 @@
#define OPC_SERVER "opc.tcp://192.168.250.5:4840" #define OPC_SERVER "opc.tcp://192.168.250.5:4840"
#define ua_print //printf #define ua_print //printf
#define ua_trace() //KPrintf("ua: [%s] line %d checked!\n", __func__, __LINE__) #define ua_trace() //printf("ua: [%s] line %d checked!\n", __func__, __LINE__)
#define ua_pr_info KPrintf
#define ua_debug #define ua_debug
int ua_server_connect(void); int ua_server_connect(void);
int ua_get_server_info(UA_Client *client); int ua_get_server_info(UA_Client *client);
void ua_browser_objects(UA_Client *client);
void ua_browser_nodes(UA_Client *client);
void ua_read_time(UA_Client *client); void ua_read_time(UA_Client *client);
int16 ua_test(void); int16 ua_test(void);

View File

@ -30,7 +30,7 @@ static UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId
} }
UA_NodeId *parent = (UA_NodeId *)handle; UA_NodeId *parent = (UA_NodeId *)handle;
ua_print("%d, %d --- %d ---> NodeId %d, %d\n", ua_pr_info("%d, %d --- %d ---> NodeId %d, %d\n",
parent->namespaceIndex, parent->identifier.numeric, parent->namespaceIndex, parent->identifier.numeric,
referenceTypeId.identifier.numeric, childId.namespaceIndex, referenceTypeId.identifier.numeric, childId.namespaceIndex,
childId.identifier.numeric); childId.identifier.numeric);
@ -59,13 +59,13 @@ int ua_get_points(UA_Client *client)
endpointArray[i].endpointUrl.data); endpointArray[i].endpointUrl.data);
} }
UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void ua_browser_objects(UA_Client *client) void ua_browser_objects(UA_Client *client)
{ {
/* Browse some objects */ /* Browse some objects */
ua_print("Browsing nodes in objects folder:\n"); ua_pr_info("Browsing nodes in objects folder:\n");
UA_BrowseRequest bReq; UA_BrowseRequest bReq;
UA_BrowseRequest_init(&bReq); UA_BrowseRequest_init(&bReq);
bReq.requestedMaxReferencesPerNode = 0; bReq.requestedMaxReferencesPerNode = 0;
@ -74,17 +74,17 @@ void ua_browser_objects(UA_Client *client)
bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */ bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */ bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq); UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
ua_print("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME"); ua_pr_info("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
for(size_t i = 0; i < bResp.resultsSize; ++i) { for(size_t i = 0; i < bResp.resultsSize; ++i) {
for(size_t j = 0; j < bResp.results[i].referencesSize; ++j) { for(size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
UA_ReferenceDescription *ref = &(bResp.results[i].references[j]); UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) { if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
ua_print("%-9d %-16d %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex, ua_pr_info("%-9d %-16d %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex,
ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length, ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length,
ref->browseName.name.data, (int)ref->displayName.text.length, ref->browseName.name.data, (int)ref->displayName.text.length,
ref->displayName.text.data); ref->displayName.text.data);
} else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) { } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
ua_print("%-9d %-16.*s %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex, ua_pr_info("%-9d %-16.*s %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex,
(int)ref->nodeId.nodeId.identifier.string.length, (int)ref->nodeId.nodeId.identifier.string.length,
ref->nodeId.nodeId.identifier.string.data, ref->nodeId.nodeId.identifier.string.data,
(int)ref->browseName.name.length, ref->browseName.name.data, (int)ref->browseName.name.length, ref->browseName.name.data,
@ -93,6 +93,7 @@ void ua_browser_objects(UA_Client *client)
/* TODO: distinguish further types */ /* TODO: distinguish further types */
} }
} }
ua_pr_info("\n");
UA_BrowseRequest_clear(&bReq); UA_BrowseRequest_clear(&bReq);
UA_BrowseResponse_clear(&bResp); UA_BrowseResponse_clear(&bResp);
} }
@ -131,7 +132,7 @@ UA_UInt32 ua_start_sub(UA_Client *client)
/* The first publish request should return the initial value of the variable */ /* The first publish request should return the initial value of the variable */
UA_Client_run_iterate(client, 1000); UA_Client_run_iterate(client, 1000);
return subId; return subId;
} }
void ua_read_attr(UA_Client *client) void ua_read_attr(UA_Client *client)
@ -269,33 +270,33 @@ void ua_add_nodes(UA_Client *client)
int ua_get_server_info(UA_Client *client) int ua_get_server_info(UA_Client *client)
{ {
UA_StatusCode retval; UA_StatusCode retval;
/* Listing endpoints */ /* Listing endpoints */
retval = ua_get_points(client); // retval = ua_get_points(client);
if(retval != UA_STATUSCODE_GOOD) { // if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client); // UA_Client_delete(client);
return EXIT_FAILURE; // return EXIT_FAILURE;
} // }
//
// /* Connect to a server */
// /* anonymous connect would be: retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); */
// retval = UA_Client_connect(client, OPC_SERVER);
// if(retval != UA_STATUSCODE_GOOD) {
// UA_Client_delete(client);
// return EXIT_FAILURE;
// }
/* Connect to a server */ ua_browser_objects(client);
/* anonymous connect would be: retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); */
retval = UA_Client_connect(client, OPC_SERVER);
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return EXIT_FAILURE;
}
ua_browser_objects(client);
/* Same thing, this time using the node iterator... */ /* Same thing, this time using the node iterator... */
ua_browser_nodes(client); ua_browser_nodes(client);
#ifdef UA_ENABLE_SUBSCRIPTIONS #ifdef UA_ENABLE_SUBSCRIPTIONS
UA_Int32 subId = ua_start_sub(client); UA_Int32 subId = ua_start_sub(client);
#endif #endif
ua_read_attr(client); ua_read_attr(client);
#ifdef UA_ENABLE_SUBSCRIPTIONS #ifdef UA_ENABLE_SUBSCRIPTIONS
/* Take another look at the.answer */ /* Take another look at the.answer */
@ -306,11 +307,11 @@ int ua_get_server_info(UA_Client *client)
#endif #endif
#ifdef UA_ENABLE_METHODCALLS #ifdef UA_ENABLE_METHODCALLS
ua_call_remote(client); ua_call_remote(client);
#endif #endif
#ifdef UA_ENABLE_NODEMANAGEMENT #ifdef UA_ENABLE_NODEMANAGEMENT
ua_add_nodes(client); ua_add_nodes(client);
#endif #endif
return EXIT_SUCCESS; return EXIT_SUCCESS;