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

📄 basic_example1.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

int main(void)
{
	char modelName[_MAX_PATH] = "basic_example1.mdl";
	int					rc;
	OP_API_INSTANCE_ID	instId;
	short				state;
	unsigned short		realTimeMode;

	/**************************************************************************
	*
	*							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 = OpalGetSystemControl(TRUE);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetSystemControl");
		return(rc);
	}
	printf("- The system control is acquired.\n");

	// Verify that the model is running
	if(state == MODEL_RUNNING)
	{
		rc = OpalPause();
		if(EOK != rc)
		{
			PrintError(rc, "OpalPause");
			return(rc);
		}
	}
	else if(state == MODEL_PAUSED)
	{
		rc = OpalExecute(1.0);
		if(EOK != rc)
		{
			PrintError(rc, "OpalExecute");
			return(rc);
		}
	}
	
	rc = OpalGetModelState(&state, &realTimeMode);
	if(EOK != rc)
	{
		PrintError(rc, "GetModelState");
		return(rc);
	}
	
	switch(state)
	{
		case MODEL_PAUSED:
			printf("- The model is now paused.\n");
			break;
		case MODEL_RUNNING:
			printf("- The model is now running.\n");
			break;
		default:
			break;
	}

	rc = OpalGetSystemControl(FALSE);
	if(EOK != rc)
	{
		PrintError(rc, "OpalGetSystemControl");
		return(rc);
	}
	printf("- The system 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 + -