Trouble with function block CfgGetDefaultGateway()

Hi Everyone,

I’ve got some trouble with this FB.

In substance i want to change the default gateway form user interface.

I don’t know why but i can’t reach the correct pdevice to point the correct value. After writing a new string i need to read it, but i can’t do it.

i attach some screenshots to explain better.

Any Suggestions?



What is the content of your Network.Data.Device variable?
It should be a STRING containing the full device path of your ETHx device.

For ETH1 of an APC3100 it would simply be “IF3” if I’m not mistaken.

→ Look up the path in your physical view! If you move the mouse over the desired interface, you should see the “PLC Address:” in the tooltip!

Best regards,

Since the ‘DefaultGateway’ is a global parameter you can set pDevice to 0.

2 Likes

I’m sorry i forgot to explain that. Yes Network.Data.Device is IF3 of my eth device.
I just know that i can see the plc address on my physical view but the default gateway is in PLC ($root), i just tried to change the address in root but it won’t work.

According to the help it should work also with device, but it seems optional…

What is the error code you are getting?

Edit: Sorry, didn’t see it in the screenshot…

Hi,

error code 29009 just means, that the default gateway isn’t configured at all.
Do you use static IP settings? If yes, is a default gateway set inside the project? Because if a DHCP client setting is used, the default gateway is transferred automatically by the DHCP server, but if using static IP addressing, the default gateway has to be set explicitely by the user in the project, or by software using CfgSetDefaultGateway().

Best regards!

1 Like

from his last answer I suppose he uses ‘$root’

image

but this is not a valid device name for this fb.

Either the device name of the real Ethernet device must be used or ‘0’. If you use the Ethernet device it is just an additional check if this device exists and if this in the right mode.

2 Likes

Hi to All,

I did some attempts, but it sill don’t work correctly.
Now I’m in this condition.


Screenshot 2024-09-18 121908
Screenshot 2024-09-18 121929
Screenshot 2024-09-18 121950

In screenshots you can see that i configured a default gateway, and now pDevice is null, why i can’t read it?

At the moment again error 29009 it means that the default gateway was not set.

Other suggestions?

Thank you all for support.

Hello Matteo,
I suppose the function block is mentioning 29009 due to the fact that your Gateway IP is wrong.

You use an IP adress of 192.168.128.50 with a subnet of 255.255.255.0
Your gateway IP on the other hand is 192.168.100.254 which does not fit to your network.
Basically the Gateway IP is the adress where all packets are sent which do not belong to the network IP range you define. The Gateway (most likely a router or VLAN switch) then forwards the telegram to a different network where it hopefully fits
In your case your network is from 192.168.128.1 to 192.168.128.254 → your gateway does not fit into this range and the IP stack of the PLC could not send any packet not belonging to its range to the gateway as it is also in a non valid range.

Please try (for testing) to set a Gateway IP of 192.168.128.254 and try again. I would expect that it then works

2 Likes

I now tested this and I can show you my results:

Here is the function block with a valid Gateway IP:

And here with a invalid IP:

I used AB as programming language but the result would be the same in ST
(192.168.10.xxx is my private IP range)

5 Likes

Thank you Oliver!

It works!

Screenshot 2024-09-18 133932

kind regards!

1 Like

Perfect :slight_smile: good to hear and thanks for the feedback !

1 Like

Hi Alexander,

Sorry to dig up an old thread, but I have been looking to get the default gateway assigned by DHCP.

I was trying to use CfgGetDefaultGateway however even once DHCP is enabled, it just returns what was last statically configured.

Do you know if there is any way to get the default gateway assigned by the DHCP server?

Thanks,
Jackson

Hi @Jacksong ,

That’s strange, from my experience CfgGetDefaultGateway also returns the right address by using DHCP client.
What PLC and Automation Runtime you’re using?

When calling CfgGetDefaultGateway in INIT, theoretically it could happen that the DHCP data isn’t already valid (depending on the servers response time), so I would recommend to wait some seconds after PLC startup and call the FB then (again).

Best regards!

Hi Alex,

Thanks for the quick response. This was on an X20CP0483 running AR P4.93. I just updated to AR R4.93 and it is showing the same behaviour.

I am calling the CfgGetDefaultGateway cyclically and as expected is is alternating between ERR_OK and ERR_FUB_BUSY.

In the CPU Configuration → Ethernet parameters → Default Gateway this is configured to 192.168.1.10.

Please see below screenshots from SDM and the program I have made:


Thanks!

Hi,

I’ve no idea why the default gateway returned (not only with the function block, but also by SDM) is not the configured one.

But honestly spoken, I don’t understand this setup at all?
For proper function of a default gateway, the gateway has to be part of one of the IP subnets used on the ethernet interfaces.
In the setting you describe, the default gateway uses a net 192.168.1/24 and therefore isn’t inside the IP subnet of IF2 (10.52.0/24), nor inside of IF3 (192.168.2/24). That means that the default gateway cannot be reached by the IF2 or IF3 net… or have you setted up some extra static routing inside the PLC?

Normally, when using DHCP client, the default gateway setting in AS is empty and the default gateway address is delivered by the DHCP server… I did a very quick test with some 4.93 version, and it worked like expected.
So unfortunately, I only can recommend to check the AS settings twice / again, or making a simple test with an empty project to check if the behavior changes.

Best regards!

Perhaps in the past the CfgSetDefaultGateway() is done with option cfgOPTION_NON_VOLATILE influencing the current behavior.

Using the function CfgClearNV() could perhaps solve the issue.

1 Like

Hi Alex and Corne,

Thanks for your responses. Definitely was on the right track. The project I was working on was a way to change the IP settings from a mappView UI.

From my testing, if a default gateway is defined statically (in the CPU configuration or using CfgSetDefaultGateway), CfgGetDefaultGateway returns this static default gateway even when DHCP is enabled.

Like Alex described though, if the default gateway is not set, CfgGetDefaultGateway returns the default gateway as assigned by the DHCP server.

To get my application/program to work, I am now setting the default gateway to be empty when DHCP is enabled (by calling CfgSetDefaultGateway with an IP of 0.0.0.0).

Thank you both for your help with this!