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.

  1. 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.
  2. 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 };
  3. Use AddVariable to add these variables into KINGSTAR Subsystem.
  4. 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);

  5. Set a default value to each variable.
  6. Copy
    //Set a default value to each variable.
    *((double*)uTemperature.Value) = 1;
    *((double*)uVoltage.Value) = 1;
    *((BOOL*)uWarning.Value) = FALSE;
  7. 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.
  8. Copy
    //Use string to display uWarning's value.
    string uWarningText = "NULL";

    if (*((BOOL*)uWarning.Value))
        uWarningText = "TRUE";
    else
        uWarningText = "FALSE";
  9. Display the values to make sure the values are set successfully.
  10. 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());