labviewmodbusplctwincatiec61131-3

Twincat3: Can't connect to LabVIEW's Modbus TCP/IP server via Tc2_ModbusSrv library (on virtual PLC)


I am trying to create a simple Hello World Modbus TCP/IP program using Twincat3 and virtual PLC (PLC running locally on my computer).

What I have tried:

This is my Beckhoff code:

PROGRAM MAIN
VAR
    ipAddr      : STRING(15) := '192.168.88.1'; //Tried also 192.168.88.126 and 192.168.88.1 'localhost'
    M0 AT %MB0  : ARRAY [0..3] OF WORD;
    nValue      : ARRAY [0..3] OF WORD;
    fbWriteRegs : FB_MBWriteRegs;
    bWriteRegs  : BOOL;
END_VAR 

IF NOT bWriteRegs THEN
    nValue[0]               := nValue[0]+1;
    nValue[1]               := nValue[1]+1;
    nValue[2]               := nValue[2]+1;
    nValue[3]               := nValue[3]+1;
    
    bWriteRegs              := TRUE;
    
    fbWriteRegs.sIPAddr     := ipAddr;
    fbWriteRegs.nTCPPort    := 502;
    fbWriteRegs.nUnitID     := 16#FF;           //Tried setting this to 0,1 and it also and didnt work ...
    fbWriteRegs.nQuantity   := 4;
    fbWriteRegs.nMBAddr     := 16#3000;         //Tried: 16#3000, 0, 1
    fbWriteRegs.cbLength    := SIZEOF(nValue);
    fbWriteRegs.pSrcAddr    := ADR(nValue);
    fbWriteRegs.tTimeout    := T#5S;
    fbWriteRegs             (bExecute:=TRUE);
ELSE
    
    IF NOT fbWriteRegs.bBUSY THEN
        bWriteRegs          :=FALSE;
        fbWriteRegs         (bExecute:=FALSE);
    END_IF
    
    fbWriteRegs             (bExecute:=FALSE);
END_IF 

What should I change in my code in order to make this example work?


Info about LabVIEW programs:

Picture of my LabVIEW programs:

enter image description here

Edit

By virtual PLC I mean this target PLC:

enter image description here


Solution

  • You didn't mention that you have installed TF6250, which is necessary. You need to install TF6250 on the PLC:

    https://www.beckhoff.com/en-en/products/automation/twincat/tfxxxx-twincat-3-functions/tf6xxx-tc3-connectivity/tf6250.html