<pre><pre lang="css">public class ShortMessage
{
#region Private Variables
private string index;
private string status;
private string sender;
private string alphabet;
private string sent;
private string message;
#endregion
#region Public Properties
public string Index
{
get { return index; }
set { index = value; }
}
public string Status
{
get { return status; }
set { status = value; }
}
public string Sender
{
get { return sender; }
set { sender = value; }
}
public string Alphabet
{
get { return alphabet; }
set { alphabet = value; }
}
public string Sent
{
get { return sent; }
set { sent = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
#endregion
}
public class ShortMessageCollection : List<ShortMessage>
{
}
#region Read SMS
public AutoResetEvent receiveNow;
public ShortMessageCollection ReadSMS(SerialPort port)
{
ShortMessageCollection messages = null;
try
{
#region Execute Command
ExecCommand(port, "AT", 300, "No phone connected");
ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");
ExecCommand(port, "AT+CSCS=\"PCCP437\"", 300, "Failed to set character set.");
ExecCommand(port, "AT+CPMS=\"SM\"", 300, "Failed to select message storage.");
string input = ExecCommand(port, "AT+CMGL=\"ALL\"", 5000, "Failed to read the messages.");
#endregion
#region Parse messages
messages = ParseMessages(input);
#endregion
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
if (messages != null)
return messages;
else
return null;
}
public ShortMessageCollection ParseMessages(string input)
{
ShortMessageCollection messages = new ShortMessageCollection();
try
{
Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
Match m = r.Match(input);
while (m.Success)
{
ShortMessage msg = new ShortMessage();
msg.Index = m.Groups[1].Value;
msg.Status = m.Groups[2].Value;
msg.Sender = m.Groups[3].Value;
msg.Alphabet = m.Groups[4].Value;
msg.Sent = m.Groups[5].Value;
msg.Message = m.Groups[6].Value;
messages.Add(msg);
m = m.NextMatch();
}
}
catch (Exception ex)
{
throw ex;
}
return messages;
}
#endregion