/// <summary> /// Select Networking Band to use /// </summary> /// <param name="Band">Band to use</param> /// <returns>True on succes</returns> public bool SelectNetworkBand(NetworkBands Band) { // Used for returning response body string responseBody; // Do a band change if (_device.ExecuteCommand("AT#BND=" + ((int)Band).ToString(), out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { return(false); } return(true); }
/// <summary> /// Get GPS Location and Fix Data /// </summary> /// <param name="Fix">Dimensions of Fix (0 = No fix, 2 = 2D fix, 3 = 3D fix)</param> /// <param name="NoSatelites">Number of tracked satelites (Valid when fix>0)</param> /// <param name="Latitude">Latitude in degrees (Valid when fix>0)</param> /// <param name="Longitude">Longitude in degrees (Valid when fix>0)</param> /// <param name="Speed">Speed in KM/H (Valid when fix>0)</param> /// <param name="GPSTime">UTC/GPS Time (Valid when fix>0)</param> public void ReadGPSData(out byte Fix, out byte NoSatelites, out Double Latitude, out Double Longitude, out Double Speed, out DateTime GPSTime) { // Used for returning response body string responseBody; // Request GPS information if (_device.ExecuteCommand("AT$GPSACP", out responseBody, 2500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } // Check response body if (responseBody.IndexOf("\r\n$GPSACP: ") != 0) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } // Split message on comma String[] gpsInformation = responseBody.Substring(11).Split(new char[] { ',' }); // Check for Fix Fix = (byte)NumberParser.StringToInt(gpsInformation[5]); if (Fix > 0) { // If Fix get Satelites, Lat and Lon NoSatelites = (byte)NumberParser.StringToInt(gpsInformation[10]); Latitude = _decode_dm(gpsInformation[1]); Longitude = _decode_dm(gpsInformation[2]); Speed = NumberParser.StringToDouble(gpsInformation[7]); // Create DateTime Object int year = NumberParser.StringToInt(gpsInformation[9].Substring(4, 2)) + 2000; int month = NumberParser.StringToInt(gpsInformation[9].Substring(2, 2)); int day = NumberParser.StringToInt(gpsInformation[9].Substring(0, 2)); int hour = NumberParser.StringToInt(gpsInformation[0].Substring(0, 2)); int minute = NumberParser.StringToInt(gpsInformation[0].Substring(2, 2)); int second = NumberParser.StringToInt(gpsInformation[0].Substring(4, 2)); GPSTime = new DateTime(year, month, day, hour, minute, second); } else { // If not return 0 NoSatelites = 0; Latitude = 0F; Longitude = 0F; Speed = 0F; GPSTime = new DateTime(); } }
/// <summary> /// Check if we have a valid GPRS Network registration /// </summary> /// <returns>True of registrated on GPRS Network</returns> public bool RegistratedOnGPRSNetwork() { // Used for returning response body string responseBody; // Do a registration check if (_device.ExecuteCommand("AT+CGREG?", out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } // Check response body if (responseBody.IndexOf("\r\n+CGREG: ") != 0) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } // Check registration state if ((responseBody[12] == '1') | (AllowRoaming & responseBody[12] == '5')) { return(true); } else { return(false); } }
public static void Main() { #region Thread to simulate working application // Simulate a thread that does some heavy work with random spikes and dips // so it could interfere with the processing of GM862 responses Thread HeavyWork = new Thread(new ThreadStart(delegate() { Random rand = new Random(); long testOperator = 0; while (true) { do { for (int x = 0; x < 1000; x++) { testOperator = rand.Next(1100); } } while (testOperator > 1000); Thread.Sleep(10); } })); HeavyWork.Priority = ThreadPriority.AboveNormal; HeavyWork.Start(); #endregion #region Reset procedure for GM862 Debug.Print("Resetting GM862"); // Reset GM862 Device OutputPort ResetPin = new OutputPort((Cpu.Pin) 44, false); System.Threading.Thread.Sleep(100); ResetPin.Write(true); Debug.Print("Turn On GM862"); // Wait a while for GM862 to reset System.Threading.Thread.Sleep(1000); // Turn on GM862 OutputPort OnOffPin = new OutputPort((Cpu.Pin) 45, true); System.Threading.Thread.Sleep(500); OnOffPin.Write(false); // Wait a while for GM862 to turn onn System.Threading.Thread.Sleep(1000); #endregion // Counter to count how hard we have been working long largeCounter = 0; // Create new GM862 Device GM862GPS GM862 = new GM862GPS(new AT_Interface("COM1", 115200)); // used for sending commands String ResponseBody = ""; #region GSM Setup // Initialize gsm function lock (GM862) { // Select network band if (!GM862.GSM.SelectNetworkBand(GSM.NetworkBands.GSM900_DCS1800)) { throw new Exception("Failed to select GSM Network Band"); } // First create function to activate phone GM862.GSM.OnPinRequest = new GSM.PinRequestHandler(delegate(String PINType) { if (PINType == "SIM PIN") { return("0000"); } if (PINType == "SIM PUK") { return("05969374, 0000"); } throw new Exception("Unkown PIN Code"); }); // Event handler for recieving calls GM862.GSM.OnRecievingCall = new GSM.RecievingCallHandler(delegate() { Debug.Print("You are beeing called!"); Thread.Sleep(500); }); // Allow roaming GM862.GSM.AllowRoaming = true; // Now try to initialize gsm GM862.GSM.Initialize(); } #endregion #region GPRS Setup // Initialize GPRS function lock (GM862) { // Allow roaming GM862.GPRS.AllowRoaming = true; // Now try to initialize GPRS GM862.GPRS.Initialize(); } #endregion #region Text Messaging Setup // Initialize text messaging function lock (GM862) { GM862.TextMessaging.OnRecievedTextMessage = new TextMessaging.RecievedTextMessageHandler(delegate(String Memory, int Location) { TextMessaging.TextMessage TextMessage; Debug.Print("Recieved Text Message!"); // Read text message from indicated storage if (GM862.TextMessaging.ReadTextMessage(Memory, Location, out TextMessage)) { Debug.Print("From: " + TextMessage.Orginator); Debug.Print("Message: " + TextMessage.Message); if (Memory.IndexOf("SM") != -1) { Debug.Print("Deleting message: " + (GM862.TextMessaging.DeleteMessage("SM", TextMessage.Location) ? "OK" : "ERROR")); } //Debug.Print("Sending it back"); //GM862.TextMessaging.SendTextMessage(TextMessage.Orginator, TextMessage.Message); } }); // Now try to initialize text messaging GM862.TextMessaging.Initialize(); } #endregion #region GPS Setup // Enable GPS Functions lock (GM862) { // Initialize GPS Functions GM862.GPS.Initialize(); } #endregion #region Extra functions // Enable status output to get some garbage in the output lock (GM862) { if (GM862.ExecuteCommand("AT+CMER=1, 0, 0, 2, 0", out ResponseBody, 1500) != AT_Interface.ResponseCodes.OK) { Debug.Print("ERROR +CMER: " + ResponseBody); System.Threading.Thread.Sleep(-1); } } // Also unsolicitated GPS output for even more garbage /* lock (GM862) * { * if (GM862.ExecuteCommand("AT$GPSNMUN=1, 0, 0, 0, 0, 1, 1", out ResponseBody, 1500) != AT_Interface.ResponseCodes.OK) * { * Debug.Print("ERROR $GPSNMUN: " + ResponseBody); * System.Threading.Thread.Sleep(-1); * } * } //*/ #endregion // Wait until we have initial GSM/GPRS ready Debug.Print("Waiting until GSM/GPRS ready"); while (true) { if (!GM862.GSM.RegistratedOnGSMNetwork()) { continue; } if (!GM862.GPRS.RegistratedOnGPRSNetwork()) { continue; } break; } Debug.Print("Ok"); // Read list of messages from SIM and delete them to make room Debug.Print("Read list of messages from SIM and delete them to make room"); TextMessaging.TextMessage[] Messages; if (GM862.TextMessaging.ListTextMessages("SM", TextMessaging.ListGroups.ALL, out Messages) > 0) { foreach (TextMessaging.TextMessage Message in Messages) { Debug.Print("Deleting message #" + Message.Location + ": " + (GM862.TextMessaging.DeleteMessage("SM", Message.Location) ? "OK" : "ERROR")); } } Debug.Print("Ok"); // Setup first GPRS Context Debug.Print("Setup first GPRS Context"); if (!GM862.GPRS.SetContextConfiguration(1, "live.vodafone.com", "vodafone", "vodafone", "0.0.0.0")) { throw new Exception("Failed to activate GPRS"); } Debug.Print("Ok"); // Activate first GPRS Context Debug.Print("Activate first GPRS Context"); if (!GM862.GPRS.ActivateContext(1)) { throw new Exception("Failed to activate GPRS"); } Debug.Print("Ok"); // Setup Socket Configuration Debug.Print("Setup Socket Configuration"); if (!GM862.GPRS.SetSocketConfig(1, 1, 0, 90, 600, 50)) { throw new Exception("Failed to setup socket 1/2"); } if (!GM862.GPRS.SetSocketExtendedConfig(1, 0, 0, 0)) { throw new Exception("Failed to setup socket 2/2"); } Debug.Print("Ok"); // Parameters used when retrieving GPS info byte gpsFix; byte gpsNoSat; double gpsLat; double gpsLon; double gpsSpeed; DateTime gpsDateTime; // Heavy testing ;-) while (true) { lock (GM862) { if (!GM862.GSM.RegistratedOnGSMNetwork()) { Debug.Print("GSM NOT READY"); } if (!GM862.GPRS.RegistratedOnGPRSNetwork()) { Debug.Print("GPRS NOT READY"); } GM862.GPS.ReadGPSData(out gpsFix, out gpsNoSat, out gpsLat, out gpsLon, out gpsSpeed, out gpsDateTime); Debug.Print((gpsFix > 0) ? "GPS Fix ;-)" : "NO GPS FIX"); } lock (GM862) { if ((largeCounter % 21) == 20) { byte[] response; Debug.Print("Fetching sample page from the Web"); if (GM862.Networking.WebRequest(1, "http://joomlacluster.edu-actief.nl/", String.Empty, out response, "GET", String.Empty, String.Empty)) { Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(response))); } else { Debug.Print("Failed"); } } } largeCounter++; Debug.Print(largeCounter.ToString()); lock (GM862) { if ((largeCounter % 5) == 0) { GM862.HandleUnsolicitatedResponses(); } } } }
/// <summary> /// Initialize Text Messaging Support /// </summary> public void Initialize() { // String used to store response body when executing functions string responseBody; // Add event handler for unsolicitated responses to check for text message events _device.OnUnsolicitatedResponse += new GM862GPS.UnsolicitatedResponseHandler(_textmessage_unsolicitated); // Select standard SMS instruction set if (_device.ExecuteCommand("AT#SMSMODE=0", out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.FAILED_INITIALIZE); } // Select Unsolicited SMS code to be buffered and in form +CMTI: <mem>, <id> if (_device.ExecuteCommand("AT+CNMI=2,1,0,0,0", out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.FAILED_INITIALIZE); } // Select Text Message Format if (_device.ExecuteCommand("AT+CMGF=1", out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.FAILED_INITIALIZE); } // Set Show Text Mode Parameters to show extended information if (_device.ExecuteCommand("AT+CSDH=1", out responseBody, 1500) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.FAILED_INITIALIZE); } // We have initialized the text message functionality succesfull _textmessage_initialized = true; }