Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Dynamic Setting IP Address from IEC code

pmasip
2023-01-12
2024-02-19
  • pmasip - 2023-01-12

    Hi,

    I'm working with a Raspberry CM4 for a project. This Raspberry is going to act as a Modbus TCP slave and I want to edit the Ethernet adapter comm settings while the code is running, and also make these changes effective in the Raspberry network settings.

    I would like to know if it is possible to dynamically change the IP, subnetmask and gateway of the Ethernet adapter in a similar way as it is done in this faq thread for a Modbus TCP Slave:

    https://faq.codesys.com/display/CDSFAQ/Modbus+TCP+Slave%3A+Dynamic+Setting+of+the+IP+Address

    I have read some other threads in this forum about this topic and I have already tried to add to the config file CODESYSControl_User.cfg this:

    [SysSocket]
    Adapter.0.Name="eth0"
    Adapter.0.EnableSetIpAndMask=1

    And I'm trying to edit the Ethernet node settings with the code in the second picture, using the method UpdateConfiguredIpSettings but it does nothing.

    What else can I try?

    Thank you in advance,
    Pablo.

     
  • TimvH

    TimvH - 2023-01-13

    the first steps are done correctly:
    - Set the SysSocket properties in the .cfg file
    - Enable the option in the Ethernet adapter to adjust the IP address

    but the code should be handled differently to change the IP adres of the PLC.
    1)
    add the following instance to your variable declaration:
    Reconfigure : DED.Reconfigure;
    2)
    In your application you should now write some code to:
    - first disable the ethernet adapter
    - then change the IP adres
    - then enable the ethernet adapter again

    So for example
    Set the enable status of the ethernet adapter to false:

         Ethernet.Enable := FALSE;
    

    Then call the Reconfigure FB, when done, change IP:
    - off course you have to create some additional variable which contain the new settings for your ethernet adapter:

            Reconfigure(xExecute := TRUE, itfNode := Ethernet);
            IF Reconfigure.xDone THEN
                ErrorCode := Ethernet.UpdateConfiguredIPSettings(IPAddress := abyNewIP, SubnetMask := abyNewSubnet, Gateway := abyNewGateway);
            END_IF
            IF Reconfigure.xDone OR Reconfigure.xError THEN
                Reconfigure(xExecute := FALSE);
            END_IF
    

    Then enable the ethernet adapter again:

            Ethernet.Enable := TRUE;
            Reconfigure(xExecute := TRUE, itfNode := Ethernet);
            IF Reconfigure.xDone OR Reconfigure.xError THEN
                Reconfigure(xExecute := FALSE);
            END_IF
    
     
  • pmasip - 2023-01-16

    Thank you very much, it works perfectly fine. I was able to change the IP and I could verify it via Linux Terminal in my Raspberry.

    One last comment, I had to disable all the underlying devices of the Ethernet node for the code to work (in my case there were a Modbus TCP Master, Modbus TCP Slave and the Pi declared as TCP Slave Device. I'm leaving the final version attached in several pics.

     
  • richard-wang - 2023-06-21

    You are awesome!!!! Thx so much for sharing, this is exactly I am looking for.

     
  • ralfk - 2024-02-19

    Hello together. I also want to try it but when enter Ethernet.UpdateConfiguredIPSettings I don't see this Option. what do I wrong. I hope somebody can help me.

     
  • ralfk - 2024-02-19

    Hello together. I also want to try it but when enter Ethernet.UpdateConfiguredIPSettings I don't see this Option. what do I wrong. I hope somebody can help me.

     

Log in to post a comment.