4

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

Jakub Szlaur
  • 1,852
  • 10
  • 39

1 Answers1

3

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

Jakob
  • 1,288
  • 7
  • 13
  • Wow interesting - I didn't see anything mentioning that I need to install this on the PLC - in my case when I am using virtual PLC - do I need to install this piece of SW onto my local PC? Thank you btw! Will check it out. – Jakub Szlaur Oct 13 '21 at 10:51
  • 1
    No you need to install it on the same machine that the XAR is running on, that is in your case the virtual machine. – Jakob Oct 13 '21 at 12:00
  • I edited my question and added screenshot of what I mean by `virtual PLC`. I managed to installed the TF6250 to my local PC - but resulted in error 1828 which means: `ADSERR_DEVICE_LICENSENOTFOUND` ... Is the `TF6250` paid for product, and there can't be a trial license? – Jakub Szlaur Oct 13 '21 at 12:52
  • 1
    You need to activate a trial license just as with any TwinCAT product. Just select TF6250 in the licenses tab, activate configuration and enter the CAPTCHA. – Jakob Oct 13 '21 at 13:18
  • I will try it as soon as I can :) – Jakub Szlaur Oct 14 '21 at 08:32