/// <summary> /// Gets all unread messages from modem /// </summary> /// <param name="port"></param> /// <returns>IEnumerable of Model.SMS</returns> public static IEnumerable <Model.SMS> AllUnread(SerialPort port) { Initialize(port); string input = COMPort.ExecuteATCommand(port, "AT+CMGL=\"REC UNREAD\"", 5000, "Failed to read the messages."); return(ParseMessages(input)); }
/// <summary> /// Initializes the Receiving process /// </summary> /// <param name="port"></param> static void Initialize(SerialPort port) { // Check connection COMPort.ExecuteATCommand(port, "AT", 300, "No phone connected"); // Use message format "Text mode" COMPort.ExecuteATCommand(port, "AT+CMGF=1", 300, "Failed to set message format."); // Use character set "PCCP437" //ExecCommand(port,"AT+CSCS=\"PCCP437\"", 300, "Failed to set character set."); // Select SIM storage COMPort.ExecuteATCommand(port, "AT+CPMS=\"SM\"", 300, "Failed to select message storage."); // Read the messages }
/// <summary> /// Deletes all read messages in modem /// </summary> /// <param name="port"></param> /// <returns>bool</returns> public static bool AllRead(SerialPort port) { try { string recievedData = COMPort.ExecuteATCommand(port, "AT", 300, "No phone connected"); recievedData = COMPort.ExecuteATCommand(port, "AT+CMGF=1", 300, "Failed to set message format."); String command = "AT+CMGD=1,3"; recievedData = COMPort.ExecuteATCommand(port, command, 300, "Failed to delete message"); return(recievedData.EndsWith("\r\nOK\r\n")); } catch (ApplicationException ex) { // Garbled response return(false); } catch (Exception ex) { return(false); } }
/// <summary> /// Sends the SMS to a specific recipent /// </summary> /// <param name="port"></param> /// <param name="mobile"></param> /// <param name="message"></param> /// <returns>bool - Yes/No(Success)</returns> public static bool Send(SerialPort port, string mobile, string message) { try { string recievedData = COMPort.ExecuteATCommand(port, "AT", 300, "No phone connected"); recievedData = COMPort.ExecuteATCommand(port, "AT+CMGF=1", 300, "Failed to set message format."); String command = "AT+CMGS=\"" + mobile + "\""; recievedData = COMPort.ExecuteATCommand(port, command, 300, "Failed to accept phoneNo"); command = message + char.ConvertFromUtf32(26) + "\r"; recievedData = COMPort.ExecuteATCommand(port, command, 3000, "Failed to send message"); return(recievedData.EndsWith("\r\nOK\r\n")); } catch (ApplicationException ex) { // Garbled response // Not enough balance or SIM is deactivated return(false); } catch (Exception ex) { return(false); } }