Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm developing SMS system using C#. I use GSMCOMM
how to send sms with more than 160 characters and enable the customer to receive cut SMS as ONE SMS
My code is
C#
  public ShortMessageCollection ReadSMS(SerialPort port, string p_strCommand)
        {

            // Set up the phone and read the messages
            ShortMessageCollection messages = null;
            try
            {

                #region Execute Command
                // Check connection
                ExecCommand(port, "AT", 5000, "No phone connected");
                // Use message format "Text mode"
                ExecCommand(port, "AT+CMGF=1", 5000, "Failed to set message format.");
                // Use character set "PCCP437"
                //ExecCommand(port,"AT+CSCS=\"PCCP437\"", 300, "Failed to set character set.");
                // Select SIM storage
                ExecCommand(port, "AT+CSCS=\"GSM\"", 5000, "Failed to set character set.");
                ExecCommand(port, "AT+CPMS=\"SM\"", 5000, "Failed to select message storage.");
                // Read the messages
                string input = ExecCommand(port, p_strCommand, 5000, "Failed to read the messages.");
                #endregion

                #region Parse messages
                messages = ParseMessages(input);
                #endregion

            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (messages != null)
                return messages;
            else
                return null;

        }
public ShortMessageCollection ParseMessages(string input)
{
    var r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""",
        RegexOptions.Compiled);
    var messages = new ShortMessageCollection();
    using (var sw = new StringReader(input))
    {
        string currentLine = sw.ReadLine();
        while (currentLine != null)
        {
            var m = r.Match(currentLine);
            if (m.Success)
            {
                // read the first line of the message
                string message = string.Empty;
                currentLine = sw.ReadLine();

                // Append any extra lines to our message, unless it's a new record
                while (currentLine != null && !r.IsMatch(currentLine))
                {
                    message += Environment.NewLine;
                    message += currentLine;

                    currentLine = sw.ReadLine();
                }

                messages.Add(new ShortMessage
                {
                    Index = m.Groups[1].Value,
                    Status = m.Groups[2].Value,
                    Sender = m.Groups[3].Value,
                    Alphabet = m.Groups[4].Value,
                    Sent = m.Groups[5].Value,
                    Message = message,
                });
            }
            else
            {
                        
                currentLine = sw.ReadLine();
            }
        }
    }
    return messages;
}
Posted
Updated 29-Apr-14 21:31pm
v3
Comments
lukeer 30-Apr-14 1:11am    
When posting code, please include it in tags like these:
<pre lang="c#">YourCodeHere();</pre>
This time, I did it for you. As you can see, it makes the code a lot easier to read.
Rini Rajan 30-Apr-14 1:18am    
Thanks lukker for your help

1 solution

You'll want to use the SmartMessageFactory[^] for creating the concatenated messages, and SmartMessageDecoder[^] to assemble a set of multipart messages back to one.

I think there's a method called ReadRawPduMessage or something like that that you can use to get the Pdu of the message, then inspect that to see if it is a multipart message and if it is use SmartMessageDecoder to build op the concatenated result.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900