By Mike Dodaro. Translated by Marco Minerva from the original English version.
Allo stato attuale dello sviluppo, il modulo GHI Electronics Ethernet_J11D, in alcune configurazioni, potrebbe non funzionare correttamente utilizzando le librerie incluse nel .NET Gadgeteer SDK. I motivi del problema sono spiegati in questo post: http://www.tinyclr.com/forum/21/5010/. Non è ancora stato rilasciato un aggiornamento ufficiale per risolvere l’inconveniente.
Ad ogni modo, GHI ha curato molto la documentazione delle sue librerie, così è abbastanza semplice trovare una soluzione temporanea al problema adattando il codice contenuto nella pagina della guida relativa alla classe Ethernet. Al momento, la classe che ho creato supporta solo la mia applicazione che utilizza il sensore Seeed Pulse Oximeter. L’unico metodo della classe invia i dati da tale sensore verso un Web Service. In attesa della pubblicazione dell’esempio completo, questo scenario è facilmente ricreabile mettendo insieme il codice che si trova nei seguenti post:
- Seeed Pulse Oximeter .NET Gadgeteer Module
- REST Web Service to Record Data from a .NET Gadgeteer Sensor Device
Per usare la classe EthernetConnection, creiamo una sua istanza e richiamiamo il metodo SendPulseOximeterData nel gestore dell’evento ButtonPressed:
EthernetConnection connection = new EthernetConnection(); void button_ButtonPressed(Button sender, Button.ButtonState state) { Debug.Print("Sending data"); bool success = connection.SendPulseOximeterData("TestName3", "65", "98"); }La classe EthernetConnection completa è mostrata di seguito.
class EthernetConnection { static public ManualResetEvent NetworkAvailablityBlocking = null; NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces(); static public bool network_is_read = false; public EthernetConnection() { if (!GHINET.Ethernet.IsEnabled) { GHINET.Ethernet.Enable(); } if (!GHINET.Ethernet.IsCableConnected) { Debug.Print("Cable is not connected!"); NetworkAvailablityBlocking.Reset(); while (!NetworkAvailablityBlocking.WaitOne(5000, false)) { if (!GHINET.Ethernet.IsCableConnected) { Debug.Print("Cable is not connected!"); Debug.Print("Still waiting."); } else break; } } Debug.Print("Ethernet cable is connected!"); Debug.Print("Enable DHCP"); try { if (!netif[0].IsDhcpEnabled) netif[0].EnableDhcp(); // This function is blocking else { netif[0].RenewDhcpLease(); // This function is blocking } network_is_read = true; Debug.Print("Network settings:"); Debug.Print("IP Address: " + netif[0].IPAddress); Debug.Print("Subnet Mask: " + netif[0].SubnetMask); Debug.Print("Default Getway: " + netif[0].GatewayAddress); Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]); } catch { Debug.Print("DHCP Failed"); } } public bool SendPulseOximeterData(string name, string pulse, string oxg) { bool success = false; using (HttpWebRequest request = WebRequest.Create("http://integral-data.com/PulseOxgData/reading/" + name + "/" + pulse + "/" + oxg) as HttpWebRequest) { request.Method = "POST"; request.ContentLength = 0; HttpWebResponse response; try { response = request.GetResponse() as HttpWebResponse; if(response.StatusCode == HttpStatusCode.OK) success = true; response.Close(); } catch (Exception ex) { string exception = ex.Message + " Inner:" + ex.InnerException; Debug.Print(exception); Debug.Print(exception); } } return success; } }