Click here to Skip to main content
12,303,940 members (69,740 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
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



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 17-Nov-12 8:05am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160530.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100