How can I tell if the Ethernet cable is connected?
T
  | ||||||||||||||
Can I tell whether the Ethernet cable is connected?
Started by Tal Tikotzki, Jan 13 2013 03:20 PM
6 replies to this topic
#1Posted 13 January 2013 - 03:20 PM How can I tell if the Ethernet cable is connected?
T
#2Posted 13 January 2013 - 03:26 PM
Hi Tal,
Assuming that you're using Netduino Plus 2... The NetworkInterface class will give you the current IP address. If you're using DHCP, that will effectively let you know if you have an acquired address. There's also the NetworkAvailabilityChanged event. Of course, the green 'ACT' LED lights up solid when an Ethernet cable is connected. But I think you were looking for the code answer Chris #3Posted 13 January 2013 - 06:30 PM Hi Chris, Thanks! I'm using a static address and not the DHCP. I'll give the event a chance, however, Is there a way to poll on the network availability?
T #4Posted 13 January 2013 - 09:23 PM
Hi Tal,
Hi Chris, Thanks! I'm using a static address and not the DHCP. I'll give the event a chance, however, Is there a way to poll on the network availability?You could try pinging your gateway IP at boot to see if you're already connected. This could be a useful feature for a future release of NETMF. Chris #5Posted 19 January 2013 - 09:59 AM Hi Chris, How can you perform the ping you suggested?
Tal #6Posted 19 January 2013 - 12:00 PM
Hi Tal,
How can you perform the ping you suggested?I haven't tested this one, but here's a ping sample from two years ago. There was a bug in NETMF which was blocking pings...so the user wrote a ping repro sample. http://netmf.codeple...m/workitem/1126 Chris #7Posted 19 January 2013 - 01:34 PM Thanks Chris,
The networkAvailabilityChange event is fired only if there is a change after the network established. So when starting (power up, reset etc.) I did not receive any notification, neither when the network was connected nor was it disconnected when Netduino powered-up..
For that reason I looked for a method to safely poll on the network status when the Netduino started.
I tried to open a listening socket on a port that I wasn't planning to use: using (var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { var bindingAddress = new IPEndPoint(IPAddress.Any, 9001); listenSocket.Bind(bindingAddress); listenSocket.Listen(1); netOkay = listenSocket.Poll(500, SelectMode.SelectError); } catch (Exception) { netOkay = false; } listenSocket.Close(); }
This is not the nicest code, but since it is being called only once, after power-up, it seems good enough.
Tal Edited by Tal Tikotzki, 20 January 2013 - 09:37 AM. 0 user(s) are reading this topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||