1

I'm trying to develop a sms sending system using asp.net mvc 4 where I'm using HUAWEI USB 3G modem to send sms. Problem is, when I receive sms, most often the text are coming like this,

AT+CMGF=1 AT+CMGS="+8801671234567" Name: Sample Name Amount1: 1000.....

Here are my codes,

public ActionResult SendSms(int RentId=0)
{
    AmountInfo BillTable = db.AmountInfoes.Find(RentId);
    var GetName = db.AmountInfoes.Where(a => a.serial.Equals(BillTable.serial)).FirstOrDefault();
    string getName = GetName.name;
    int getAmount1 = GetName.amount1;
    int getAmount2 = GetName.amount2;
    int getAmount3 = GetName.amount3;
    int getAmount4 = GetName.amount4;
    int getAmount5 = GetName.amount5;
    int getAmount6 = GetName.amount6;
    string getNumber = GetName.phone;
    try
    {
        SP.PortName = "COM8";
        SP.Close();
        SP.Open();
        string ph_no;
        string the_no = getNumber;
        string txt_msg = "Name: " + getName + "   Amount1: " + getAmount1 + "   Amount2: " + getAmount2 + "   Amount3: " + getAmount3 + "   Amount4: " + getAmount4 + "   Amount5: " + getAmount5 + "   Amount6: " + getAmount6;
        ph_no = Char.ConvertFromUtf32(34) + the_no + char.ConvertFromUtf32(34);
        SP.Write("AT+CMGF=1" + Char.ConvertFromUtf32(13));
        SP.Write("AT+CMGS=" + ph_no + Char.ConvertFromUtf32(13));
        SP.Write(txt_msg + Char.ConvertFromUtf32(26) + Char.ConvertFromUtf32(13));
        SP.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return RedirectToAction("RentManager");
}

Is there something wrong in my code? How can I fix this problem where AT commands will not appear in my sms? Need this help badly. Thanks.

captainsac
  • 2,484
  • 3
  • 27
  • 48
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67
  • 1
    You are writing the commands in quick succession, not waiting for a result. [This answer](http://stackoverflow.com/a/15198219/60761) has a very crude solution with Sleep() but you might try it to diagnose the issue. And lots of similar questions here and samples on CodeProject. – H H May 14 '15 at 08:56
  • And `"AT+CMGF=1" + Char.ConvertFromUtf32(13)` is just `"AT+CMGF=1\r"` – H H May 14 '15 at 08:57
  • Tnx. Let me check if it works. – Shihan Khan May 14 '15 at 08:59
  • Seems like it's working. Though I'm having trouble in my network. Let me check again after changing my sim card. You should probably post your solution as an answer so that I can tick on it. & thanks for your response. :) – Shihan Khan May 14 '15 at 09:55
  • No, when you find a better answer (actually Reading the response) you should post a self-answer. Otherwise I will close this as a dupe later. – H H May 14 '15 at 14:49
  • No, please never, never, never use `Sleep` as a substitute for reading responses. See [this answer](http://stackoverflow.com/a/20731000/23118) for more information. – hlovdal May 14 '15 at 21:43
  • @hlovdal, Why? What will go wrong if I use `Sleep`? – Shihan Khan May 16 '15 at 13:00
  • @SinOscuras can you help me with the code, I am trying to use similar code but not working from my side. I also posted it in StackOverflow [link](http://stackoverflow.com/questions/30504495/at-commands-to-send-sms-not-working-in-windows-8-1) – Luzan Baral Jun 08 '15 at 08:39

0 Answers0