PLC Shell commands via ST Code

2025-02-27
2025-03-04
  • fabiodasilveira

    fabiodasilveira - 2025-02-27

    Hello Everybody,

    I have created a project for an Eaton XC303 that sends lots of data to a router via UDP. It works fine. However, when there is more than one product connected to the router, then it is necessary to change the IP address of the Ethernet port 0, from e.g. 192.168.2.11 to 192.168.2.12 (second product). It is easily done via PLC Shell (setipaddr 0 192.168.2.12), but the people in production is struggling with the PLC Shell commands and I would like to create a Visualization page that will hide that. I used the instruction: eChangeIPResult:= SysSockSetIPAddress(strEthernetPort, strIPAddress); and it works, until the PLC resets. I already read some posts and it appears that I need to stop the Ethernet 0 port and Reconfigure it, but I am really struggling to find the right way to do it. Any help will be much appreciated.

     

    Last edit: fabiodasilveira 2025-02-28
  • bingo - 2025-03-02

    For a permanent change, you may need to update the PLC configuration file or set the IP at startup using retained variables.

     
  • fabiodasilveira

    fabiodasilveira - 2025-03-03

    Thank you, Bingo.

    I have added the instructions below to the CODESYSControl.cfg file, but they haven't changed anything.

    [SysSocket]
    Adapter.0.Name="ETH0"
    Adapter.0.EnableSetIpAndMask=1
    Adapter.1.Name="ETH1"
    Adapter.1.EnableSetIpAndMask=1

    Regarding your second suggestion, could you please expand a bit on that?

     
  • bingo - 2025-03-03

    Suppose you need to switch PLC IP dynamically between 192.168.1.10\192.168.2.10,
    First, set PLC IP to 192.16.1.10.

    Create a PGVL with a flag β€˜xUseSecondry’
    xUseSecondry : bool ;

    create a function that use socket to update IP:

    FUNCTION fSwitchIP : bool
    VAR_INPUT
    useMainIP:bool;
    END_VAR
    --------------------------------CODE------
    if useMainIP then
    SysSockSetIPAddress(β€˜0’, β€˜192.168.1.10’);
    PGVL.xUseSecondry := 0;
    else
    SysSockSetIPAddress(β€˜0’, β€˜192.168.2.10’);
    PGVL.xUseSecondry := 1;
    end_if

    Now, if during execution, you need to switch IP, call the function β€˜fSwitchIP()’,
    Set input to 1 for main IP, 0 for secondary IP.

    In order to set secondary IP at startup, you can do the following:

    in your PLC_PRG add a flag:
    xInit : bool := 1 ;

    and the code:
    if xInit then
    if PGVL.xUseSecondry then
    fSwitchIP(0);
    end_if
    xInit := 0 ;
    end_if
    *Alternative way is to use system events.

     
  • fabiodasilveira

    fabiodasilveira - 2025-03-04

    Thank you, Bingo. Your idea of adding the change to the start up process works.

    However, I am still intrigued with the fact that SysSockSetIPAddress(strEthernetPort:= '0', strIPAddress:= '192.168.2.12') is different from PLC Shell (setipaddr 0 192.168.2.12).:-(

     

Log in to post a comment.