Search code examples
twincatmodbus-tcpstructured-texttwincat-ads

Twincat Modbus TCP communication giving ADS error 6


I have a program in structured text using twincat 3 that should read from and write to a modbus tcp server. However whenever i run the read or write functions i get the error 6. From what I have researched it is a ADS error Target Port not found but i could not find a way to fix it. Whenever i login I also get a warning message saying: Application 'Port_851' does not exist on device 'device name'. It gives an option to download and continue which i choose. I am currently not connected to a plc and just running the program on my laptop.

VAR
    // Modbus TCP Read Coils
    MBReadCoils : FB_MBReadCoils;
    nReadValue : BYTE;
END_VAR

MBReadCoils(
            sIPAddr:= 'localhost', 
            nTCPPort:= 502, 
            nUnitID:= 16#FF, 
            nQuantity:= 1, 
            nMBAddr:= 1, 
            cbLength:= SIZEOF(nReadValue), 
            pDestAddr:= ADR(nReadValue), 
            bExecute:= TRUE, 
            tTimeout:= , 
            bBusy=> , 
            bError=> , 
            nErrId=> , 
            cbRead=> );
            
MBReadCoils(bExecute := FALSE); 

I have double checked the addresses and they are all correct. I have tried looking into the ADS server but cannot find anything concrete on anything pertaining to my error.


Solution

  • A couple things:

    1. Did you install the TF6250 TC3 Modbus TCP package? This has to be installed separately from TwinCAT. https://www.beckhoff.com/en-us/products/automation/twincat/tfxxxx-twincat-3-functions/tf6xxx-connectivity/tf6250.html
    2. Disable Windows Firewall completely if you haven't already.
    3. Your code is cycling the bExecute bit on and off constantly, which is problematic because a Modbus read or write cycle will take considerably longer than one PLC scan. The recommended method is to trigger bExecute, then wait for the bBusy bit to deactivate before triggering bExecute again. See Beckhoff's example code: https://infosys.beckhoff.com/english.php?content=../content/1033/tf6250_tc3_modbus_tcp/192789899.html&id=