64 lines
1.8 KiB
C
64 lines
1.8 KiB
C
#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]);
|
|
}
|
|
|
|
} |