plctwincatstructured-text

Why do I not receive any data from Factory IO in my TwinCAT 3 project through Modbus?


I've set up the first Factory IO tutorial scenario, with only one input and one output (coil). I have Factory IO configured to use the "Modbus TCP/IP Server" with the following settings:

Host:             127.0.0.1
Port:             502
Slave ID:         255
Network adapter:  Software Loopback Interface 1
All other values at default

I run the simulation and leave it in a state where the one input (sensor) should be returning a value.

In my TwinCAT project, I have the following:

Declaration:

PROGRAM MAIN
VAR
    LFB_MBReadInputs : FB_MBReadInputs;
    LFB_MBWriteCoils : FB_MBWriteCoils;
    inputs : ARRAY [0..15] OF BYTE; 
    coils : ARRAY [0..15] OF BYTE;
END_VAR

Implementation:

// Get the status of the inputs
LFB_MBReadInputs(
    sIPAddr := '127.0.0.1',
    nTCPPort := 502,
    nUnitID := 255,
    nQuantity := 16,
    nMBAddr := 0,
    cbLength := SIZEOF(inputs),
    pDestAddr := ADR(inputs),
    bExecute := TRUE,
    tTimeout := T#1S,
    bBusy => ,
    bError => ,
    nErrId => ,
    cbRead => ,
 );
LFB_MBReadInputs(bExecute := FALSE);

In TwinCAT I do "Activate Configuration", "Restart TwinCAT in Run Mode", login, download the program to the virtual PLC, and hit Start. The program appears to be running, but the byte array inputs shows all zeroes.

I've tried switching to my actual IP address instead of home, I've refactored it down from multiple Functions down to just one "MAIN" function. Is my code wrong? Is there some setting I don't know about in either TwinCAT or Factory IO to allow this to work?

EDIT: I've explained the reason for my problem in my own answer, that it was because I hadn't installed the TCP/IP plugin to TwinCAT. However, it may help someone in the future to know that when I examined the "nErrId" field, I was getting a value of "6", which according to the documentation means that the Modbus server could not be reached. This is what lead me to examine the connectivity itself, since the IP address and port were correct. Also, in the end I had to switch to my actual network adapter instead of "Software Loopback Interface"


Solution

  • Sorry to answer my own question here, but thanks to @kolyur and @Roald, I managed to get it working.

    My issue was that the TwinCAT PLC could not see the Modbus Server running in Factory IO because TCP/IP connectivity was not enabled. I downloaded two modules for TwinCAT, one for TCP/IP client/server connectivity, and one for Modbus. Then, in my project I generated trial licenses for both of them and ran my project. I might have diagnosed it sooner if I'd checked the error code field after executing and noticed that the error code denoted that it could not reach the Modbus server.