Search code examples
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).

  • I have set up a LabVIEW Modbus TCP/IP master + slave program (both of which are working see screenshots below).
  • I can send data between LabVIEW's master and slave programs. And also read the data from Python script (so I know for sure they are all working).
  • Now I have tried to create a Twincat3 program to set some registers using Beckhoff example.

What I have tried:

  • I have tried changing the ipAddr parameter to: 'localhost', '192.168.88.1', '192.168.88.126'
  • Also I have tried the nUnitID set to: 0, 1, 16#FF (16#FF as specified by Beckhoff docs)

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:

  • I am using the Modbus TCP Master / Slave examples
  • The IP address is set to 'localhost' and the port to 502 (which should match the IP address in my Beckhoff code)
  • I am 100% sure it works because I have also tried connecting to this program via Python script.

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