Hello guys! I'm trying to read tags from MasterOPC using the open62541 C library in Visual Studio. Here's how:
int main(void)
{
UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client));
const char *endpoint = "opc.tcp://localhost:4840";
UA_StatusCode retval = UA_Client_connect(client, endpoint);
if(retval != UA_STATUSCODE_GOOD)
{
printf("Failed to connect: %s (0x%08x)\n",
UA_StatusCode_name(retval), retval);
UA_Client_delete(client);
return (int)retval;
}
printf("Connected to %s\n", endpoint);
UA_NodeId nodeId = UA_NODEID_STRING(1, "Channel1.Device1.TagName");
UA_Variant value;
UA_Variant_init(&value);
retval = UA_Client_readValueAttribute(client, nodeId, &value);
if(retval == UA_STATUSCODE_GOOD)
{
// Check the type and print the value
if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_FLOAT]))
{
float val = *(float*)value.data;
printf("Value (Float): %.3f\n", val);
}
else
if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32]))
{
UA_Int32 val = *(UA_Int32*)value.data;
printf("Value (Int32): %d\n", (int)val);
}
else
if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_BOOLEAN]))
{
UA_Boolean val = *(UA_Boolean*)value.data;
printf("Value (Bool): %s\n", val ? "true" : "false");
}
else
if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_STRING]))
{
UA_String str = *(UA_String*)value.data;
printf("Value (String): %.*s\n", (int)str.length, str.data);
}
else
{
printf("Value of another type (not processed)\n");
}
}
else
{
printf("Read error: %s (0x%08x)\n", UA_StatusCode_name(retval), retval);
}
UA_Variant_clear(&value);
UA_Client_disconnect(client);
UA_Client_delete(client);
printf("\nDone. Press Enter...\n");
getchar();
return 0;
}
I inserted the server IP, port, and nodeI.
And here's the problem: Tags like SERVER_ONLY are read correctly, but other types, like HOLDING_REGISTR, aren't read at all. What could be wrong, please tell me.
Grock insists that the ns value might be incorrect, but I'm checking it in UaExpert.
And just to be sure, I tried reading in a loop, iterating through ns values from 0 to 32. It didn't work. Please help a noob.