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?

Hi Sven, sorry for reopening an old thread but I am trying to connect TC3 to Factory IO too. I am a newbie to the Beckhoff world so, bear with me…

Anyway, I have downloaded your project and set the Server in Factory IO as per your screenshot. I completely understand what you have done there, but for whatever reason the simulation still does not work.

In fact, both the read and write FBs output error No.6, which according to the manual corresponds to “Target Port Not Found”.

Any idea what I am missing?

Ok, I got this to work but I had to download the " TF6250 | TwinCAT 3 Modbus TCP" tool from Beckoff’s website.

Or at least I think it was that that solved the issue…

1 Like

I will refresh this topic. How to relate any bits from array with variables? For example, I want to use name “lamp” instead FactoryOut[0].0? Any ideas? I`ve tried everything… Thanks in advance

Hello user67, welcome to the forum!

I’m not sure if I understood what you mean by wanting to relate the bits from the arrays to the variables, but I’ve come up with two possible solutions that I hope will help you. They are annexed to this reply and include the TwinCAT3 solutions and the corresponding Factory I/O scenes.

In the first solution, the one named SingleVariables, I didn’t use any arrays, but, in order to write the coils corresponding to my two outputs I had to create two instances of the block FB_MBWriteCoils, one for each output. If you use one single block to write coils then it will only write one of the outputs. This program works. However, when you run the Factory I/O scene, you will notice that the button’s light flickers. This is not intended and I think it might be caused by the fact that there are two FB_MBWriteCoils.

In the second solution, named SingleVariablesAlternative, I did use arrays. By using arrays only one block FB_MBWriteCoils is needed and, in the Factory I/O scene, the button’s light no longer flickers. However, in this solution, in order to use variables with names such as “lamp” or, in this case, “Button”, you have to associate the variable with the array’s bits, as it’s shown in the picture below.

I hope this helps!

SingleVariables.zip (859.6 KB)
SingleVariablesAlternative.zip (1.0 MB)

Hi Sven,
I hope you are doing good.
I used your example file and it helped me a lot.
I was thinking to access Input/Holding Registers of the Factory IO but could not find a feasible solution yet, have you tried it? can you please share a file in which you can use 1 or 2 registers and i can get the idea and continue.

Thanks in advance.

Sorry for my late response. I didn’t get any notification about it :slight_smile:
I prepared a new working version for you. It’s able to read and write your integers.

test.zip (1.4 MB)