OK, Gus …
Here is the “main” part of the code … it’s really simple and, in fact, it doesn’t even call any of the code in the other file yet.
using System;
using System.Threading;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.SPOT;
using NetInfo = Microsoft.SPOT.Net.NetworkInformation;
using METAR;
namespace FEZ_Cobra_Console_Application1
{
public class Program
{
public static void Main()
{
// Set up networking ...
NetInfo.NetworkInterface NI = NetInfo.NetworkInterface.GetAllNetworkInterfaces()[0];
NI.EnableDhcp();
Debug.Print("\n\rDCHP - IP Address = " + NI.IPAddress + ", Net Mask = " + NI.SubnetMask + ", Gateway = " + NI.GatewayAddress);
Debug.Print("DNS Server = " + NI.DnsAddresses[0].ToString() + ", MAC Address = " + NI.PhysicalAddress[0].ToString() + "-" + NI.PhysicalAddress[1].ToString() + "-" + NI.PhysicalAddress[2].ToString() + "-"
+ NI.PhysicalAddress[3].ToString() + "-" + NI.PhysicalAddress[4].ToString() + "-" + NI.PhysicalAddress[5].ToString() + "\n\r");
}
}
}
The other part of the code is shown below -
using System;
using System.Threading;
using System.Net;
using System.IO;
//using System.Text;
using System.Xml;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
//using NetInfo = Microsoft.SPOT.Net.NetworkInformation;
namespace METAR
{
public class getMetar
{
// List of tags to be parsed from the METAR
public string[] tags = new string[] {
"location",
"station_id",
"observation_time",
"observation_time_rfc822",
"weather",
"temp_f",
"temp_c",
"relative_humidity",
"wind_string",
"wind_dir",
"wind_degrees",
"wind_mph",
"wind_gust_mph",
"windchill_f",
"visibility_mi",
"pressure_in",
"dewpoint_f",
"wind_kt",
"pressure_mb",
"icon_url_base",
"icon_url_name",
"latitude",
"longitude",
};
//
//
public int maxRetries = 5;
public int retryWait = 100;
public int requestTimeout = 15000;
//
//
public getMetar()
{
}
//
//
private string[,] getWeather(string icao)
{
//
// Used to hold tags and their values obtained from resStream.
string[,] foundTag = new string[tags.Length, 2]; // Need as many rows as there are strings in the "tags" array.
// Missing tags will be null.
//
// Create the request, response, and stream needed to fetch the METAR.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.weather.gov/xml/current_obs/" + icao + ".xml");
request.KeepAlive = true;
request.ReadWriteTimeout = requestTimeout;
HttpWebResponse response;
Stream resStream;
try
{
response = (HttpWebResponse)request.GetResponse();
resStream = response.GetResponseStream();
}
catch
{
return foundTag; // In case of exception, return null string array.
}
//
// Set up the xml reader.
XmlReaderSettings ss = new XmlReaderSettings();
ss.IgnoreWhitespace = true;
XmlReader xmlr = XmlReader.Create(resStream, ss);
int k = 0;
//
// Loop through the xml-formatted METAR.
while (!xmlr.EOF)
{
try
{
xmlr.Read();
}
catch (Exception e)
{
Debug.Print("xml.Read() Exception: " + e.ToString());
break;
}
if (xmlr.NodeType == XmlNodeType.Element) // We found a potential tag ...
{
foreach (string tag in tags) // so loop through the strings in the "tags" array ...
{ // and compare the potential tag to the tags we are looking for.
if (xmlr.Name == tag) // If we find a match ...
{
foundTag[k, 0] = xmlr.Name; // save the tag, ...
xmlr.Read(); // do another read to fetch the value associated with the tag, ...
foundTag[k, 1] = xmlr.Value; // and save the value
k++;
} // End if
} // End foreach
} // End if
} // End while
response.Close();
return foundTag;
} // End getWeather
//
//
public string[,] getObs(string icao)
{
int tries = 0;
string[,] obs = new string[tags.Length, 2];
//
while (tries < maxRetries)
{
obs = getWeather(icao);
if (obs[0, 0] != null)
return obs;
tries++;
Thread.Sleep(retryWait);
} // End while
return obs;
} // End getObs
} // End class getMetar
} // End namespace METAR
When I can get the whole thing to build, I’ll add a call to “getObs” to main. I tested the code from the METAR namespace in .NET on a PC, and it worked just fine. Thanks for taking a look at this …