Beckhoff Twincat 2

Hi guys,

Has anyone managed to get Twincat 2 to connect to Factory IO before?

I think this would be possible using OPC DA drivers but havent been able to get it working.

Kind Regards,

Dan

1 Like

Hi,

I am using TwinCAT 3 with ModBus. Its working fine! Give it a try. Modbus libs should be also included in TwinCat 2.
Sven

Hi Sven,

Could you give a very rough outline of how to set this up? It might be useful for users in the future.

Hello,

All you need is Modbus lib (Tc2_ModbusSrv) in your project.

Here is the configuration of my FactorIO which is my ModBus server.

Quick and dirty:

Declaration

PROGRAM F_004_FactoryIO_Modbus_ReadInputs
VAR
LAB_Data : ARRAY [0…15] OF BYTE; // 16 bytes = 128 bits
LFB_MBReadInputs : FB_MBReadInputs;
LD_cbRead : udint;
END_VAR

Programm:

LFB_MBReadInputs(
sIPAddr := 127.0.0.1,
nTCPPort := 505,
nUnitID := 255,
nQuantity := 16,
nMBAddr := 0,
cbLength := SIZEOF(LAB_Data),
pDestAddr := ADR(LAB_Data),
bExecute := true,
tTimeout := T#1S,
bBusy => ,
bError => ,
nErrId => ,
cbRead => ,
);
LFB_MBReadInputs(bExecute := false);
LFB_MBReadInputs(bExecute := true);

Your inputs are now available in the var “LAB_Data”.

Reading input reg you need the FB “FB_MBReadInputRegs”.
Writing calls it is “FB_MBWriteCoils” and for writing register its called “FB_MBWriteRegs”.

Hi there Sven,

I am a complete beginner with this program. I have created a new structured text POU with code similar to what you posted. I had to change a few things in your code to get it to compile such as the ’ ’ around the ip address.

PROGRAM MAIN
VAR
LAB_Data : ARRAY [0..15] OF BYTE;
LFB_MBReadInputs : FB_MBReadInputs;
LD_cbRead : UDINT;
x0: BOOL;
a: ARRAY [0..15] OF BYTE;
    END_VAR

and then in the program

LFB_MBReadInputs(
sIPAddr := '169.254.207.68',
nTCPPort := 505,
nUnitID := 255,
nQuantity := 16,
nMBAddr := 0,
cbLength := SIZEOF(LAB_Data),
pDestAddr := ADR(LAB_Data),
bExecute := TRUE,
tTimeout := T#1S,
bBusy => ,
bError => ,
nErrId => ,
cbRead => ,
   );
LFB_MBReadInputs(bExecute := FALSE);
LFB_MBReadInputs(bExecute := TRUE);

But I dont understand what you mean next about inputs available in the var “LAB_Data”.

Can you give an example of the program just to read and input and send an output?

I am a complete beginner with this program. I have created a new structured text POU with code similar to what you posted. I had to change a few things in your code to get it to compile such as the ’ ’ around the ip address

Sorry about that. My code looks a bit different and I tried to make it easier to understand and removed a lot of my own stuff. I made mistakes obviously.

To read inputs copy the code above. Your inputs like your sensors from FactoryIO are available in the LAB_Data array.

You can access input0 by using LAB_Data[0].0
input1 in FactoryIO would be LAB_Data[0].1

input8 would be LAB_Data[1].0 !

LAB_Data is an array of bytes (16 bytes so the index is from 0 to 15). Inputs are bools so you have to access LAB_Data array bitwise.

Any more questions?

Sorry I am far more of a beginner than that! Imagine I have a start button on input 0 and a conveyor on output 0. Could you show the code to put the conveyor on when the button is pressed?

Here you can find more informations about Modbus if needed: https://download.beckhoff.com/download/document/automation/twincat3/TF6250_TC3_Modbus_TCP_EN.pdf

I created a new project in TwinCat3 with just basic stuff (includes the FactoryIO scene too).
TwinCat3 is free btw. You can use it as much as you want with trial licences at no costs as long as you want.

I created 2 vars.

GAB_FactoryIO_Inputs : ARRAY [0..15] OF BYTE; // Inputs from FactoryIO
GAB_FactoryIO_Outputs : ARRAY [0..15] OF BYTE; // Outputs from FactoryIO

Inside the program “PIQ_Conveyor1” I just added this to run the conveyor:

// Input button starts the conveyor and turn on the light of the button
GAB_FactoryIO_Outputs[0].0 := GAB_FactoryIO_Inputs[0].0;
GAB_FactoryIO_Outputs[0].1 := GAB_FactoryIO_Inputs[0].0;

test.zip (784.7 KB)

I finally got it working on Twincat 3 with your assistance Sven! Thankyou so much for the assistance.

I am now going to try and get it to work with Twincat 2 (This older software specifically because on our site we have lots of Twincat 2 machines and no Twincat 3 based machines)

Do you happen to have any knowledge of Modbus in Twincat 2?

I don’t have experience with twincat 2. But it should be similar to twincat 3. It’s probably even the same modbus libary.
What problem do facing using the same code as for twincat 3?