⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blob1.c

📁 RT-LAB分布式实时仿真平台下API函数的调用例程
💻 C
字号:
/****************************************************************************

		Example using RT-LAB API.
		Gives the main step in order to connect and receive data from
		a model.

*****************************************************************************/



#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

// Header for getcwd() is platform-dependent
#if defined(WIN32)
	#include <direct.h>
#elif defined(__linux__)
	#include <unistd.h>
	#define _MAX_PATH 256
#endif
#include "OpalApi.h"

void	PrintError(int rc, char *funcName);

#define FALSE 0
#define TRUE 1

#define	BLOB_ID		"my_blob_id"

int main(void)
{
	char modelName[_MAX_PATH] = "blob1.mdl";
	int					rc;
	OP_API_INSTANCE_ID	instId;
	short				state;
	OP_GET_BLOBS		*pBlob;
	double				*pBlobVal;

	/**************************************************************************
	*
	*							CONNECTION STEP
	*
	***************************************************************************/
	
	// The state's variable give the state of the model (loaded, running , ...) 
	rc = OpalConnectByName(modelName, NULL, TRUE, FALSE, FALSE, &instId, &state);
	if(EOK != rc)
	{
		PrintError(rc, "OpalConnectByName");
		return(rc);
	}
	printf("- The connection with '%s' is completed.\n", modelName);
	switch(state)
	{
		case MODEL_PAUSED:
			printf("- The model is paused.\n");
			break;
		case MODEL_RUNNING:
			printf("- The model is running.\n");
			break;
		default:
			break;
	}
	
	rc = OpalGetBlobsControl(TRUE);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetBlobControl");
		return(rc);
	}
	printf("- The blob control is acquired.\n");

	rc = OpalGetBlob(BLOB_ID, &pBlob);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetBlob");
		return(rc);
	}
	// This blob contains only one double.
	// There is only one subsys for this blob
	pBlobVal = (double *) pBlob->ssBlobList[0].pData;

	printf("- The blob returns value : %f.\n", *pBlobVal);
	
	*pBlobVal += 1;
	rc = OpalSendBlob(BLOB_ID, pBlobVal, sizeof(double));
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetBlob");
		return(rc);
	}
	printf("- The blob has been sent with value : %f.\n", *pBlobVal);

	rc = OpalGetBlob(BLOB_ID, &pBlob);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetBlob");
		return(rc);
	}
	// This blob contains only one double.
	// There is only one subsys for this blob
	pBlobVal = (double *) pBlob->ssBlobList[0].pData;

	printf("- The blob returns value : %f.\n", *pBlobVal);

	rc = OpalGetBlobsControl(FALSE);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetBlobControl");
		return(rc);
	}
	printf("- The blob control is released.\n");

	OpalDisconnect();

	return 0;
}


void	PrintError(int rc, char *funcName)
{
	char			buf[512];
	unsigned short	len;

	OpalGetLastErrMsg(buf, sizeof(buf), &len);
	printf("Error %s (code %d) from %s\n", buf, rc, funcName);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -