I’m working on a program in C# that can send a SMS from my computer using my GSM modem, and I got most of my program to work.
I can send most Hayes AT commands like: “AT” and “AT+CGMI;+CGMM”, which returns the message “OK”, so I know I can communicate with the modem.
However I DO have problems sending a standard 7-bit encoded message from the modem to my cell phone.
With the help of PDUspy I have a somewhat confident that I’m encoding my message right.
However the following code fails miserably:
public string SendEncodedSms(string reciever, string message)
{
string response = GetResponse("AT+CMGF=0");
if (isFine(response))
{
string encodedBody = "000100" + EncodedReceiver(reciever) + "0000";
encodedBody += EncodeToSeptet(message);
int cmgs_header = encodedBody.Length / 2;
port.WriteLine("AT+CMGS=" + cmgs_header.ToString() + "\r\n");
port.WriteLine(encodedBody + (char)26);
return ReadResponse(300);
}
else throw new ApplicationException("Cant go into SMS PDU mode");
}
There five function calls inside:
-
GetResponse() is function that works as a wrapper when communicating with the modem – always making sure each command ends with a “\r”, and returns OK or Error depending on the response from the modem.
-
isFine() is a simple function that check whether or not the response from GetResponse() contained a “OK” message.
-
ReadResponse() returns any response from the modem whether that is OK or any error messages and whatever there also might be embedded between the AT command and the status message.
-
EncodeReceiver() encode the receivers phone number in reverse nibble notation. E.g. 12345678 becomes 21436587.
-
EncodeToSeptet() Encodes the message from 8bit notation to 7bit notation.
All functions have been confirmed to work fine using PDU spy and responses sent to debug window.
Calling SendEncodedSms("<my phone number>", "test") returns:
AT+CMGS=17
0001000A91xxxxxxxxxx000004F4F29C0E
+CMS ERROR: 304
I replaced my encoded phone number for privacy issues… 😉
- Is there anybody who can give me a hint what I am failing at?
UPDATED:
Link hinted I was calculating cmgs_header wrong.
After a bit of cleaning code I got:
public string SendEncodedSms(string receiver, string message)
{
if(isFine("AT+CMGF=0"))
{
string encodedBody = EncodedReceiver(receiver) + "0000";
encodedBody += EncodeToSeptet(message);
/* The +2 in calculation is a hack. Its probleby two of the octets
* in 000100 that should be a part of length calculation. But need
* to verify in against severeal sites.
*/
int cmgs_header = (encodedBody.Length / 2) + 2;
encodedBody = "000100" + encodedBody;
// Rest is as before.
...
}
else ...
}
Maybe this helps you. Why don’t you send the SMS in text mode?