Dumbfounded
After posting my reply I ran some code that was failing and guess what… It worked while debugger attached and when only deployed.
Maybe getting kicked out of here for awhile (browser crash and password problem) did some good.
I was not using a static IP and the test code ran as expected.
This was partial code of the original and I will keep adding code back and see if I can find what causes it to fail.
Code that worked
using System;
using GHI.Processor;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Presentation; //
using Microsoft.SPOT.Presentation.Media;
using MSPM = Microsoft.SPOT.Presentation.Media;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using GHI.Networking; // WiFiRS9110 - EthernetBuiltIn
using GHI.Pins;
using G400 = GHI.Pins.G400;
/*
Using firmware 4.3.8.0
NETMF 4.3
Windows 10 Pro
Visual Studio 2013 Pro
*/
/*
My G400D Dev Board 1.2 has a battery installed.
See GHI Support/Documents/Real Time Clock (RTC)
for additional usage and exception checks
*/
// Original code by Peter. See:
// https: //www.ghielectronics.com/community/codeshare/entry/962
namespace EthernetNetTimeG400D
{
public class Program
{
private const string NTP_ADDRESS = "us.pool.ntp.org"; // Your country?
// UTC -6 hours Central Standard. Use -(60 * 5) for Daylight savings
private const int NTP_TIME_OFFSET = -(60 * 6); // Your offset required
private static GHI.Networking.EthernetBuiltIn ethernet = null;
private static OutputPort led1 = null; // Blue
private static OutputPort led2 = null; // Green
private static OutputPort led3 = null; // Orange
private static OutputPort led4 = null; // Red
private static Font fontSmall = null;
private static Font fontLarge = null;
private static Bitmap bmp;
private static MSPM.Color backColor;
//Set this bool true to save internet time received to the RealTimeClock
private static bool saveTimeToHardwareClock = false;
// If pin PA26 is grounded we will save internet time received to the RealTimeClock
private static InputPort saveTime = new InputPort(G400D.Gpio.PA26, false, Port.ResistorMode.PullUp);
private static bool isavail = false;
public static void Main()
{
fontLarge = Resources.GetFont(Resources.FontResources.NinaB);
fontSmall = Resources.GetFont(Resources.FontResources.small);
bmp = new Bitmap(SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);
backColor = ColorUtility.ColorFromRGB(0X00, 0XCC, 0XFF); //Light blue
DrawBackgroundBmp(bmp);
DrawBackgroundLine(Resources.GetString(Resources.StringResources.Hello), bmp, backColor, Colors.Black, fontSmall, 5, 5);
led1 = new OutputPort(Led.Led1, false); //Blue
led2 = new OutputPort(Led.Led2, false); //Green
led3 = new OutputPort(Led.Led3, false); //Orange
led4 = new OutputPort(Led.Led4, false); //Red
try
{
long constructorTime = 0;
GHI.Networking.EthernetBuiltIn ethernet = null;
while (constructorTime == 0)
{
DateTime now = DateTime.Now;
ethernet = new GHI.Networking.EthernetBuiltIn();
constructorTime = ((DateTime.Now - now).Ticks / TimeSpan.TicksPerMillisecond);
Debug.Print("Ethernet constructor time (ms) = " + constructorTime);
if (constructorTime == 0)
{
ethernet.Dispose();
}
}
//
if (ethernet != null)
{
ethernet.Open();
ethernet.EnableDhcp();
ethernet.EnableDynamicDns();
Debug.Print("Obtaining IP Address...");
while (ethernet.IPAddress == "0.0.0.0")
{
Thread.Sleep(1000);
}
Debug.Print("IP Address is : " + ethernet.IPAddress);
DrawBackgroundLine("IP Address is : " + ethernet.IPAddress, bmp, backColor, Colors.Black, fontLarge, 5, 5);
}
//
// Get the time
NTPTime(NTP_ADDRESS, NTP_TIME_OFFSET);
Thread.Sleep(-1); // STOP HERE FOR TEST
}
catch (Exception e)
{
Debug.Print(e.Message);
}
//
//FIN
Thread.Sleep(-1);
}
//
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
isavail = true;
}
else
{
isavail = false;
}
//
Debug.Print("Network IsAvailable: " + isavail.ToString());
}
//
public static bool NTPTime(string TimeServer, int GmtOffset = 0)
{
Socket s = null;
try
{
EndPoint rep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var ntpData = new byte[48];
Array.Clear(ntpData, 0, 48);
ntpData[0] = 0x1B; // Set protocol version
s.SendTo(ntpData, rep); // Send Request
// Wait for 30 seconds. if nothing: timeout
if (s.Poll(30 * 1000 * 1000, SelectMode.SelectRead))
{
s.ReceiveFrom(ntpData, ref rep); // Receive Time
byte offsetTransmitTime = 40;
ulong intpart = 0;
ulong fractpart = 0;
for (int i = 0; i <= 3; i++) intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];
for (int i = 4; i <= 7; i++) fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];
ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
s.Close();
DateTime dateTime = new DateTime(1900, 1, 1) +
TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
// Needs a battery
if (saveTimeToHardwareClock)
{
RealTimeClock.SetDateTime(dateTime.AddMinutes(GmtOffset));
Utility.SetLocalTime(RealTimeClock.GetDateTime());
DrawBackgroundLine("Saving internet time to RealTimeClock: " + dateTime, bmp, backColor, Colors.Red, fontSmall, 5, 180);
Thread.Sleep(1000);
DateTime RTC = RealTimeClock.GetDateTime();
DrawBackgroundLine("Real-time Clock is now: " + RTC.ToString(), bmp, backColor, Colors.Black, fontSmall, 5, 195);
}
else
{
string theTime = "Time " + dateTime.AddMinutes(GmtOffset).ToString("hh:mm:ss tt"); //Use for 12 Hour without date
DrawBackgroundLine("Just looking...", bmp, backColor, Colors.Red, fontSmall, 5, 180);
DrawBackgroundLine("NOT saving internet time to RealTimeClock: " + dateTime.AddMinutes(GmtOffset), bmp, backColor, Colors.Red, fontSmall, 5, 196);
DrawBackgroundLine("12 Hour clock: " + theTime, bmp, backColor, Colors.Red, fontSmall, 5, 212);
DrawBackgroundLine("Ground Header pin PA26 to save internet time to RealTimeClock", bmp, backColor, Colors.Red, fontSmall, 5, 240);
DrawBackgroundLine("then reset this board", bmp, backColor, Colors.Red, fontSmall, 5, 255);
}
//
return true;
}
//
s.Close();
// Time out
Debug.Print("Time out!!! Time was not set");
}
catch (Exception ex)
{
Debug.Print(ex.Message);
try
{
s.Close();
}
catch
{
}
}
return false;
}
//
private static string GetMACAddress(byte[] PhysicalAddress)
{
return ByteToHex(PhysicalAddress[0]) + "-"
+ ByteToHex(PhysicalAddress[1]) + "-"
+ ByteToHex(PhysicalAddress[2]) + "-"
+ ByteToHex(PhysicalAddress[3]) + "-"
+ ByteToHex(PhysicalAddress[4]) + "-"
+ ByteToHex(PhysicalAddress[5]);
}
//
private static string ByteToHex(byte number)
{
string hex = "0123456789ABCDEF";
return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
}
//
private static void DrawBackgroundBmp(Bitmap b)
{
//Fill with background color
b.DrawRectangle(backColor, // outline color
0, // outline
0, // x
0, // y
SystemMetrics.ScreenWidth, // width
SystemMetrics.ScreenHeight, // height
0, // x corner radius
0, // y corner radius
backColor, // start gradient color
0, // start gradient x
0, // start gradient y
backColor, // end gradient color
SystemMetrics.ScreenWidth, // end gradient x
SystemMetrics.ScreenHeight, // end gradient y
0xFF); // opacity
b.Flush();
}
//
/// <summary>
/// Overwrites old text and flushes to bitmap
/// Background color fill is the bitmap width
/// </summary>
/// <param name="String to write"></param>
/// <param name="Bitmap used"></param>
/// <param name="Background color bitmap width"></param>
/// <param name="Text color"></param>
/// <param name="font"></param>
/// <param name="Text X"></param>
/// <param name="Text Y"></param>
private static void DrawBackgroundLine(string s, Bitmap b, MSPM.Color Bcolor, MSPM.Color Tcolor, Font font, int X, int Y)
{
//Fill text line with background color
//Erases old text if there
b.DrawRectangle(Bcolor, // outline color
0, 1, Y,
SystemMetrics.ScreenWidth,
font.Height,
0, // x corner radius
0, // y corner radius
Bcolor, // start gradient color
1, // start gradient x
Y, // start gradient y
Bcolor, // end gradient color
SystemMetrics.ScreenWidth, // end gradient x
font.Height, // end gradient y
0xFF); // opacity
b.Flush();
//Draw the text
bmp.DrawText(s, font, Tcolor, X, Y);
b.Flush();
}
//
}
//End class
/************************************************************************************/
public static class Led
{
public const Cpu.Pin Led1 = G400D.Gpio.PC18; //Blue
public const Cpu.Pin Led2 = G400D.Gpio.PD14; //Green
public const Cpu.Pin Led3 = G400D.Gpio.PD17; //Orange
public const Cpu.Pin Led4 = G400D.Gpio.PD18; //Red
}
//
static class Button
{
public const Cpu.Pin Up = G400D.Gpio.PA24; // LDR0
public const Cpu.Pin Down = G400D.Gpio.PA4; // LDR1
public const Cpu.Pin Left = G400D.Gpio.PD7;
public const Cpu.Pin Right = G400D.Gpio.PD9;
public const Cpu.Pin Select = G400D.Gpio.PD8;
}
//
} //End namespace