Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How to check if ethernet is connected?


  • Please log in to reply
10 replies to this topic

#1 Anshul

Anshul

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 19 February 2012 - 08:44 PM

What's the proper method to check if the ethernet cable is connected, or if the Netduino+ has an IP Address, or both? Thanks

#2 Nathan Baker

Nathan Baker

    Member

  • Members
  • PipPip
  • 22 posts
  • LocationWisconsin

Posted 23 February 2012 - 02:54 AM

Hrm, this is a good question. I have to do this at some point to make my client more stable, just been lazy. :)
Posted Image

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 23 February 2012 - 03:33 AM

Hi Anshul, If you're using DHCP, you can check your current IPAddress through the NetworkInterface object (in Microsoft.SPOT.Net). Chris

#4 clem0338

clem0338

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 October 2012 - 02:00 PM

Hi, I know that i'm digging an old thread but I have the similar question and I must manually set the IP address (so no DHCP). Is there any way to do it ? Thanks. Cédric

#5 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 29 October 2012 - 03:16 PM

Except from NetworkAvailbilityChanged event, which cannot be used to detect the initial state, I suggest making somekind of request, wrapped in a try-catch ... if it fails, no network.

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/


#6 clem0338

clem0338

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 October 2012 - 05:10 PM

Except from NetworkAvailbilityChanged event, which cannot be used to detect the initial state, I suggest making somekind of request, wrapped in a try-catch ... if it fails, no network.



Thanks,

but it is exactly what i don't want. Let's explain myself:
The network can be down or unplugged.
When I'm trying to do the Socket.Connect, my ND+ is totally stuck (and all my threads :( ...)
It seem's to be a well known bug (corrected in MF 4.3 beta)
http://netmf.codeple...m/workitem/1396 for more info

So, i'm looking for a way to "check" the network before Socket.Connect even after boot up ...

Thanks anyway, regards

Cédric


Edit:
More info
http://netmf.codeple...m/workitem/1181
http://www.dotnetsol...word-of-warning

Didn't work with 4.1 neither 4.2 (Flashed for this bug)

I still have an IP address in memory even unplugged (DHCP trick)

#7 andybareweb

andybareweb

    Member

  • Members
  • PipPip
  • 12 posts

Posted 29 October 2012 - 05:14 PM

Hi,

I know that i'm digging an old thread but I have the similar question and I must manually set the IP address (so no DHCP).

Is there any way to do it ?

Thanks.

Cédric


Hi Cédric:

var interfaces = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                foreach (var networkInterface in interfaces)
                {
                    Debug.Print(networkInterface.IsDhcpEnabled ? "Dhcp Enabled" : "Static Ip");
                    Debug.Print(networkInterface.IPAddress);
                    if (networkInterface.IsDhcpEnabled)
                    {
                        networkInterface.EnableStaticIP("192.168.0.9", "255.255.255.0", "192.168.0.1");
                    }
                }

This prints out whether you're already Dhcp Enabled or Static, and if Dhcp it sets to static.

You'd probably want to be a little cleverer around releasing leases if you want to be friendly to your dhcp server, but otherwise the above works.

Regards
Andy

#8 caEstrada

caEstrada

    Advanced Member

  • Members
  • PipPipPip
  • 84 posts

Posted 29 October 2012 - 05:59 PM

Use any network operation inside a {Try/Catch} and then, check the Exception Value and Message, works perfect.

#9 Nobby

Nobby

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 30 October 2012 - 12:45 AM

One method I use is to create a socket which is a listener using IPAddress.Any for the binding. The listener will be created if the network is up or down but you can call Socket.Poll(). It will throw a SocketException if the network is down with an error code 10050. The only time this approach fails is if you power the ND+ up and it initially isn't connected to the switch/router. There's some bug which is apparently being fixed in 4.3 where network connectivity to the ND+ is impossible under this start-up condition but the ND+ internally believes connectivity is fine after you plug the network cable in.

#10 clem0338

clem0338

    New Member

  • Members
  • Pip
  • 3 posts

Posted 08 November 2012 - 10:10 AM

Many thanks for your answers and please, excuse me for the while I took. To andybarweb: I may be static IP or DHCP (from configuration file on SD). I don't want to force a static IP To caEstrada: Already in a try/catch. The problem is Socket.Connect never returns even with an exception To Nobby: This can be an half way solution. This will be OK for the ethernet activity test but, if my server is down, the problem's remains. Thanks to all. Cédric

#11 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 09 November 2012 - 01:57 PM

Did you set a SendTimeout on your socket before trying to connect?

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.