I/O Control complete code

This page contains the complete code of I/O Control.

In IOModule.cpp, your I/O Control code should be as follows:

Copy
#include "RT_Project_01.h"
#include "IOModule.h"

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");
    }
}

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");
}

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");
}

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");
}