/// <summary> /// AT+CMGR. Reads a single SMS message from the current read/delete storage using the PDU mode. /// </summary> /// <param name="index">The index of the message to read.</param> /// <returns>A <see cref="T:GsmComm.GsmCommunication.ShortMessage" /> object containing the message at the index specified.</returns> /// <remarks>Always switches to PDU mode at the beginning.</remarks> public ShortMessageFromPhone ReadMessage(int index) { ShortMessageFromPhone shortMessageFromPhone; lock (this) { this.VerifyValidConnection(); this.ActivatePduMode(); this.LogIt(LogLevel.Info, string.Concat("Reading message from index ", index.ToString(), "...")); string str = this.ExecAndReceiveMultiple(string.Concat("AT+CMGR=", index.ToString())); Regex regex = new Regex("\\+CMGR: (\\d+),(?:\"(\\w*)\")?,(\\d+)\\r\\n(\\w+)"); Match match = regex.Match(str); if (!match.Success) { this.HandleCommError(str); throw new CommException("Unexpected response.", str); } else { int num = int.Parse(match.Groups[1].Value); string value = match.Groups[2].Value; int num1 = int.Parse(match.Groups[3].Value); string value1 = match.Groups[4].Value; string[] strArrays = new string[6]; strArrays[0] = "stat="; strArrays[1] = num.ToString(); strArrays[2] = ", alpha=\""; strArrays[3] = value; strArrays[4] = "\", length="; strArrays[5] = num1.ToString(); this.LogIt(LogLevel.Info, string.Concat(strArrays)); ShortMessageFromPhone shortMessageFromPhone1 = new ShortMessageFromPhone(index, num, value, num1, value1); shortMessageFromPhone = shortMessageFromPhone1; } } return shortMessageFromPhone; }
/// <summary> /// Decodes a short message read from the phone. /// </summary> /// <param name="message">The message to decode.</param> /// <returns>The decoded short message.</returns> /// <remarks> /// Use this function to decode messages that were read with the <see cref="M:GsmComm.GsmCommunication.GsmCommMain.ReadRawMessages(GsmComm.GsmCommunication.PhoneMessageStatus,System.String)" /> function. /// <seealso cref="M:GsmComm.GsmCommunication.GsmCommMain.ReadRawMessages(GsmComm.GsmCommunication.PhoneMessageStatus,System.String)" /> /// </remarks> /// <exception cref="T:GsmComm.GsmCommunication.CommException">There is no decoder available that can handle the message's status.</exception> public SmsPdu DecodeShortMessage(ShortMessageFromPhone message) { PhoneMessageStatus status = (PhoneMessageStatus)message.Status; SmsPdu smsPdu = null; PhoneMessageStatus phoneMessageStatu = status; if (phoneMessageStatu == PhoneMessageStatus.ReceivedUnread || phoneMessageStatu == PhoneMessageStatus.ReceivedRead) { try { smsPdu = this.DecodeReceivedMessage(message); } catch (Exception exception) { this.LogIt(LogLevel.Warning, "Unable to decode message with specified status - trying other variants."); smsPdu = this.DecodeStoredMessage(message); } } else if (phoneMessageStatu == PhoneMessageStatus.StoredUnsent || phoneMessageStatu == PhoneMessageStatus.StoredSent) { try { smsPdu = this.DecodeStoredMessage(message); } catch (Exception exception1) { this.LogIt(LogLevel.Warning, "Unable to decode message with specified status - trying other variants."); smsPdu = this.DecodeReceivedMessage(message); } } else { string[] str = new string[5]; str[0] = "No decoder available for message of status \""; str[1] = status.ToString(); str[2] = "\" (index "; int index = message.Index; str[3] = index.ToString(); str[4] = ")."; string str1 = string.Concat(str); this.LogIt(LogLevel.Error, str1); throw new CommException(str1); } return smsPdu; }
/// <summary> /// AT+CMGL. Reads SMS messages from the current read/delete storage using the PDU mode. /// </summary> /// <param name="status">The message status</param> /// <returns>An array of <see cref="T:GsmComm.GsmCommunication.ShortMessage" /> objects representing the messages read.</returns> /// <remarks>Always switches to PDU mode at the beginning.</remarks> public ShortMessageFromPhone[] ListMessages(PhoneMessageStatus status) { ShortMessageFromPhone[] shortMessageFromPhoneArray; lock (this) { this.VerifyValidConnection(); this.ActivatePduMode(); this.LogIt(LogLevel.Info, string.Concat("Reading messages, requesting status \"", status.ToString(), "\"...")); string str = string.Concat("AT+CMGL=", (int)status); string str1 = this.ExecAndReceiveMultiple(str); ArrayList arrayLists = new ArrayList(); Regex regex = new Regex("\\+CMGL: (\\d+),(\\d+),(?:\"(\\w*)\")?,(\\d+)\\r\\n(\\w+)"); for (Match i = regex.Match(str1); i.Success; i = i.NextMatch()) { int num = int.Parse(i.Groups[1].Value); int num1 = int.Parse(i.Groups[2].Value); string value = i.Groups[3].Value; int num2 = int.Parse(i.Groups[4].Value); string value1 = i.Groups[5].Value; string[] strArrays = new string[8]; strArrays[0] = "index="; strArrays[1] = num.ToString(); strArrays[2] = ", stat="; strArrays[3] = num1.ToString(); strArrays[4] = ", alpha=\""; strArrays[5] = value; strArrays[6] = "\", length="; strArrays[7] = num2.ToString(); this.LogIt(LogLevel.Info, string.Concat(strArrays)); ShortMessageFromPhone shortMessageFromPhone = new ShortMessageFromPhone(num, num1, value, num2, value1); arrayLists.Add(shortMessageFromPhone); } int count = arrayLists.Count; this.LogIt(LogLevel.Info, string.Concat(count.ToString(), " message(s) read.")); ShortMessageFromPhone[] shortMessageFromPhoneArray1 = new ShortMessageFromPhone[arrayLists.Count]; arrayLists.CopyTo(shortMessageFromPhoneArray1, 0); shortMessageFromPhoneArray = shortMessageFromPhoneArray1; } return shortMessageFromPhoneArray; }
/// <summary> /// Stores a raw short message in the specified storage. /// </summary> /// <param name="message">The message to store.</param> /// <param name="storage">The storage to store the message in.</param> /// <returns>The index of the message. If the index could not be retrieved, zero is returned.</returns> /// <remarks> /// This function is useful for importing messages that were previously exported with <see cref="M:GsmComm.GsmCommunication.GsmCommMain.ReadRawMessages(GsmComm.GsmCommunication.PhoneMessageStatus,System.String)" />. /// <seealso cref="M:GsmComm.GsmCommunication.GsmCommMain.ReadRawMessages(GsmComm.GsmCommunication.PhoneMessageStatus,System.String)" /> /// </remarks> public int WriteRawMessage(ShortMessageFromPhone message, string storage) { this.theDevice.SelectWriteStorage(storage); return this.theDevice.WriteMessageToMemory(message.Data, message.Length, message.Status); }