Hello everybody,
for my project I need to adapt the SampleClient::read() function that is developed in the Client development tutorial. Additionally to reading the current server time, I like to read one of my own variables that are deployed on the server. The server address is "opc.tcp://localhost:4840". I created the server via TwinCAT 3 (Beckhoff) and I defined some global variables (syntax: {attribute 'OPC.UA.DA' := '1'} gl_Uint_Test : UINT := 123;).
The server and the variables are visible in UAExpert (see picture).
Here comes my problem: I adapted the SampleClient::read() function so that I can read the variable "GVL_Global.gl_Bool_Heartbeat", but unfortunately I do not get any result. I already tried out nearly everything without success. I do not know where the problem should be.
Here is the code:
UaStatus SampleClient::read()
{
UaStatus result;
ServiceSettings serviceSettings;
UaReadValueIds nodeToRead;
UaDataValues values;
UaDiagnosticInfos diagnosticInfos;
// Configure one node to read
// We read the value of the ServerStatus -> CurrentTime
nodeToRead.create(2);
nodeToRead[0].AttributeId = OpcUa_Attributes_Value;
nodeToRead[0].NodeId.Identifier.Numeric = OpcUaId_Server_ServerStatus_CurrentTime;
nodeToRead[1].AttributeId = OpcUa_Attributes_NodeId;
nodeToRead[1].NodeId.NamespaceIndex = 4;
nodeToRead[1].NodeId.Identifier.String.strContent = "GVL_Global.gl_Bool_Heartbeat";
nodeToRead[1].NodeId.Identifier.String.uLength = strlen("GVL_Global.gl_Bool_Heartbeat");
printf("\nReading ...\n");
result = m_pSession->read(serviceSettings, 0, OpcUa_TimestampsToReturn_Both, nodeToRead, values,
diagnosticInfos);
if (result.isGood())
{
// Read service succeded - check status of read value
if (OpcUa_IsGood(values[0].StatusCode))
{
printf("ServerStatusCurrentTime: %s\n", UaVariant(values[0].Value).toString().toUtf8());
printf("Inhalt von Heartbeat: %s\n", UaVariant(values[1].Value).toString().toUtf8());
}
else
{
printf("Read failed for item[0] with status %s\n", UaStatus(values[0].StatusCode).toString().toUtf8());
}
}
else
{
// Service call failed
printf("Read failed with status %s\n", result.toString().toUtf8());
}
return result;
}
By the way: I have the same struggle when I use the subscription function.
SampleClient::read() cannot read own global variables
Moderator: uasdkcpp
-
- Hero Member
- Posts: 21
- Joined: 28 Mar 2017, 07:20
- Support Team
- Hero Member
- Posts: 3068
- Joined: 18 Mar 2011, 15:09
Re: SampleClient::read() cannot read own global variables
Hello mantela.
you should use the SDK class to set the NodeId:
That will solve the problem.
you should use the SDK class to set the NodeId:
Code: Select all
UaNodeId nodeId("GVL_Global.gl_Bool_Heartbeat", 4);
nodeId.copyTo(&nodesToRead[1].NodeId);
nodesToRead[1].AttributeId = OpcUa_Attributes_Value;
Best regards
Unified Automation Support Team
Unified Automation Support Team
-
- Hero Member
- Posts: 21
- Joined: 28 Mar 2017, 07:20
Re: SampleClient::read() cannot read own global variables
Hi Support Team,
thank you for your reply.
Meanwhile I already found the following post: http://forum.unified-automation.com/topic1766.html
That is exactly the same problem that I had.
Now everything is working fine.
thank you for your reply.
Meanwhile I already found the following post: http://forum.unified-automation.com/topic1766.html
That is exactly the same problem that I had.
Now everything is working fine.