Add variables
AddVariable adds user variables to the KINGSTAR Subsystem. These variables can be retrieved by other applications or functions later because they are in the shared memory. After adding variables, you can set their default values.
The following code is written in the function CreateUserVariables.
- To create user variables, we need to create instances of the UserVariable structure. The number of instances is based on how many user variables you want to create. In this guide, we create three variables. Notice that every Id is zero, because this value is specified by KINGSTAR. The Value field is an output for the system to read. Since there are no values in these variables now, we set them to NULL.
- Use AddVariable to add these variables into KINGSTAR Subsystem.
- Set a default value to each variable.
- The third variable
uWarning
's data type is BOOL, which returns zero or one. We replace the values with the strings TRUE or FALSE so we know it's a boolean immediately. - Display the values to make sure the values are set successfully.
Copy
/*------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 };
Copy
//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);
Copy
//Set a default value to each variable.
*((double*)uTemperature.Value) = 1;
*((double*)uVoltage.Value) = 1;
*((BOOL*)uWarning.Value) = FALSE;
Copy
//Use string to display uWarning's value.
string uWarningText = "NULL";
if (*((BOOL*)uWarning.Value))
uWarningText = "TRUE";
else
uWarningText = "FALSE";
Copy
printf("Temperature: %f\n", *((double*)uTemperature.Value));
printf("Voltage: %f\n", *((double*)uVoltage.Value));
RtPrintf("Warning: %s\n\n", uWarningText.c_str());
Complete code
The complete code should be as follows:
Copy
/*------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);
//Give 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());