Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
single msg sent but when i try to send multi msgs i must wait thread.Sleep(10000)thats not good and if i give thread.Sleep(1000) or less this is not work
help me plz

/////Single msg
C#
private void button2_Click(object sender, EventArgs e)
       {
           SmsClass sm = new SmsClass(cboPorts.Text);
           sm.Opens();
           sm.sendSms(txtPhone.Text, txtMessage.Text);
           sm.Closes();
           MessageBox.Show("Message Sent!");
       }



////////////////Multi msgs



C#
private void btnSendGrouped_Click(object sender, EventArgs e)
        {
            SmsClass sm = new SmsClass(cboPorts.Text);

             for (int a = 0; a <= MobileNumbers.Items.Count - 1; a++)
            {

                sm.Opens();
                sm.sendSms(MobileNumbers.Items[a].ToString(), GtxtMessage.Text);
                sm.Closes();
                Thread.Sleep(10000);
                MessageBox.Show("Message Sent Successfully");
            }

             sm = null;
Posted

1 solution

Hi there,

Why do you close the connection every time through your loop then reopen it for the next message! That is insane! You should open the connection then send all your messages then close the connection - move the sm.Opens() and sm.Closes() outside the loop and remove the Sleep(). E.g.

C#
private void btnSendGrouped_Click(object sender, EventArgs e)
{
    SmsClass sm = new SmsClass(cboPorts.Text);

    sm.Opens();

    for (int a = 0; a <= MobileNumbers.Items.Count - 1; a++)
    {
        sm.sendSms(MobileNumbers.Items[a].ToString(), GtxtMessage.Text);
    }

    sm.Closes();
    MessageBox.Show("Messages Sent Successfully");

    sm = null;
}


Hope this helps,
Ed
 
Share this answer
 
Comments
Member 9411249 18-Nov-12 2:45am    
Thanx ED but this code sent msg only One Number(1st number of the loop) not other numbers
any idea what to do...?
Ed Nutting 18-Nov-12 6:37am    
Where did you get your SmsClass? Is there any documentation for it? If so, take a look at that or look through the original code - that might give you some idea why it didn't work. I would be very surprised if you had to open a new connection for every message that you want to send... If you can get at the original code you might well be able to adapt/bug fix it for your needs.

Ed
Member 9411249 18-Nov-12 13:27pm    
here SMS class
namespace SMS
{
class SmsClass
{
SerialPort serialPort;
public SmsClass(string comPort)
{
this.serialPort = new SerialPort();
this.serialPort.PortName = comPort;
this.serialPort.BaudRate = 9600;
this.serialPort.Parity = Parity.None;
this.serialPort.DataBits = 8;
this.serialPort.StopBits = StopBits.One;
this.serialPort.Handshake = Handshake.RequestToSend;
this.serialPort.DtrEnable = true;
this.serialPort.RtsEnable = true;

}
public bool sendSms(string cellNo, string sms)
{
string messages = null;
messages = sms;
if (this.serialPort.IsOpen == true)
{
try
{
this.serialPort.NewLine = System.Environment.NewLine;
this.serialPort.WriteLine("AT" + (char)(13));
Thread.Sleep(4);
this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
Thread.Sleep(5);
this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\"");
Thread.Sleep(10);
this.serialPort.WriteLine(">" + messages + (char)(26));
}
catch (Exception ex)
{
//MessageBox.Show(ex.Source);
}
return true;
}
else
return false;
}

public void Opens()
{
if (this.serialPort.IsOpen == false)
{
this.serialPort.Open();
}
}
public void Closes()
{
if (this.serialPort.IsOpen == true)
{
this.serialPort.Close();
}
}
}
}
Ed Nutting 18-Nov-12 14:11pm    
Looking at this code, in your send method adds in Thread.Sleeps to allow the attached hardware time to process each part of the message. However, there is no Thread.Sleep after the final WriteLine so it may be that you need something after that to delay enough for the message to be sent. It may be that your hardware has a maximum send rate (very likely that it does) since it takes several seconds for text messages to be sent. Add in a Thread.Sleep after the last WriteLine call. Adjust the time to the proper value or use trial and error to find what works.

My modified loop is the proper way to do this though and I still recommend using it. You should not continually open/close/reopen connections to your hardware for every message - maintain one connection and just limit the send rate.

I would be surprised if your hardware could handle sending more than one text message every few seconds without investment in upmarket kit or hardware with multiple transmitters/receivers/links to mobile companies.

Hope this helps,
Ed
Member 9411249 19-Nov-12 13:17pm    
Hm thnx ED i will try

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