利用gsm模块做的短信平台,由于软件是放在服务器上,不会关闭软件,在使用的第一天没有问题,可是在第二天短信就发不出去,下面的发短信的核心代码(利用SerialPort),群发时每次调用messageSingle函数,短信循环发送
public bool messageSingle(string strPhone, string message, string cNO)
{
try
{ portSet ps = new portSet();
PDUcoding sms = new PDUcoding();
if (Initialization(ps.Serial, ps.Baudrate))
{
if (sp.IsOpen)
{
//string cNO =exeAT("AT+CSCA?").Substring(20, 12);
char str1 = (char)26;
sp.Write("AT+CMGF=0\r");
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 300));
string decodedSMS = sms.smsDecodedsms(cNO, strPhone, message);
//string decodedSMS = sms.smsDecodedsms(strCententNO, strPhone, message);
sp.Write("AT+CMGS=" + sms.nLength + "\r\n");
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 300));
sp.Write(decodedSMS + str1.ToString());
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 3000));
string ok = sp.ReadExisting();
sp.DiscardOutBuffer();
sp.DiscardInBuffer(); if (ok.IndexOf("ERROR") <= 0)
{
sp.Close();
return true; }
else
{
return false;
} }
else
{
return false;
}
}
else
{
return false;
} }
catch
{ return false;
}
finally
{ getClose();
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 5000));
} }
不知道这是为什么?各位大侠帮忙看一下
public bool messageSingle(string strPhone, string message, string cNO)
{
try
{ portSet ps = new portSet();
PDUcoding sms = new PDUcoding();
if (Initialization(ps.Serial, ps.Baudrate))
{
if (sp.IsOpen)
{
//string cNO =exeAT("AT+CSCA?").Substring(20, 12);
char str1 = (char)26;
sp.Write("AT+CMGF=0\r");
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 300));
string decodedSMS = sms.smsDecodedsms(cNO, strPhone, message);
//string decodedSMS = sms.smsDecodedsms(strCententNO, strPhone, message);
sp.Write("AT+CMGS=" + sms.nLength + "\r\n");
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 300));
sp.Write(decodedSMS + str1.ToString());
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 3000));
string ok = sp.ReadExisting();
sp.DiscardOutBuffer();
sp.DiscardInBuffer(); if (ok.IndexOf("ERROR") <= 0)
{
sp.Close();
return true; }
else
{
return false;
} }
else
{
return false;
}
}
else
{
return false;
} }
catch
{ return false;
}
finally
{ getClose();
Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, 5000));
} }
不知道这是为什么?各位大侠帮忙看一下
解决的办法是:
(1).你要把发短信的任务做在一个线程中,每次发一条信息时,生成一个线程,然后再检查线程是否结束,只有结束了,才再进入下一个信息的发送(再创建一个新的线程)。
(2).Sleep调用之前之后最好调用一下Application.DoEvent().