ForceInput

In ReadInput, ReadOutput, and WriteOutput, you have learned how to read and write data into a real I/O module. In this section, you'll learn how to use ForceInput to write data into a simulated I/O module and read it.

ForceInput functions are for simulated I/O modules only. Because simulated I/O modules don't have any data to send back, you need to write something into them, and then you have the simulated data to read.

The following process shows how to use a ForceInput function:

SetConfiguredIoCount -> Create an instance of SlaveStatus structure -> Set input and output length of the instance -> ConfigureIo -> ForceInput function -> ReadInput function

The process of using a ForceInput function is almost the same as the way you create simulated I/O modules, which we have talked about in Chapter 2 > Start the KINGSTAR Subsystem, step 4. Since we have created simulated I/O modules in SystemInitialization.cpp, we can use a ForceInput function, such as ForceInputWord, to write data into a simulated I/O module. Remember that simulated I/O modules must be created before Start. We then use ReadInputWord to read the value to check whether the value is written successfully.

In IOModule.cpp, under ReadInputOutput, add the following code:

Copy
VOID ForceInput(int Index, int Offset, int Value)
{
    WORD value;    //Declare a WORD variable.
    int nRet = 0;

    nRet = ForceInputWord(Index, Offset, Value);
    nRet = ReadInputWord(Index, Offset, &value);
    RtPrintf("Simulated IO module index: %d, offset %d, the input value is %d.\n", Index, Offset, value);
    RtPrintf("\n");
}

To let you use ForceInput flexibly, we use three parameters, Index, Offset, and Value. This way, when you call this function, you can specify the index, offset, and value of an I/O module.