/// <summary> /// Send Data to Socket while in IDLE state /// </summary> /// <param name="SocketNo">Socket Number (1-6)</param> /// <param name="DataToSend">Data to send, max 1024 bytes</param> /// <returns>True on succes</returns> public bool SocketSendData(int SocketNo, byte[] DataToSend) { // Check if GPRS functions has been initialized if (!_gprs_initialized) { throw new GM862Exception(GM862Exception.WRONGARGUMENT); } // Check data to send if (DataToSend == null) { throw new GM862Exception(GM862Exception.WRONGARGUMENT); } if (DataToSend.Length > 1024) { throw new GM862Exception(GM862Exception.WRONGARGUMENT); } // Used when executing commands string responseBody; // Say we want to send data if (_device.ExecuteCommand("AT#SSEND=" + SocketNo.ToString(), out responseBody, 15000) != AT_Interface.ResponseCodes.SEND_SMS_DATA) { return(false); } // Wait a little before sending the message System.Threading.Thread.Sleep(20); // Send Data _device.SendRawData(DataToSend, 0, DataToSend.Length); // End data _device.SendRawData(new byte[] { 0x1A }, 0, 1); // Wait for response if (_device.WaitForResponse(out responseBody, 15000) != AT_Interface.ResponseCodes.OK) { return(false); } // Succes return(true); }
/// <summary> /// Send Text Message /// </summary> /// <param name="Destination">Destination for SMS</param> /// <param name="Message">Message to Send</param> public void SendTextMessage(String Destination, String Message) { // Check if Text Messaging was initialized correctly if (!_textmessage_initialized) { throw new GM862Exception(GM862Exception.WRONGARGUMENT); } // String used to store response body when executing functions string responseBody; // Add format identification to destination if (Destination.IndexOf('+') != -1) { Destination = "\"" + Destination + "\",157"; // Internation format } else { Destination = "\"" + Destination + "\",129"; // National format } // Say we want to send message if (_device.ExecuteCommand("AT+CMGS=" + Destination, out responseBody, 15000) != AT_Interface.ResponseCodes.SEND_SMS_DATA) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } // Wait a little before sending the message System.Threading.Thread.Sleep(20); // Send Message, End with ^Z byte[] OutBuffer = System.Text.Encoding.UTF8.GetBytes(Message + "\x1A"); _device.SendRawData(OutBuffer, 0, OutBuffer.Length); // Wait for response if (_device.WaitForResponse(out responseBody, 15000) != AT_Interface.ResponseCodes.OK) { throw new GM862Exception(GM862Exception.BAD_RESPONSEBODY); } }
public bool WebRequest(int SocketID, String URL, String Referer, out byte[] Contents, String RequestMethod, String POSTContentType, String POSTBody) { // Activate first GPRS Context _device.GPRS.ActivateContext(1); // Check for HTTP and remove it from the URL if (URL.IndexOf("http://") != 0) { Contents = null; return(false); } URL = URL.Substring(7); // Get Server String server = URL.Substring(0, URL.IndexOf("/")); // Get Path String path = URL.Substring(URL.IndexOf("/")); // Get Port Int32 port = 80; if (server.IndexOf(':') != -1) { String[] sServer = server.Split(new char[] { '|' }); server = sServer[0]; port = NumberParser.StringToInt(sServer[1]); } // Create request header String requestHeader = RequestMethod + " " + path + " HTTP/1.0\r\n"; // Request URI requestHeader += "Host: " + server + "\r\n"; // HTTP1.0 Host requestHeader += "Connection: Close\r\n"; // HTTP1.0 Close connection after request requestHeader += "Pragma: no-cache\r\n"; // HTTP1.0 No caching support requestHeader += "Cache-Control: no-cache\r\n"; // HTTP1.0 No caching support requestHeader += "User-Agent: GM862 (.NET Micro Framework 3.0)\r\n"; // HTTP1.0 User Agent if (Referer != String.Empty) { requestHeader += "Referer: " + Referer + "\r\n"; // HTTP1.0 Referer } if ((RequestMethod == "POST") & (POSTContentType != String.Empty)) { requestHeader += "Content-Type: " + POSTContentType + "\r\n"; } if (POSTBody != String.Empty) { requestHeader += "Content-Length: " + POSTBody.Length + "\r\n"; requestHeader += "\r\n"; requestHeader += POSTBody; } else { requestHeader += "\r\n"; } // Make byte array from request byte[] requestHeaderRAW = System.Text.Encoding.UTF8.GetBytes(requestHeader); // Make connection if (!_device.GPRS.SocketDail(SocketID, GPRS.SocketProtocols.TCP, server, port, false, true)) { Contents = null; return(false); } // Open online data mode if (!_device.GPRS.SocketRestore(1)) { Contents = null; return(false); } // Variables used for parsing response byte[] responseBuffer = new byte[0xffff]; int responseLength = 0; byte[] END_OF_HEADER = new byte[] { 13, 10, 13, 10 }; String responseHeader = String.Empty; byte[] responseHeaderRAW; DateTime timeoutAt = DateTime.Now.AddSeconds(120); int bytesRead; int expectedSize = -1; // This part of code in a try block. // To prevent an Thread.Abort messing up the current device state try { // Send request _device.SendRawData(requestHeaderRAW, 0, requestHeaderRAW.Length); // Read data from Socket until timeout while ((DateTime.Now < timeoutAt)) { // Read as many bytes as posible from GM862 bytesRead = _device.ReadRawData(responseBuffer, responseLength, responseBuffer.Length - responseLength); // If we have data reset timeout if (bytesRead > 0) { // Reset Timeout Timer timeoutAt = DateTime.Now.AddMilliseconds(10000); // Increase read position responseLength += bytesRead; } else { System.Threading.Thread.Sleep(10); continue; } // If our response buffer gets somewhat small increase it if ((responseBuffer.Length - responseLength) < 32) { byte[] ResponseTemp = new byte[responseBuffer.Length + 0xffff]; Array.Copy(responseBuffer, ResponseTemp, responseBuffer.Length); responseBuffer = ResponseTemp; } // Search for header End if (responseHeader == String.Empty) { int HeaderEnd = _find_array(END_OF_HEADER, responseBuffer); if (HeaderEnd != -1) { // Copy data from header responseHeaderRAW = new byte[HeaderEnd]; Array.Copy(responseBuffer, responseHeaderRAW, responseHeaderRAW.Length); // Try to parse header try { responseHeader = new String(System.Text.Encoding.UTF8.GetChars(responseHeaderRAW)) + "\r\n"; } catch { continue; } // Create lower case variant String ResponseHeaderLower = responseHeader.ToLower(); // Find an expected length if (ResponseHeaderLower.IndexOf("content-length:") != -1) { // Get Content Length Int32 SizeStart = ResponseHeaderLower.IndexOf("content-length: ") + 10; Int32 SizeEnd = responseHeader.IndexOf("\r\n", SizeStart); expectedSize = NumberParser.StringToInt(responseHeader.Substring(SizeStart, SizeEnd - SizeStart)); } // Check for a location header if (ResponseHeaderLower.IndexOf("location: ") != -1) { // Get Location Int32 LocationStart = ResponseHeaderLower.IndexOf("location: ") + 10; Int32 LocationEnd = responseHeader.IndexOf("\r\n", LocationStart); String newLocation = responseHeader.Substring(LocationStart, LocationEnd - LocationStart); // Close online connection if (_device.EscapeSequence(1500, 1500, 5000) != AT_Interface.ResponseCodes.OK) { Contents = null; return(false); } // Close socket if (!_device.GPRS.SocketShutdown(SocketID)) { Contents = null; return(false); } // Pass on WebRequest return(WebRequest(SocketID, newLocation, URL, out Contents, RequestMethod, POSTContentType, POSTBody)); } // Strip header from response responseLength -= HeaderEnd + 4; byte[] ResponseTemp = new byte[responseBuffer.Length - HeaderEnd - 4]; Array.Copy(responseBuffer, HeaderEnd + 4, ResponseTemp, 0, ResponseTemp.Length); responseBuffer = ResponseTemp; } } // Close connection when we've reached the expected size if ((expectedSize != -1) & (responseLength >= expectedSize)) { break; } } } catch (System.Threading.ThreadAbortException) { // Close online connection if (_device.EscapeSequence(1500, 1500, 5000) != AT_Interface.ResponseCodes.OK) { Contents = null; return(false); } // Close socket if (!_device.GPRS.SocketShutdown(SocketID)) { Contents = null; return(false); } Contents = null; return(false); } // Close online connection if (_device.EscapeSequence(1500, 1500, 5000) != AT_Interface.ResponseCodes.OK) { Contents = null; return(false); } // Close socket if (!_device.GPRS.SocketShutdown(SocketID)) { Contents = null; return(false); } // Check header if (responseHeader == String.Empty) { Contents = null; return(false); } // Check if we have all data if ((expectedSize != -1) & (responseLength < expectedSize)) { Contents = null; return(false); } // If we have an expected size strip response if ((expectedSize != -1) & (responseLength >= expectedSize)) { responseLength = expectedSize; } // Strip unused bytes from input buffer Contents = new byte[responseLength]; Array.Copy(responseBuffer, Contents, responseLength); // Succes return(true); }