ReadInput, ReadOutput, and WriteOutput

In this section, you'll learn how to use I/O control functions to access a real I/O module. Their uses are similar. The difference is that they read and write different unit of data. Once you know how to use one of them, you know how to use the rest.

The process for using I/O control functions

Before we read or write anything, we need to check whether the device is an input or output module, or both. Next, we need to check the length of the input and output. Finally, we use the functions to read and write the value. If you use WriteOutput to write the value into an output, you can use ReadOutput to check whether the value is written successfully. You can also use ReadInput or ReadOutput individually to see the value that has already been written into the specified location.

Display inputs and outputs of all I/O modules

Before we read or write anything into an input or output, we need to know its length. We use the SubsystemStatus structure to get the total number of I/O modules, and the SlaveStatus structure and GetIOByIndex to get the length of the inputs and outputs of all connected I/O modules. The code is written in GetAllInputsOutputs. You'll find that the functions used here are those you've learned in Chapter 2.

  1. In IOModule.cpp, add the following code to include RT_Project_01.h and IOModule.h.
  2. Copy
    #include "RT_Project_01.h"
    #include "IOModule.h"
  3. Under #include "IOModule.h", add the following code.
  4. Copy
    VOID GetAllInputsOutputs()
    {
        int ioCount = 0;    //Total IO modules.
        KsError nRet = errNoError;
        SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
        SlaveStatus Sts = { 0 };

        GetStatus(&Subsystem, NULL);
        ioCount = Subsystem.IOCount;
        RtPrintf("Total IO modules: %d\n\n", Subsystem.IOCount);

        if (nRet != errNoError)
        {
            RtPrintf("Unable to get I/O information: %x\n", nRet);
            return;
        }

        for (int i = 0; i < ioCount; i++)
        {
            nRet = GetIOByIndex(i, &Sts);
            RtPrintf("%s input length: %d\n", Sts.Name, Sts.InputLength);
            RtPrintf("%s output length: %d\n", Sts.Name, Sts.OutputLength);
            RtPrintf("\n");
        }
    }

Write and read a value from an output

To write data to an output, use WriteOutput functions. In this guide we use WriteOutputByte to write a bit value, and ReadOutputByte to check whether the value is written successfully.

In IOModule.cpp, add the following code:

Copy
VOID WriteReadOutput(int Index, int Offset, int Value)
{
    BYTE value = 0;
    KsError nRet = errNoError;
    SlaveStatus Sts = { 0 };

    nRet = GetIOByIndex(Index, &Sts);
    if (nRet != errNoError)
    {
        RtPrintf("Unable to get IO information: %x\n", nRet);
        return;
    }

    //Write a value into an I/O module.
    nRet = WriteOutputByte(Index, Offset, Value);

    //Read a value from the same I/O module.
    //Remember to change the data type of the value when you use different functions. 
    nRet = ReadOutputByte(Index, Offset, &value);

    //Print the written value of the I/O module.
    RtPrintf("IO module: %s, offset %d, the output value is %d.\n", Sts.Name, Offset, value);
    RtPrintf("\n");
}

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

Read a value from an input and output

To read data from an input or output, use ReadInput and ReadOutput. In this guide we use ReadInputBit and ReadOutputBit to read a bit value. This code block is very similar to WriteReadOutput. The difference is that this block is for you to get a readout from an I/O module. Notice that ReadInput and ReadOutput can be used with real and simulated I/O modules. In the ForceInput section, we'll talk about how to write and read data from a simulated I/O module.

In IOModule.cpp, add the following code:

Copy
VOID ReadInputOutput(int Index, int Offset)
{
    BOOL value = FALSE;
    KsError nRet = errNoError;
    SlaveStatus Sts = { 0 };

    nRet = GetIOByIndex(Index, &Sts);
    if (nRet != errNoError)
    {
        RtPrintf("Unable to get IO information: %x\n", nRet);
        return;
    }

    nRet = GetIOByIndex(Index, &Sts);
    nRet = ReadInputBit(Index, Offset, &value);
    RtPrintf("IO module: %s, offset %d, the input value is %d.\n", Sts.Name, Offset, value);
    nRet = ReadOutputBit(Index, Offset, &value);
    RtPrintf("IO module: %s, offset %d, the output value is %d.\n", Sts.Name, Offset, value);
    RtPrintf("\n");
}