forked from xuos/xiuos
support plc demo
This commit is contained in:
@@ -15,25 +15,29 @@
|
||||
#include "ua_api.h"
|
||||
|
||||
|
||||
#define UA_DEV_IP_LEN 128
|
||||
#define UA_NODE_LEN 50
|
||||
|
||||
typedef struct _ua_dev_t
|
||||
{
|
||||
char ua_remote_ip[UA_DEV_IP_LEN];
|
||||
char ua_node[UA_NODE_LEN];
|
||||
UA_Client *client;
|
||||
}ua_dev_t;
|
||||
|
||||
|
||||
int ua_open(void *dev)
|
||||
{
|
||||
ua_dev_t *pdev = (ua_dev_t *)dev;
|
||||
|
||||
pdev->client = UA_Client_new();
|
||||
|
||||
ua_pr_info("ua: [%s] start ...\n", __func__);
|
||||
|
||||
if (pdev->client == NULL)
|
||||
{
|
||||
ua_print("ua: [%s] tcp client null\n", __func__);
|
||||
return EEMPTY;
|
||||
}
|
||||
|
||||
UA_ClientConfig *config = UA_Client_getConfig(pdev->client);
|
||||
UA_ClientConfig_setDefault(config);
|
||||
|
||||
ua_pr_info("ua: [%s] %d %s\n", __func__, strlen(pdev->ua_remote_ip), pdev->ua_remote_ip);
|
||||
|
||||
UA_StatusCode retval = UA_Client_connect(pdev->client, pdev->ua_remote_ip);
|
||||
if(retval != UA_STATUSCODE_GOOD) {
|
||||
UA_Client_delete(pdev->client);
|
||||
// UA_Client_delete(pdev->client);
|
||||
ua_pr_info("ua: [%s] deleted ret %x!\n", __func__, retval);
|
||||
return (int)retval;
|
||||
}
|
||||
return EOK;
|
||||
|
||||
@@ -14,10 +14,20 @@
|
||||
|
||||
#include "open62541.h"
|
||||
|
||||
#define UA_DEV_IP_LEN 128
|
||||
#define UA_NODE_LEN 50
|
||||
|
||||
typedef struct _ua_dev_t
|
||||
{
|
||||
char ua_remote_ip[UA_DEV_IP_LEN];
|
||||
char ua_node[UA_NODE_LEN];
|
||||
UA_Client *client;
|
||||
}ua_dev_t;
|
||||
|
||||
#define OPC_SERVER "opc.tcp://192.168.250.5:4840"
|
||||
|
||||
#define ua_print //printf
|
||||
#define ua_trace() //printf("ua: [%s] line %d checked!\n", __func__, __LINE__)
|
||||
#define ua_print //KPrintf
|
||||
#define ua_trace() //KPrintf("ua: [%s] line %d checked!\n", __func__, __LINE__)
|
||||
#define ua_pr_info KPrintf
|
||||
#define ua_debug
|
||||
|
||||
|
||||
@@ -149,19 +149,19 @@ void ua_read_attr(UA_Client *client)
|
||||
{
|
||||
/* Read attribute */
|
||||
UA_Int32 value = 0;
|
||||
ua_print("\nReading the value of node (1, \"the.answer\"):\n");
|
||||
ua_pr_info("\nReading the value of node (1, \"the.answer\"):\n");
|
||||
UA_Variant *val = UA_Variant_new();
|
||||
UA_StatusCode retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
|
||||
if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
|
||||
val->type == &UA_TYPES[UA_TYPES_INT32]) {
|
||||
value = *(UA_Int32*)val->data;
|
||||
ua_print("the value is: %i\n", value);
|
||||
ua_pr_info("the value is: %i\n", value);
|
||||
}
|
||||
UA_Variant_delete(val);
|
||||
|
||||
/* Write node attribute */
|
||||
value++;
|
||||
ua_print("\nWriting a value of node (1, \"the.answer\"):\n");
|
||||
ua_pr_info("\nWriting a value of node (1, \"the.answer\"):\n");
|
||||
UA_WriteRequest wReq;
|
||||
UA_WriteRequest_init(&wReq);
|
||||
wReq.nodesToWrite = UA_WriteValue_new();
|
||||
@@ -174,7 +174,7 @@ void ua_read_attr(UA_Client *client)
|
||||
wReq.nodesToWrite[0].value.value.data = &value;
|
||||
UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
|
||||
if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
|
||||
ua_print("the new value is: %i\n", value);
|
||||
ua_pr_info("the new value is: %i\n", value);
|
||||
|
||||
UA_WriteRequest_clear(&wReq);
|
||||
UA_WriteResponse_clear(&wResp);
|
||||
@@ -297,7 +297,7 @@ int ua_get_server_info(UA_Client *client)
|
||||
|
||||
#ifdef UA_ENABLE_SUBSCRIPTIONS
|
||||
/* Take another look at the.answer */
|
||||
UA_Client_run_iterate(client, 100);
|
||||
UA_Client_run_iterate(client, 10000);
|
||||
/* Delete the subscription */
|
||||
if(UA_Client_Subscriptions_deleteSingle(client, subId) == UA_STATUSCODE_GOOD)
|
||||
ua_print("Subscription removed\n");
|
||||
|
||||
Reference in New Issue
Block a user