Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a36c844457 | ||
![]() |
c8373c2d34 | ||
![]() |
04e94b70f2 |
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
}
|
316
UA_PLC.c
316
UA_PLC.c
@ -192,11 +192,11 @@ u32 UA_NodeGetHandleList(long ConnectionHdl,u16 NodeIDCount,UANodeID *NodeIDs,lo
|
||||
if(NodeIDCount>MAX_ELEMENTS_NODELIST){
|
||||
retval=0xA0000002;
|
||||
return retval;
|
||||
}
|
||||
|
||||
for(i=0;i<NodeIDCount;i++){
|
||||
printf("\nNdeIDs[i].IdentifierType=%d,NodeIDs[i].NamespaceIndex=%d,NodeIDs[i].Identifier.length=%d, NodeIDs[i].Identifier.data=%s\n",
|
||||
NodeIDs[i].IdentifierType, NodeIDs[i].NamespaceIndex, NodeIDs[i].Identifier.length ,NodeIDs[i].Identifier.data );
|
||||
}
|
||||
|
||||
for(i=0;i<NodeIDCount;i++){
|
||||
printf("\nNdeIDs[i].IdentifierType=%d,NodeIDs[i].NamespaceIndex=%d,NodeIDs[i].Identifier.length=%d, NodeIDs[i].Identifier.data=%s\n",
|
||||
NodeIDs[i].IdentifierType, NodeIDs[i].NamespaceIndex, NodeIDs[i].Identifier.length ,NodeIDs[i].Identifier.data );
|
||||
|
||||
if(NodeIDs[i].IdentifierType>=0&&NodeIDs[i].IdentifierType<4){
|
||||
switch(NodeIDs[i].IdentifierType){
|
||||
@ -641,7 +641,7 @@ u32 UA_ReadList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditio
|
||||
if(NodeHdlCount>MAX_ELEMENTS_NODELIST){
|
||||
retval=0xA0000002;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
for(i=0;i<NodeHdlCount;i++){
|
||||
UA_ReadValueId_init(&item[i]);
|
||||
@ -654,172 +654,172 @@ u32 UA_ReadList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditio
|
||||
request.nodesToRead = &item[0];
|
||||
request.nodesToReadSize = 1;
|
||||
UA_ReadResponse response = UA_Client_Service_read(c, request);
|
||||
retval = response.responseHeader.serviceResult;
|
||||
if(retval == UA_STATUSCODE_GOOD)
|
||||
retval = response.responseHeader.serviceResult;
|
||||
if(retval == UA_STATUSCODE_GOOD)
|
||||
{
|
||||
if(response.resultsSize==NodeHdlCount)
|
||||
if(response.resultsSize==NodeHdlCount)
|
||||
{
|
||||
for (i = 0; i < NodeHdlCount; i++)
|
||||
{
|
||||
ErrorIDs[i]=response.results[i].status;
|
||||
}
|
||||
UA_Variant *out=malloc(sizeof(UA_Variant));
|
||||
for(i=0;i<NodeHdlCount;i++)
|
||||
{
|
||||
if(ErrorIDs[i]==UA_STATUSCODE_GOOD)
|
||||
{
|
||||
UA_DataValue res = response.results[i];
|
||||
if(!res.hasValue)
|
||||
{
|
||||
UA_ReadResponse_clear(&response);
|
||||
return UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_VALUE)
|
||||
{
|
||||
memcpy(out, &res.value, sizeof(UA_Variant));
|
||||
UA_Variant_init(&res.value);
|
||||
variables[i].data=(void *)*res.value.data;//out->data;
|
||||
variables[i].type=out->type->typeId.identifier.numeric;
|
||||
}
|
||||
else if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_NODECLASS)
|
||||
{
|
||||
UA_NodeClass *out1;
|
||||
memcpy(out1, (UA_NodeClass*)res.value.data, sizeof(UA_NodeClass));
|
||||
variables[i].data=out1;
|
||||
variables[i].type=UA_TYPES_NODECLASS;
|
||||
}
|
||||
else if(UA_Variant_isScalar(&res.value))
|
||||
{
|
||||
memcpy(&variables[i].data, &res.value.data, res.value.type->memSize);
|
||||
UA_free(res.value.data);
|
||||
res.value.data = NULL;
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
|
||||
}
|
||||
} //end for
|
||||
}
|
||||
else
|
||||
UA_Variant *out=malloc(sizeof(UA_Variant));
|
||||
for(i=0;i<NodeHdlCount;i++)
|
||||
{
|
||||
if(ErrorIDs[i]==UA_STATUSCODE_GOOD)
|
||||
{
|
||||
UA_DataValue res = response.results[i];
|
||||
if(!res.hasValue)
|
||||
{
|
||||
UA_ReadResponse_clear(&response);
|
||||
return UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_VALUE)
|
||||
{
|
||||
memcpy(out, &res.value, sizeof(UA_Variant));
|
||||
UA_Variant_init(&res.value);
|
||||
variables[i].data=(void *)*res.value.data;//out->data;
|
||||
variables[i].type=out->type->typeId.identifier.numeric;
|
||||
}
|
||||
else if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_NODECLASS)
|
||||
{
|
||||
UA_NodeClass *out1;
|
||||
memcpy(out1, (UA_NodeClass*)res.value.data, sizeof(UA_NodeClass));
|
||||
variables[i].data=out1;
|
||||
variables[i].type=UA_TYPES_NODECLASS;
|
||||
}
|
||||
else if(UA_Variant_isScalar(&res.value))
|
||||
{
|
||||
memcpy(&variables[i].data, &res.value.data, res.value.type->memSize);
|
||||
UA_free(res.value.data);
|
||||
res.value.data = NULL;
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
|
||||
}
|
||||
} //end for
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
}//end if(retval == UA_STATUSCODE_GOOD)
|
||||
|
||||
UA_ReadResponse_clear(&response);
|
||||
|
||||
UA_ReadResponse_clear(&response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
u32 UA_ReadList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditionalInfo *NodeAddInfos,UA_Value *variables,u32 ErrorIDs[]){
|
||||
//UA_Client *c = (UA_Client*)ConnectionHdl;
|
||||
UA_Client *c=client;
|
||||
int i;
|
||||
u32 retval;
|
||||
UA_ReadValueId item[NodeHdlCount];
|
||||
if(NodeHdlCount>MAX_ELEMENTS_NODELIST){
|
||||
retval=0xA0000002;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0;i<NodeHdlCount;i++){
|
||||
UA_ReadValueId_init(&item[i]);
|
||||
item[i].nodeId = *(UA_NodeId*)NodeHdls[i];
|
||||
//item[i].nodeId.identifier.numeric = NodeHdls[i];//zjy 20240910
|
||||
item[i].attributeId = NodeAddInfos[i].AttributeID;
|
||||
//item[i].nodeId.namespaceIndex = 1;
|
||||
console_printf("------------#2------NodeHdls[i] = %ld ,item[i].nodeId = %d,item[i].attributeId=%d,item[i].nodeId.namespaceIndex=%d-\n",
|
||||
NodeHdls[i],item[i].nodeId.identifier.string.length,item[i].attributeId,item[i].nodeId.namespaceIndex);
|
||||
}
|
||||
UA_ReadRequest request;
|
||||
UA_ReadRequest_init(&request);
|
||||
request.nodesToRead = &item[0];
|
||||
request.nodesToReadSize = 1;
|
||||
UA_ReadResponse response = UA_Client_Service_read(c, request);
|
||||
retval = response.responseHeader.serviceResult;
|
||||
if(retval == UA_STATUSCODE_GOOD)
|
||||
{
|
||||
console_printf("------------#3---------------\n");
|
||||
if(response.resultsSize==NodeHdlCount)
|
||||
{
|
||||
console_printf("------------#4---------------\n");
|
||||
for (i = 0; i < NodeHdlCount; i++)
|
||||
{
|
||||
ErrorIDs[i]=response.results[i].status;
|
||||
}
|
||||
retval = 0 ;
|
||||
for(i=0;i<NodeHdlCount;i++)
|
||||
{
|
||||
if(ErrorIDs[i]==UA_STATUSCODE_GOOD)
|
||||
{
|
||||
console_printf("------------#5---------------\n");
|
||||
UA_DataValue res = response.results[i];
|
||||
if(!res.hasValue)
|
||||
{
|
||||
console_printf("------------#6---------------\n");
|
||||
UA_ReadResponse_clear(&response);
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_VALUE)
|
||||
{
|
||||
//UA_Variant *out=malloc(sizeof(UA_Variant));
|
||||
UA_Variant out;
|
||||
//memcpy(&out, &res.value, sizeof(UA_Variant));
|
||||
//UA_Variant_init(&res.value);
|
||||
//variables[i].data =res.value.data;
|
||||
long temp ;
|
||||
memcpy(&temp,res.value.data,8);
|
||||
variables[i].data = (void *)temp;
|
||||
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
|
||||
}
|
||||
else if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_NODECLASS)
|
||||
{
|
||||
//UA_NodeClass *out1;
|
||||
//memcpy(out1, (UA_NodeClass*)res.value.data, sizeof(UA_NodeClass));
|
||||
memcpy(variables[i].data,res.value.data,8);
|
||||
variables[i].type=UA_TYPES_NODECLASS;
|
||||
}
|
||||
else if(UA_Variant_isScalar(&res.value))
|
||||
{
|
||||
//memcpy(&variables[i].data, &res.value.data, res.value.type->memSize);
|
||||
//UA_free(res.value.data);
|
||||
//res.value.data = 0;
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
|
||||
}
|
||||
UA_free(res.value.data); //????? need or not
|
||||
}
|
||||
} //end for
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
}//end if(retval == UA_STATUSCODE_GOOD)
|
||||
// console_printf("------------#f8 --------------\n");
|
||||
// UA_ReadResponse_clear(&response);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
*/
|
||||
u32 UA_ReadList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditionalInfo *NodeAddInfos,UA_Value *variables,u32 ErrorIDs[]){
|
||||
//UA_Client *c = (UA_Client*)ConnectionHdl;
|
||||
UA_Client *c=client;
|
||||
int i;
|
||||
u32 retval;
|
||||
UA_ReadValueId item[NodeHdlCount];
|
||||
if(NodeHdlCount>MAX_ELEMENTS_NODELIST){
|
||||
retval=0xA0000002;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0;i<NodeHdlCount;i++){
|
||||
UA_ReadValueId_init(&item[i]);
|
||||
item[i].nodeId = *(UA_NodeId*)NodeHdls[i];
|
||||
//item[i].nodeId.identifier.numeric = NodeHdls[i];//zjy 20240910
|
||||
item[i].attributeId = NodeAddInfos[i].AttributeID;
|
||||
//item[i].nodeId.namespaceIndex = 1;
|
||||
console_printf("------------#2------NodeHdls[i] = %ld ,item[i].nodeId = %d,item[i].attributeId=%d,item[i].nodeId.namespaceIndex=%d-\n",
|
||||
NodeHdls[i],item[i].nodeId.identifier.string.length,item[i].attributeId,item[i].nodeId.namespaceIndex);
|
||||
}
|
||||
UA_ReadRequest request;
|
||||
UA_ReadRequest_init(&request);
|
||||
request.nodesToRead = &item[0];
|
||||
request.nodesToReadSize = 1;
|
||||
UA_ReadResponse response = UA_Client_Service_read(c, request);
|
||||
retval = response.responseHeader.serviceResult;
|
||||
if(retval == UA_STATUSCODE_GOOD)
|
||||
{
|
||||
console_printf("------------#3---------------\n");
|
||||
if(response.resultsSize==NodeHdlCount)
|
||||
{
|
||||
console_printf("------------#4---------------\n");
|
||||
for (i = 0; i < NodeHdlCount; i++)
|
||||
{
|
||||
ErrorIDs[i]=response.results[i].status;
|
||||
}
|
||||
retval = 0 ;
|
||||
for(i=0;i<NodeHdlCount;i++)
|
||||
{
|
||||
if(ErrorIDs[i]==UA_STATUSCODE_GOOD)
|
||||
{
|
||||
console_printf("------------#5---------------\n");
|
||||
UA_DataValue res = response.results[i];
|
||||
if(!res.hasValue)
|
||||
{
|
||||
console_printf("------------#6---------------\n");
|
||||
UA_ReadResponse_clear(&response);
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_VALUE)
|
||||
{
|
||||
//UA_Variant *out=malloc(sizeof(UA_Variant));
|
||||
UA_Variant out;
|
||||
//memcpy(&out, &res.value, sizeof(UA_Variant));
|
||||
//UA_Variant_init(&res.value);
|
||||
//variables[i].data =res.value.data;
|
||||
long temp ;
|
||||
memcpy(&temp,res.value.data,8);
|
||||
variables[i].data = (void *)temp;
|
||||
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
|
||||
}
|
||||
else if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_NODECLASS)
|
||||
{
|
||||
//UA_NodeClass *out1;
|
||||
//memcpy(out1, (UA_NodeClass*)res.value.data, sizeof(UA_NodeClass));
|
||||
memcpy(variables[i].data,res.value.data,8);
|
||||
variables[i].type=UA_TYPES_NODECLASS;
|
||||
}
|
||||
else if(UA_Variant_isScalar(&res.value))
|
||||
{
|
||||
//memcpy(&variables[i].data, &res.value.data, res.value.type->memSize);
|
||||
//UA_free(res.value.data);
|
||||
//res.value.data = 0;
|
||||
variables[i].type=res.value.type->typeId.identifier.numeric;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
|
||||
}
|
||||
UA_free(res.value.data); //????? need or not
|
||||
}
|
||||
} //end for
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
}//end if(retval == UA_STATUSCODE_GOOD)
|
||||
// console_printf("------------#f8 --------------\n");
|
||||
// UA_ReadResponse_clear(&response);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
u32 UA_WriteList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditionalInfo *NodeAddInfos,UA_Value *variables,u32 ErrorIDs[]){
|
||||
UA_Client *c = (UA_Client*)ConnectionHdl;
|
||||
|
64
myClient.c
64
myClient.c
@ -1,64 +0,0 @@
|
||||
#include <string.h>
|
||||
#include "UA_PLC.h"
|
||||
|
||||
UA_Client *client;
|
||||
|
||||
u32 Connect(long *ConnectionHdl){
|
||||
UA_Client *c = UA_Client_new();
|
||||
UA_StatusCode flag = UA_ClientConfig_setDefault(UA_Client_getConfig(c));
|
||||
UA_ClientConfig *config=UA_Client_getConfig(c);
|
||||
if (flag == UA_STATUSCODE_GOOD) {
|
||||
flag=UA_Client_connect(c, "opc.tcp://localhost:4840");
|
||||
|
||||
}else
|
||||
{
|
||||
UA_Client_delete(c);
|
||||
//memset(c,0,100);
|
||||
return flag;
|
||||
}
|
||||
client = c;
|
||||
*ConnectionHdl = (long *)c;
|
||||
printf("ConnectionHdl %lld \n",*ConnectionHdl);
|
||||
return flag;
|
||||
}
|
||||
|
||||
u32 NamespaceGetIndexList(long ConnectionHdl,u16 NamespaceUrisCount,STRING *NamespaceUris,u16 *NamespaceIndexes){
|
||||
UA_Client *c = (UA_Client*)ConnectionHdl;
|
||||
int i;
|
||||
u32 retval=0;
|
||||
if(NamespaceUrisCount>MAX_ELEMENTS_NAMESPACES){
|
||||
retval=0xA0000002;
|
||||
return retval;
|
||||
}
|
||||
for(i=0;i<NamespaceUrisCount;i++){
|
||||
UA_String NamespaceUri;
|
||||
NamespaceUri.length = NamespaceUris[i].length;
|
||||
NamespaceUri.data = NamespaceUris[i].data;
|
||||
retval=UA_Client_NamespaceGetIndex(c,&NamespaceUri,&NamespaceIndexes[i]);
|
||||
if (retval!= UA_STATUSCODE_GOOD) {
|
||||
printf("Failed to get namespace index for URI %.*s\n", (int)NamespaceUri.length, NamespaceUri.data);
|
||||
break; // 出现错误时立即退出循环
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
long connectionHdl;
|
||||
Connect(&connectionHdl);
|
||||
STRING str[3];
|
||||
str[0].data = (UA_Byte*)"http://opcfoundation.org/UA/";
|
||||
str[0].length = 28;
|
||||
str[1].data = (UA_Byte*)"urn:open62541.server.application";
|
||||
str[1].length = 32;
|
||||
str[2].data = (UA_Byte*)"http://example.com/MyNamespace";
|
||||
str[2].length = 30;
|
||||
u16 num[3] = {0};
|
||||
NamespaceGetIndexList(connectionHdl,3,str,num);
|
||||
printf("Namespace Indexes:\n");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
printf("URI %d: Index = %u\n", i, num[i]);
|
||||
}
|
||||
|
||||
}
|
BIN
opcua_client
BIN
opcua_client
Binary file not shown.
BIN
opcua_server
BIN
opcua_server
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user