2

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"

Alex Kelly
  • 21
  • 4
  • 1
    Hi Alex, welcome to StackOverflow. I've never really done something with TCP/IP server, but did you install the [TcpIpServer](https://infosys.beckhoff.com/english.php?content=../content/1033/tcpipserver/html/tcpipserver_overview.htm&id=)? And do you get any `nErrId`? – Roald Aug 21 '21 at 08:51
  • I changed my code to another example suggested elsewhere, where it calls FB_MBWriteSingleCoil once to attempt to turn on the single coil in the example. This time I checked the bError and nErrId values, and it's setting the error to "6", which according to documentation is "Target port not found – ADS server is not started or is not reachable." I've tried changing the ports and addresses around, still no luck. It may be some kind of software or setting I have to install in order for TwinCAT to work over TCP/IP, even though it's on the same device. – Alex Kelly Aug 24 '21 at 15:08
  • 1
    Did you install the TwinCAT Modbus function TF6250 on your runtime PC? `https://www.beckhoff.com/en-us/products/automation/twincat/tfxxxx-twincat-3-functions/tf6xxx-tc3-connectivity/tf6250.html` – kolyur Aug 24 '21 at 16:42
  • 1
    Thank you! That was the problem. I installed the two modules, the one for TCP/IP and one for Modbus. I had to generate the 7-day trial licenses for them, but once I did the FB_MBWriteSingleCoil function block worked perfectly! – Alex Kelly Aug 24 '21 at 19:28

2 Answers2

1

You have to give the function block time to do its work. By executing the FB with bExecute:=true and bExecute:=false in the same scan, it could be failing to run. Try eliminating your second LFB_MBReadInputs call, and modify the first one to use bExecute := NOT bBusy.

With FB_MBReadInputs, nQuantity refers to bits while cbLength is bytes. So only the first two elements of your array will be written to. Matching up Modbus addresses between devices can be tricky. An nMBAddr of 0 could equate to an address of 100000 or 100001 in the other device. As @Roald mentioned, check for an error code in the function block.

kolyur
  • 457
  • 2
  • 13
0

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.

Alex Kelly
  • 21
  • 4
  • Can you also add the error code to your original question? Then other people can easily see if they have the same issue. – Roald Aug 26 '21 at 11:57
  • Thank you, I edited my original question. At the moment, I'm still struggling to get it to work consistently, but at least I know that I'm on the right track. – Alex Kelly Aug 27 '21 at 12:06