📄 basic_example2.c
字号:
/****************************************************************************
Example using RT-LAB API.
Gives the main step in order to load and execute a model.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.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);
int main(void)
{
char modelName[_MAX_PATH] = "basic_example2.mdl";
char modelPath[_MAX_PATH];
char modelPathName[_MAX_PATH*2];
#if defined(WIN32)
char relativePath[] = "\\..\\models\\simulink\\";
#elif defined(__linux__)
char relativePath[] = "/../models/simulink/";
#endif
OP_API_INSTANCE_ID instId;
double timeFactor;
int rc;
unsigned short realTimeMode;
if (NULL != getcwd(modelPath, sizeof(modelPath))
&& (strlen(modelPath) + strlen(relativePath) < _MAX_PATH))
{
strcat(modelPath, relativePath);
}
else
{
PrintError(0, "Invalid path");
return 1;
}
// Set current model
strcpy(modelPathName, modelPath);
strcat(modelPathName, modelName);
rc = OpalSetCurrentModel(modelPathName, NULL);
if(EOK != rc)
{
PrintError(rc, "OpalSetCurrentModel");
return(rc);
}
printf("- Setting of model '%s' is completed.\n", modelName);
// Load the model
timeFactor = 1.0;
realTimeMode = 2;
rc = OpalLoad(realTimeMode, &instId, timeFactor);
if(EOK != rc)
{
PrintError(rc, "OpalLoad");
return(rc);
}
printf("- Model %s is loaded.\n", modelName);
// Execute the model
rc = OpalExecute(timeFactor);
if(EOK != rc)
{
PrintError(rc, "OpalExecute");
return(rc);
}
printf("- Model %s is executed.\n", modelName);
// Reset the model
rc = OpalReset();
if(EOK != rc)
{
PrintError(rc, "OpalReset");
return(rc);
}
printf("- Model %s is reset.\n", modelName);
// Disconnect from the model
OpalDisconnect();
printf("- Model %s is disconnected.\n", modelName);
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 + -