UserVariable complete code
This page contains the complete code of UserVariables.
In Log.cpp
, your UserVariable code should be like this:
Copy
#include "RT_Project_01.h"
#include <wchar.h>
using namespace std;
int CreateUserVariable()
{
/*------Add variables------*/
/*Declare three instances of UserVariable. These variables are in share
memory. They can be used by KS APIs, KS tools, and user applications.*/
//u = user.
UserVariable uTemperature = { 0, L"Temperature", NULL, logDouble };
UserVariable uVoltage = { 0, L"Voltage", NULL, logDouble };
UserVariable uWarning = { 0, L"Warning", NULL, logBool };
//Add three user variables to the KS Subsystem.
KsError nRet = AddVariable(&uTemperature);
if (nRet != errNoError)
RtPrintf("AddVariable failed: %x\n", nRet);
nRet = AddVariable(&uVoltage);
if (nRet != errNoError)
RtPrintf("AddVariable failed: %x\n", nRet);
nRet = AddVariable(&uWarning);
if (nRet != errNoError)
RtPrintf("AddVariable failed: %x\n\n", nRet);
//Set a default value to each variable.
*((double*)uTemperature.Value) = 1;
*((double*)uVoltage.Value) = 1;
*((BOOL*)uWarning.Value) = FALSE;
//Use string to display uWarning's value.
string uWarningText = "NULL";
if (*((BOOL*)uWarning.Value))
uWarningText = "TRUE";
else
uWarningText = "FALSE";
printf("Temperature: %f\n", *((double*)uTemperature.Value));
printf("Voltage: %f\n", *((double*)uVoltage.Value));
RtPrintf("Warning: %s\n\n", uWarningText.c_str());
/*------Get a variable------*/
RtPrintf("Get user variables one by one.\n\n");
//Pass the address of the variables to GetVariable.
GetVariable(&uTemperature);
GetVariable(&uVoltage);
GetVariable(&uTemperature);
/*------Get variables------*/
RtPrintf("Get the list of user variables.\n\n");
//Declare the variables for the buffer length and the total of the variables.
int BufferLength = 3;
int Variables = 0;
/*There are two ways to allocate memory space to store the list of user variables.
One is malloc() - free(). The other is array.*/
//UserVariable *Space = (UserVariable*)malloc(BufferLength * sizeof(UserVariable));
//Declare an array to store the list of user variables. Max is 64.
UserVariable Space[3];
//The types of a log.
wstring logType[11] = { L"logBool", L"logByte", L"logSInt", L"logWord", L"logInt",
L"logDWord", L"logDInt", L"logFloat", L"logLWord", L"logLInt",
L"logDouble" };
//The array used to store the types of user variables.
wstring Type[3] = { L"NULL" };
GetVariables(BufferLength, &Variables, Space);
RtPrintf("Total variables: %d\n\n", Variables);
//Convert the Type value into string.
for (int i = 0; i < 3; i++) //For Type array.
{
for (int j = 0; j < 11; j++) //For logType array.
{
if (Space[i].Type == j)
Type[i] = logType[j]; //Use Type elements to save the result.
}
}
//Display each user variable's attributes.
wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
Space[0].Id, Space[0].Name, *((double*)Space[0].Value), Type[0].c_str());
wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
Space[1].Id, Space[1].Name, *((double*)Space[1].Value), Type[1].c_str());
wprintf(L"Variables details: ID: %d, Name: %s, Value: %d, Type: %s\n\n",
Space[2].Id, Space[2].Name, *((BOOL*)Space[2].Value), Type[2].c_str());
//Free the space allocated to read user variables.
//If you use malloc(), you need to use free().
//free(Space);
/*------Remove variables------*/
RtPrintf("Remove user variables.\n\n");
//Remove the added user variables.
RemoveVariable(uTemperature.Id);
RemoveVariable(uVoltage.Id);
RemoveVariable(uWarning.Id);
return 0;
}