Compare commits
	
		
			9 Commits
		
	
	
		
			origin
			...
			MethodCall
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 4041481593 | |||
| 9e7793ea5f | |||
| c9370e53db | |||
| 2692cbfa14 | |||
| 92dc482f10 | |||
| f014791721 | |||
| d2290d43a7 | |||
| 26b2939a5c | |||
|   | 8c9b73c2d3 | 
							
								
								
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +0,0 @@ | |||||||
| { |  | ||||||
|     "C_Cpp.errorSquiggles": "disabled" |  | ||||||
| } |  | ||||||
							
								
								
									
										629
									
								
								myClient.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										629
									
								
								myClient.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,629 @@ | |||||||
|  | #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; | ||||||
|  | // } | ||||||
|  |  | ||||||
|  | void saveUA_ReferenceDescriptionToUAReferenceDescription(int j,UA_ReferenceDescription *ref,UAReferenceDescription *BrowseResult){ | ||||||
|  | 	memcpy(BrowseResult[j].BrowseName.data,ref->browseName.name.data,ref->browseName.name.length); | ||||||
|  | 	BrowseResult[j].BrowseName.length=ref->browseName.name.length; | ||||||
|  | 	memcpy(BrowseResult[j].DisplayName.Locale,ref->displayName.locale.data,ref->displayName.locale.length); | ||||||
|  | 	memcpy(BrowseResult[j].DisplayName.Text.data,ref->displayName.text.data,ref->displayName.text.length); | ||||||
|  | 	BrowseResult[j].IsForward=ref->isForward; | ||||||
|  | 	BrowseResult[j].NodeClass=ref->nodeClass; | ||||||
|  | 	BrowseResult[j].NodeID.ID.NamespaceIndex=ref->nodeId.nodeId.namespaceIndex; | ||||||
|  | 	BrowseResult[j].NodeID.ID.IdentifierType=ref->nodeId.nodeId.identifierType; | ||||||
|  | 	BrowseResult[j].NodeID.ID.Identifier.length=ref->nodeId.nodeId.identifier.string.length; | ||||||
|  | 	BrowseResult[j].NodeID.ID.Identifier.data=ref->nodeId.nodeId.identifier.string.data; | ||||||
|  | 	BrowseResult[j].NodeID.NamespaceURI.length=ref->nodeId.namespaceUri.length; | ||||||
|  | 	memcpy(BrowseResult[j].NodeID.NamespaceURI.data,ref->nodeId.namespaceUri.data,ref->nodeId.namespaceUri.length); | ||||||
|  | 	BrowseResult[j].NodeID.ServerIndex=ref->nodeId.serverIndex; | ||||||
|  | 	BrowseResult[j].ReferenceTypeID.NamespaceIndex=ref->referenceTypeId.namespaceIndex; | ||||||
|  | 	BrowseResult[j].ReferenceTypeID.IdentifierType=ref->referenceTypeId.identifierType; | ||||||
|  | 	BrowseResult[j].ReferenceTypeID.Identifier.length=ref->referenceTypeId.identifier.string.length; | ||||||
|  | 	//memcpy(BrowseResult[j].ReferenceTypeID.Identifier.data,ref->referenceTypeId.identifier.string.data,ref->referenceTypeId.identifier.string.length); | ||||||
|  | 	BrowseResult[j].TypeDefinition.ID.NamespaceIndex=ref->typeDefinition.nodeId.namespaceIndex; | ||||||
|  | 	BrowseResult[j].TypeDefinition.ID.IdentifierType=ref->typeDefinition.nodeId.identifierType; | ||||||
|  | 	BrowseResult[j].TypeDefinition.ID.Identifier.length=ref->typeDefinition.nodeId.identifier.string.length; | ||||||
|  | 	memcpy(BrowseResult[j].TypeDefinition.ID.Identifier.data,ref->typeDefinition.nodeId.identifier.string.data,ref->typeDefinition.nodeId.identifier.string.length); | ||||||
|  | 	BrowseResult[j].TypeDefinition.NamespaceURI.length=ref->typeDefinition.namespaceUri.length; | ||||||
|  | 	memcpy(BrowseResult[j].TypeDefinition.NamespaceURI.data,ref->typeDefinition.namespaceUri.data,ref->typeDefinition.namespaceUri.length); | ||||||
|  | 	BrowseResult[j].TypeDefinition.ServerIndex=ref->typeDefinition.serverIndex; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 UA_NodeGetHandleList(long ConnectionHdl,u16 NodeIDCount,UANodeID *NodeIDs,long *NodeHdls,u32 ErrorIDs[]){ | ||||||
|  | 	int i; | ||||||
|  | 	u32 retval=0; | ||||||
|  | 	UA_NodeId *NodeID=malloc(sizeof(UA_NodeId)*NodeIDCount); | ||||||
|  | 	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 ); | ||||||
|  |  | ||||||
|  | 		if(NodeIDs[i].IdentifierType>=0&&NodeIDs[i].IdentifierType<4){ | ||||||
|  | 			switch(NodeIDs[i].IdentifierType){ | ||||||
|  | 				case 0:NodeID[i]=UA_NODEID_NUMERIC(NodeIDs[i].NamespaceIndex,NodeIDs[i].Identifier.data);break; | ||||||
|  | 				case 1:NodeID[i]=UA_NODEID_STRING(NodeIDs[i].NamespaceIndex,NodeIDs[i].Identifier.data);break; | ||||||
|  | 				case 2:NodeID[i].identifierType=4; | ||||||
|  | 						NodeID[i].identifier.string.data=NodeIDs[i].Identifier.data; | ||||||
|  | 						NodeID[i].identifier.string.length=NodeIDs[i].Identifier.length; | ||||||
|  | 						NodeID[i].namespaceIndex=NodeIDs[i].NamespaceIndex;break; | ||||||
|  | 				case 3:NodeID[i]=UA_NODEID_BYTESTRING(NodeIDs[i].NamespaceIndex,NodeIDs[i].Identifier.data);break; | ||||||
|  | 			} | ||||||
|  | 			ErrorIDs[i]=0; | ||||||
|  | 		}else{ | ||||||
|  | 			ErrorIDs[i]=0xA0000301; | ||||||
|  | 			retval=ErrorIDs[i]; | ||||||
|  | 		} | ||||||
|  | 		// NodeID[i].identifier.string.data=NodeIDs[i].Identifier.data; | ||||||
|  | 		// NodeID[i].identifier.string.length=NodeIDs[i].Identifier.length; | ||||||
|  | 		// NodeID[i].namespaceIndex=NodeIDs[i].NamespaceIndex; | ||||||
|  | 		NodeHdls[i]=&NodeID[i]; | ||||||
|  | 		//UA_NodeId *NodeID1=malloc(sizeof(UA_NodeId)*NodeIDCount); | ||||||
|  | 		//UA_NodeId_delete(&NodeID[i]); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return retval; | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 UA_ReadList(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UANodeAdditionalInfo *NodeAddInfos,UA_Value *variables,u32 ErrorIDs[]){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	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; | ||||||
|  |             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) | ||||||
|  |         { | ||||||
|  |             printf("------------#3---------------\n"); | ||||||
|  |             if(response.resultsSize==NodeHdlCount) | ||||||
|  |             { | ||||||
|  |                 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) | ||||||
|  |                     { | ||||||
|  |                         printf("------------#5---------------\n"); | ||||||
|  |                         UA_DataValue res = response.results[i]; | ||||||
|  |                         if(!res.hasValue) | ||||||
|  |                         { | ||||||
|  |                             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) | ||||||
|  |       //  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; | ||||||
|  | 	int i; | ||||||
|  | 	UA_StatusCode retval=0; | ||||||
|  | 	if(NodeHdlCount>MAX_ELEMENTS_NODELIST){ | ||||||
|  | 		retval=0xA0000002; | ||||||
|  | 		return retval; | ||||||
|  | 	} | ||||||
|  | 	//UA_NodeId NODEid= UA_NODEID_STRING(1, "A1.the anwser2"); | ||||||
|  | 	//UA_Client_writeValueAttribute(c,NODEid,&in); | ||||||
|  | 	UA_WriteValue wValue[NodeHdlCount]; | ||||||
|  | 	for(i=0;i<NodeHdlCount;i++){ | ||||||
|  | 		UA_WriteValue_init(&wValue[i]); | ||||||
|  | 	 	wValue[i].nodeId = *(UA_NodeId*)NodeHdls[i]; | ||||||
|  | 	 	//wValue[i].nodeId = NODEid; | ||||||
|  | 	 	wValue[i].attributeId = NodeAddInfos[i].AttributeID; | ||||||
|  | 	 	if(NodeAddInfos[i].AttributeID == UA_ATTRIBUTEID_VALUE){ | ||||||
|  | 	 		UA_Variant in; | ||||||
|  | 			UA_Variant_init(&in); | ||||||
|  | 			UA_Variant_setScalar(&in,variables[i].data,&UA_TYPES[variables[i].type-1]); | ||||||
|  |          	wValue[i].value.value = in; | ||||||
|  | 			wValue[i].value.hasValue = true; | ||||||
|  | 	 	} | ||||||
|  | 	 	else{ | ||||||
|  | 	 		UA_Variant_setScalar(&wValue[i].value.value, (void*)(uintptr_t)variables[i].data, &UA_TYPES[variables[i].type-1]); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	UA_WriteRequest wReq; | ||||||
|  | 	UA_WriteRequest_init(&wReq); | ||||||
|  | 	wReq.nodesToWrite = &wValue[0]; | ||||||
|  | 	wReq.nodesToWriteSize = NodeHdlCount; | ||||||
|  |  | ||||||
|  | 	UA_WriteResponse wResp = UA_Client_Service_write(c, wReq); | ||||||
|  |  | ||||||
|  | 	retval = wResp.responseHeader.serviceResult; | ||||||
|  | 	if(retval==UA_STATUSCODE_GOOD){ | ||||||
|  | 		if(wResp.resultsSize==NodeHdlCount){ | ||||||
|  | 			for(i=0;i<NodeHdlCount;i++){ | ||||||
|  | 				ErrorIDs[i]=wResp.results[i]; | ||||||
|  | 			} | ||||||
|  | 		}else{ | ||||||
|  | 			retval=UA_STATUSCODE_BADUNEXPECTEDERROR; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	UA_WriteRequest_clear(&wResp); | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void readTest(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UA_Value *variables,u32 ErrorIDs[]) | ||||||
|  | { | ||||||
|  |     UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	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 = 1; | ||||||
|  |             //item[i].nodeId.namespaceIndex = 1; | ||||||
|  |             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) | ||||||
|  |         { | ||||||
|  |             printf("------------#3---------------\n"); | ||||||
|  |             printf("%d\n",response.resultsSize); | ||||||
|  |             if(response.resultsSize==NodeHdlCount) | ||||||
|  |             { | ||||||
|  |                 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) | ||||||
|  |                     { | ||||||
|  |                         printf("------------#5---------------\n"); | ||||||
|  |                         UA_DataValue res = response.results[i]; | ||||||
|  |                         if(!res.hasValue) | ||||||
|  |                         { | ||||||
|  |                             printf("------------#6---------------\n"); | ||||||
|  |                             UA_ReadResponse_clear(&response); | ||||||
|  |                             retval =  UA_STATUSCODE_BADUNEXPECTEDERROR; | ||||||
|  |                         } | ||||||
|  | 						else | ||||||
|  | 						{ | ||||||
|  |   							memcpy(variables[i].data,res.value.data,8); | ||||||
|  | 							variables[i].type=UA_TYPES_NODECLASS; | ||||||
|  | 						} | ||||||
|  | 						UA_free(res.value.data);   //?????   need or not | ||||||
|  | 					} | ||||||
|  |                 }  //end for | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |  | ||||||
|  |                 retval = UA_STATUSCODE_BADUNEXPECTEDERROR; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void writeTest(long ConnectionHdl,u16 NodeHdlCount,long *NodeHdls,UA_Value *variables,u32 ErrorIDs[]){ | ||||||
|  |     UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	int i; | ||||||
|  | 	UA_StatusCode retval=0; | ||||||
|  | 	if(NodeHdlCount>MAX_ELEMENTS_NODELIST){ | ||||||
|  | 		retval=0xA0000002; | ||||||
|  | 		return retval; | ||||||
|  | 	} | ||||||
|  | 	//UA_NodeId NODEid= UA_NODEID_STRING(1, "A1.the anwser2"); | ||||||
|  | 	//UA_Client_writeValueAttribute(c,NODEid,&in); | ||||||
|  | 	UA_WriteValue wValue[NodeHdlCount]; | ||||||
|  | 	for(i=0;i<NodeHdlCount;i++){ | ||||||
|  | 		UA_WriteValue_init(&wValue[i]); | ||||||
|  | 	 	wValue[i].nodeId = *(UA_NodeId*)NodeHdls[i]; | ||||||
|  | 	 	//wValue[i].nodeId = NODEid; | ||||||
|  | 	 	wValue[i].attributeId = 1; | ||||||
|  | 	 	UA_Variant_setScalar(&wValue[i].value.value, (void*)(uintptr_t)variables[i].data, &UA_TYPES[variables[i].type-1]); | ||||||
|  | 	} | ||||||
|  | 	UA_WriteRequest wReq; | ||||||
|  | 	UA_WriteRequest_init(&wReq); | ||||||
|  | 	wReq.nodesToWrite = &wValue[0]; | ||||||
|  | 	wReq.nodesToWriteSize = NodeHdlCount; | ||||||
|  |  | ||||||
|  | 	UA_WriteResponse wResp = UA_Client_Service_write(c, wReq); | ||||||
|  |  | ||||||
|  | 	retval = wResp.responseHeader.serviceResult; | ||||||
|  | 	if(retval==UA_STATUSCODE_GOOD){ | ||||||
|  | 		if(wResp.resultsSize==NodeHdlCount){ | ||||||
|  | 			for(i=0;i<NodeHdlCount;i++){ | ||||||
|  | 				ErrorIDs[i]=wResp.results[i]; | ||||||
|  | 			} | ||||||
|  | 		}else{ | ||||||
|  | 			retval=UA_STATUSCODE_BADUNEXPECTEDERROR; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	UA_WriteRequest_clear(&wResp); | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 SubscriptionCreate(long ConnectionHdl,bool PublishingEnable,u16 Priority,long *SubscriptionHdl,TIME *PublishingInterval){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default(); | ||||||
|  | 	request.requestedPublishingInterval=*PublishingInterval; | ||||||
|  | 	request.publishingEnabled=PublishingEnable; | ||||||
|  | 	request.priority=Priority; | ||||||
|  | 	UA_CreateSubscriptionResponse *response=malloc(sizeof(UA_CreateSubscriptionResponse)); | ||||||
|  | 	*response = UA_Client_Subscriptions_create(c,request,NULL,NULL,NULL); | ||||||
|  | 	u32 retval=response[0].responseHeader.serviceResult; | ||||||
|  | 	if(retval!=UA_STATUSCODE_GOOD){ | ||||||
|  | 		UA_CreateSubscriptionResponse_clear(&response[0]); | ||||||
|  | 		return retval; | ||||||
|  | 	} | ||||||
|  | 	printf("response[0].subscripstionId is %x\n",response[0].subscriptionId); | ||||||
|  | 	*SubscriptionHdl=(long *)response[0].subscriptionId; | ||||||
|  | 	UA_CreateSubscriptionResponse_clear(&response[0]); | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 UA_SubscriptionDelete(long ConnectionHdl,long SubscriptionHdl){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	u32 retval=UA_Client_Subscriptions_deleteSingle(c,SubscriptionHdl); | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 SubscriptionHdlzu2[100]; | ||||||
|  | u32 MonItemHdlzu[100]; | ||||||
|  | UAMonitoredVariables MonVlaue[100]; | ||||||
|  | int MontNum=0; | ||||||
|  | static void handler_DataChanged(UA_Client *client, UA_UInt32 subId, | ||||||
|  |                                     void *subContext, UA_UInt32 monId, | ||||||
|  |                                     void *monContext, UA_DataValue *value) | ||||||
|  | { | ||||||
|  |     UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Received Notification"); | ||||||
|  |  | ||||||
|  |     UA_NodeId *ptr = (UA_NodeId*)monContext; | ||||||
|  | 	SubscriptionHdlzu2[MontNum]=subId; | ||||||
|  | 	MonItemHdlzu[MontNum]=monId; | ||||||
|  | 	MonVlaue[MontNum].Values.data=value->value.data; | ||||||
|  | 	MonVlaue[MontNum].Values.type=value->value.type->typeId.identifier.numeric; | ||||||
|  | 	UA_DateTime now = UA_DateTime_nowMonotonic(); | ||||||
|  | 	MonVlaue[MontNum].TimeStamps=now; | ||||||
|  | 	MonVlaue[MontNum].NewValuesCount=1; | ||||||
|  | 	//MonVlaue[MontNum].NodeQualityIDs[0]=0; | ||||||
|  |     //UA_Int32 currentValue = *(UA_Int32*)(value->value.data); | ||||||
|  |  | ||||||
|  | 	MontNum++; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 MonitoredItemAddList(long ConnectionHdl,long SubscriptionHdl,u16 NodeHdlCount, | ||||||
|  | 							long *NodeHdls,UAMonitoringSyncMode SyncMode, | ||||||
|  | 							UANodeAdditionalInfo *NodeAddInfos,u32 *MonitoredItemHdls,u32 ErrorIDs[]){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	u32 retval=0; | ||||||
|  | 	if(NodeHdlCount>MAX_ELEMENTS_MONITORLIST){ | ||||||
|  | 		retval=0xA0000002; | ||||||
|  | 		return retval; | ||||||
|  | 	} | ||||||
|  | 	if(SyncMode==UAMS_ControllerSync){ | ||||||
|  | 		for(int i=0;i<NodeHdlCount;i++){ | ||||||
|  | 			UA_NodeId *NodeID=(UA_NodeId*)NodeHdls[i]; | ||||||
|  | 			UA_MonitoredItemCreateRequest monRequest=UA_MonitoredItemCreateRequest_default(*NodeID); | ||||||
|  | 			monRequest.itemToMonitor.attributeId = NodeAddInfos[i].AttributeID; | ||||||
|  | 			UA_MonitoredItemCreateResult monResponse= | ||||||
|  | 			UA_Client_MonitoredItems_createDataChange(c,SubscriptionHdl,UA_TIMESTAMPSTORETURN_BOTH, | ||||||
|  | 														monRequest,NodeID,handler_DataChanged,NULL); | ||||||
|  | 		if(monResponse.statusCode!=UA_STATUSCODE_GOOD){ | ||||||
|  | 			retval=monResponse.statusCode; | ||||||
|  | 		} | ||||||
|  | 		ErrorIDs[i]=monResponse.statusCode; | ||||||
|  | 		MonitoredItemHdls[i]=monResponse.monitoredItemId; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return retval; | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | u32 Browse(long ConnectionHdl,UABrowseDescription BrowseDescription,u32 ContinuationPointIn,u16 *BrowseResultCount,UAReferenceDescription *BrowseResult,u32 ContinuationPointOut){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	UA_StatusCode retval; | ||||||
|  | 	if(ContinuationPointIn==0){ | ||||||
|  | 		UA_BrowseRequest bReq; | ||||||
|  | 		UA_BrowseRequest_init(&bReq); | ||||||
|  | 		bReq.requestedMaxReferencesPerNode = 0; | ||||||
|  | 		bReq.nodesToBrowse = UA_BrowseDescription_new(); | ||||||
|  | 		bReq.nodesToBrowseSize = 1; | ||||||
|  | 		switch(BrowseDescription.StartingNodeID.IdentifierType){ | ||||||
|  | 			case 0:bReq.nodesToBrowse[0].nodeId=UA_NODEID_NUMERIC(BrowseDescription.StartingNodeID.NamespaceIndex,BrowseDescription.StartingNodeID.Identifier.data);break; | ||||||
|  | 			case 1:bReq.nodesToBrowse[0].nodeId=UA_NODEID_STRING(BrowseDescription.StartingNodeID.NamespaceIndex,BrowseDescription.StartingNodeID.Identifier.data);break; | ||||||
|  | 			case 2:bReq.nodesToBrowse[0].nodeId.identifierType=4; | ||||||
|  | 					bReq.nodesToBrowse[0].nodeId.identifier.string.data=BrowseDescription.StartingNodeID.Identifier.data; | ||||||
|  | 					bReq.nodesToBrowse[0].nodeId.identifier.string.length=BrowseDescription.StartingNodeID.Identifier.length; | ||||||
|  | 					bReq.nodesToBrowse[0].nodeId.namespaceIndex=BrowseDescription.StartingNodeID.NamespaceIndex;break; | ||||||
|  | 			case 3:bReq.nodesToBrowse[0].nodeId=UA_NODEID_BYTESTRING(BrowseDescription.StartingNodeID.NamespaceIndex,BrowseDescription.StartingNodeID.Identifier.data);break; | ||||||
|  | 			default:retval=UA_STATUSCODE_BADUNEXPECTEDERROR; | ||||||
|  | 		} | ||||||
|  | 		if(BrowseDescription.ReferenceTypeID.Identifier.length!=NULL) | ||||||
|  | 			bReq.nodesToBrowse[0].referenceTypeId=UA_NODEID_NUMERIC(0,BrowseDescription.ReferenceTypeID.Identifier.length); | ||||||
|  | 		if(BrowseDescription.ResultMask==1||BrowseDescription.ResultMask==2||BrowseDescription.ResultMask==4||BrowseDescription.ResultMask==8||BrowseDescription.ResultMask==16) | ||||||
|  | 		 	bReq.nodesToBrowse[0].resultMask = BrowseDescription.ResultMask; | ||||||
|  | 		if(BrowseDescription.Direction==2||BrowseDescription.Direction==1||BrowseDescription.Direction==0) | ||||||
|  | 		 	bReq.nodesToBrowse[0].browseDirection = BrowseDescription.Direction; | ||||||
|  | 		if(BrowseDescription.IncludeSubtypes==0||BrowseDescription.IncludeSubtypes==1) | ||||||
|  | 		 	bReq.nodesToBrowse[0].includeSubtypes = BrowseDescription.IncludeSubtypes; | ||||||
|  | 		if(BrowseDescription.NodeClass!=NULL) | ||||||
|  | 		 	bReq.nodesToBrowse[0].nodeClassMask = BrowseDescription.NodeClass; | ||||||
|  |  | ||||||
|  | 		UA_BrowseResponse bResp = UA_Client_Service_browse(c, bReq); | ||||||
|  | 		retval = bResp.responseHeader.serviceResult; | ||||||
|  | 		if(retval == UA_STATUSCODE_GOOD) { | ||||||
|  | 			if(bResp.results[0].referencesSize<=MAX_ELEMENTS_NODELIST){ | ||||||
|  | 				*BrowseResultCount=bResp.results[0].referencesSize; | ||||||
|  | 				for(size_t j = 0; j < bResp.results[0].referencesSize; ++j){ | ||||||
|  | 					UA_ReferenceDescription *ref = &bResp.results[0].references[j]; | ||||||
|  | 					saveUA_ReferenceDescriptionToUAReferenceDescription(j,ref,BrowseResult); | ||||||
|  | 				} | ||||||
|  | 				ContinuationPointOut=0; | ||||||
|  | 			}else if(bResp.results[0].referencesSize>MAX_ELEMENTS_NODELIST){ | ||||||
|  | 				*BrowseResultCount=MAX_ELEMENTS_NODELIST; | ||||||
|  | 				UA_ReferenceDescription *ref1=malloc(sizeof(UA_ReferenceDescription)*(bResp.results[0].referencesSize-MAX_ELEMENTS_NODELIST+1)); | ||||||
|  | 				for(size_t j = 0; j < MAX_ELEMENTS_NODELIST; ++j){ | ||||||
|  | 					UA_ReferenceDescription *ref = &bResp.results[0].references[j]; | ||||||
|  | 					saveUA_ReferenceDescriptionToUAReferenceDescription(j,ref,BrowseResult); | ||||||
|  | 				} | ||||||
|  | 				for(size_t j=MAX_ELEMENTS_NODELIST+1;j<=bResp.results[0].referencesSize;j++){ | ||||||
|  | 					ref1[j-MAX_ELEMENTS_NODELIST]=bResp.results[0].references[j]; | ||||||
|  | 				} | ||||||
|  | 				ref1[0].browseName.namespaceIndex=bResp.results[0].referencesSize-MAX_ELEMENTS_NODELIST; | ||||||
|  | 				ContinuationPointOut=(u32 *)ref1; | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 		} | ||||||
|  | 		//UA_BrowseRequest_clear(&bReq); | ||||||
|  |     	//UA_BrowseResponse_clear(&bResp); | ||||||
|  | 	}else{ | ||||||
|  | 		UA_ReferenceDescription *ref=(UA_ReferenceDescription *)ContinuationPointOut; | ||||||
|  | 		if(ref[0].browseName.namespaceIndex<=MAX_ELEMENTS_NODELIST){ | ||||||
|  | 			for(size_t j = 1; j < ref[0].browseName.namespaceIndex+1; ++j){ | ||||||
|  | 				saveUA_ReferenceDescriptionToUAReferenceDescription(j,ref,BrowseResult); | ||||||
|  | 			} | ||||||
|  | 			ContinuationPointOut=0; | ||||||
|  | 		}else if(ref[0].browseName.namespaceIndex>MAX_ELEMENTS_NODELIST){ | ||||||
|  | 			UA_ReferenceDescription *ref1=malloc(sizeof(UA_ReferenceDescription)*(ref[0].browseName.namespaceIndex-MAX_ELEMENTS_NODELIST+1)); | ||||||
|  | 			for(size_t j = 1; j < MAX_ELEMENTS_NODELIST+1; ++j){ | ||||||
|  | 				saveUA_ReferenceDescriptionToUAReferenceDescription(j,ref,BrowseResult); | ||||||
|  | 			} | ||||||
|  | 			for(size_t j=MAX_ELEMENTS_NODELIST+1;j<=ref[0].browseName.namespaceIndex;j++){ | ||||||
|  | 				ref1[j-MAX_ELEMENTS_NODELIST]=ref[j]; | ||||||
|  | 			} | ||||||
|  | 			ref1[0].browseName.namespaceIndex=ref[0].browseName.namespaceIndex-MAX_ELEMENTS_NODELIST; | ||||||
|  | 			ContinuationPointOut=(u32 *)ref1; | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | u32 UA_MethodGetHandleList(long ConnectionHdl,u16 NodeIDCount,UANodeID *ObjectNodeIDs,UANodeID *MethodNodeIDs,long *MethodHdls,u32 ErrorIDs[]){ | ||||||
|  | 	UA_Client *c = (UA_Client*)ConnectionHdl; | ||||||
|  | 	u32 retval=0; | ||||||
|  | 	if(NodeIDCount>MAX_ELEMENTS_METHOD){ | ||||||
|  | 		retval=0xA0000002; | ||||||
|  | 		return retval; | ||||||
|  | 	} | ||||||
|  | 	UA_CallRequest *request=malloc(sizeof(UA_CallRequest)); | ||||||
|  | 	UA_CallRequest_init(request); | ||||||
|  | 	UA_CallMethodRequest *item = (UA_CallMethodRequest*)malloc(sizeof(UA_CallMethodRequest) * NodeIDCount); | ||||||
|  | 	for(int i = 0;i<NodeIDCount;i++){ | ||||||
|  |  | ||||||
|  | 		UA_CallMethodRequest_init(&item[i]); | ||||||
|  |  | ||||||
|  | 		switch(ObjectNodeIDs[i].IdentifierType){ | ||||||
|  | 			case 0:item[i].objectId=UA_NODEID_NUMERIC(ObjectNodeIDs[i].NamespaceIndex,ObjectNodeIDs[i].Identifier.data);break; | ||||||
|  | 			case 1:item[i].objectId=UA_NODEID_STRING(ObjectNodeIDs[i].NamespaceIndex,ObjectNodeIDs[i].Identifier.data);break; | ||||||
|  | 			case 2:item[i].objectId.identifierType=4; | ||||||
|  | 					item[i].objectId.identifier.string.data=ObjectNodeIDs[i].Identifier.data; | ||||||
|  | 					item[i].objectId.identifier.string.length=ObjectNodeIDs[i].Identifier.length; | ||||||
|  | 					item[i].objectId.namespaceIndex=ObjectNodeIDs[i].NamespaceIndex;break; | ||||||
|  | 			case 3:item[i].objectId=UA_NODEID_BYTESTRING(ObjectNodeIDs[i].NamespaceIndex,ObjectNodeIDs[i].Identifier.data);break; | ||||||
|  | 			default:ErrorIDs[i]==0xA0000301;retval=ErrorIDs[i];break; | ||||||
|  | 		} | ||||||
|  | 		switch(MethodNodeIDs[i].IdentifierType){ | ||||||
|  | 			case 0:item[i].methodId=UA_NODEID_NUMERIC(MethodNodeIDs[i].NamespaceIndex,MethodNodeIDs[i].Identifier.data);break; | ||||||
|  | 			case 1:item[i].methodId=UA_NODEID_STRING(MethodNodeIDs[i].NamespaceIndex,MethodNodeIDs[i].Identifier.data);break; | ||||||
|  | 			case 2:item[i].methodId.identifierType=4; | ||||||
|  | 					item[i].methodId.identifier.string.data=MethodNodeIDs[i].Identifier.data; | ||||||
|  | 					item[i].methodId.identifier.string.length=MethodNodeIDs[i].Identifier.length; | ||||||
|  | 					item[i].methodId.namespaceIndex=MethodNodeIDs[i].NamespaceIndex;break; | ||||||
|  | 			case 3:item[i].methodId=UA_NODEID_BYTESTRING(MethodNodeIDs[i].NamespaceIndex,MethodNodeIDs[i].Identifier.data);break; | ||||||
|  | 			default:ErrorIDs[i]==0xA0000301;retval=ErrorIDs[i];break; | ||||||
|  | 		} | ||||||
|  | 		//UA_CallMethodRequest_clear(&item); | ||||||
|  | 		 | ||||||
|  |  | ||||||
|  | 		//UA_CallRequest_clear(request); | ||||||
|  | 		 | ||||||
|  | 		MethodHdls[i]=(long *)&item[i]; | ||||||
|  | 		printf("%lld\n",MethodHdls[i]); | ||||||
|  | 		ErrorIDs[i]=0; | ||||||
|  | 	} | ||||||
|  | 	request->methodsToCall = &item; | ||||||
|  | 	request->methodsToCallSize = NodeIDCount; | ||||||
|  |  | ||||||
|  | 	return retval; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | int main(int argc, char *argv[]) { | ||||||
|  | 	 | ||||||
|  |     // long connecthdl; | ||||||
|  | 	// Connect(&connecthdl); | ||||||
|  | 	UA_Client *client = UA_Client_new(); | ||||||
|  |     UA_ClientConfig_setDefault(UA_Client_getConfig(client)); | ||||||
|  |     UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); | ||||||
|  |     if(retval != UA_STATUSCODE_GOOD) { | ||||||
|  |         UA_Client_delete(client); | ||||||
|  |         return (int)retval; | ||||||
|  |     } | ||||||
|  | 	long hdl = (long)client; | ||||||
|  | 	UANodeID nodes[4]; | ||||||
|  | 	long nodeHdls[4] = {0}; | ||||||
|  | 	u32 errors[4]; | ||||||
|  | 	printf("Browsing nodes in objects folder:\n"); | ||||||
|  |     UA_BrowseRequest bReq; | ||||||
|  |     UA_BrowseRequest_init(&bReq); | ||||||
|  |     bReq.requestedMaxReferencesPerNode = 0; | ||||||
|  |     bReq.nodesToBrowse = UA_BrowseDescription_new(); | ||||||
|  |     bReq.nodesToBrowseSize = 1; | ||||||
|  |     bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */ | ||||||
|  |     bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */ | ||||||
|  |     UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq); | ||||||
|  | 	printf("\n\n 0x%08x \n\n",bResp.responseHeader.serviceResult); | ||||||
|  |     printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME"); | ||||||
|  |     for(size_t i = 0; i < bResp.resultsSize; ++i) { | ||||||
|  |         for(size_t j = 0; j < bResp.results[i].referencesSize; ++j) { | ||||||
|  |             UA_ReferenceDescription *ref = &(bResp.results[i].references[j]); | ||||||
|  |             if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) { | ||||||
|  |                 printf("%-9u %-16u %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex, | ||||||
|  |                        ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length, | ||||||
|  |                        ref->browseName.name.data, (int)ref->displayName.text.length, | ||||||
|  |                        ref->displayName.text.data); | ||||||
|  |             } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) { | ||||||
|  |                 printf("%-9u %-16.*s %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex, | ||||||
|  |                        (int)ref->nodeId.nodeId.identifier.string.length, | ||||||
|  |                        ref->nodeId.nodeId.identifier.string.data, | ||||||
|  |                        (int)ref->browseName.name.length, ref->browseName.name.data, | ||||||
|  |                        (int)ref->displayName.text.length, ref->displayName.text.data); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  | 			printf("\n%lld\n\n",ref->nodeId.nodeId.identifier.numeric); | ||||||
|  |  | ||||||
|  | 			nodes[j].Identifier.length = ref->nodeId.nodeId.identifier.string.length; | ||||||
|  | 			if(nodes[j].Identifier.length > 0) { | ||||||
|  |     			nodes[j].Identifier.data = (u8*)malloc(nodes[j].Identifier.length); | ||||||
|  | 				snprintf(nodes[j].Identifier.data, 64, "%lld", (long long)ref->nodeId.nodeId.identifier.numeric); | ||||||
|  |     			 | ||||||
|  | 			} | ||||||
|  | 			nodes[j].IdentifierType = ref->nodeId.nodeId.identifierType; | ||||||
|  | 			nodes[j].NamespaceIndex = ref->nodeId.nodeId.namespaceIndex; | ||||||
|  |             /* TODO: distinguish further types */ | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     UA_BrowseRequest_clear(&bReq); | ||||||
|  |     UA_BrowseResponse_clear(&bResp); | ||||||
|  |  | ||||||
|  | 	UANodeID ObjectNodeIDs[4]; | ||||||
|  | 	UANodeID MethodNodeIDs[1]; | ||||||
|  | 	long MethodHdls[4]; | ||||||
|  | 	u32 ErrorIDs[1]; | ||||||
|  | 	UA_MethodGetHandleList(hdl,4,nodes,MethodNodeIDs,MethodHdls,ErrorIDs); | ||||||
|  | 	printf("1111\n"); | ||||||
|  | 	printf("%lld\n",MethodHdls[1]); | ||||||
|  | 	 | ||||||
|  |     /* Clean up */ | ||||||
|  |     UA_Client_disconnect(client); | ||||||
|  |     UA_Client_delete(client); | ||||||
|  | 	 | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user