amir hamza

amir hamza

  • 1.6k
  • 63
  • 1.7k

Multi Part SMS Read/ Write from GSM Modem

Nov 29 2015 1:37 AM
 
 My task is to read multi part SMS from GSM modem. (Model- MF710M HUSPA USB 3G Modem)
 
When I am reading message through my application it returns garbage value.
 
Original Message : " Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello                                          Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello" 
 
Returned Message:    
 
Part 1:            
 
"0500032E0202D86F10B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F05B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F10B2CC66BF41C8329BF 0621CB6CF61B842CB3D96F10B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F10B2CC66BF41C8329BFD0621CB6CF61B"  
 
Part-2
 
"0500032E0202D86F10B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F05B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F10B2CC66BF41C8329BF 0621CB6CF61B842CB3D96F10B2CC66BF41C8329BFD0621CB6CF61B842CB3D96F10B2CC66BF41C8329BFD0621CB6CF61B"  
 
The code Snippet which is worked for reading this message:
 
 
 
public clsMessageCollection ReadSMS(SerialPort port, string p_strCommand)
{
// Set up the Modem and read the messages
clsMessageCollection messages = null;
try
{
#region Execute Command
// Check connection
ExecCommand(port, "AT", 300, "No Modem connected");
// Use message format "Text mode"
ExecCommand(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
ExecCommand(port, "AT+CPMS=\"SM\"", 300, "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 string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
port.Write(command + "\r");
string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
throw new ApplicationException("No success message was received.");
return input;
}
catch (Exception ex)
{
throw ex;
}
}
  
public string ReadResponse(SerialPort port, int timeout)
{
string buffer = string.Empty;
try
{
do
{
if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length > 0)
throw new ApplicationException("Response received is incomplete.");
else
throw new ApplicationException("No data received from Modem.");
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
 
 
Kindly suggest me is there any problem or any change required in my code or suggest me any helpful link where I find clear explanation about multi part sms reading.