📄 form1.cs
字号:
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(544, 437);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.targetNumber);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.btnSend);
this.Name = "Form1";
this.Text = "终端短信群发";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 初始化串口
/// </summary>
public bool InitCom(string m_port, int m_baudrate)
{
ss_port.PortNum = m_port;
ss_port.BaudRate = m_baudrate;
ss_port.ByteSize = 8;
ss_port.Parity = 0;
ss_port.StopBits = 1;
ss_port.ReadTimeout = 1000;
try
{
if (ss_port.Opened)
{
ss_port.Close();
ss_port.Open();
}
else
{
ss_port.Open();//打开串口
}
return true;
}
catch
(Exception e)
{
MessageBox.Show("错误:" + e.Message);
return false;
}
}
/// <summary>
/// 初始化代码,并获取手机相关信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, System.EventArgs e)
{
bool opened = InitCom(ConnectPort.SelectedItem.ToString(),Convert.ToInt32(ConnectBaudRate.SelectedItem.ToString()));
bool Connected = false;
if (opened)
{
string response;
try
{
ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //获取手机品牌
response= Encoding.ASCII.GetString(ss_port.Read(128));
if (response.Length > 0)
{
sign=response.Substring(10,7);
ConnectState.Text = "与手机连接成功";
Connected = true;
}
else
{
ConnectState.Text = "与手机连接不成功";
Connected = false;
}
}
catch(Exception ex)
{
}
try
{
ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMM\r"));//获取手机型号
response = Encoding.ASCII.GetString(ss_port.Read(128));
if(response.Length > 0)
{
ConnectState.Text =ConnectState.Text+ " " + response.Substring(10,5) + " 连接中......";
Connected = true;
}
else
{
ConnectState.Text = "与手机连接不成功";
Connected = false;
}
}
catch
{
}
ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
response = Encoding.ASCII.GetString(ss_port.Read(128));
if(response.Length > 0)
{
CenterNumber = response.Substring(20,13);
Connected = true;
}
else
{
Connected = false;
}
if (Connected == true)
{
ConnectState.Text = "手机品牌:" + sign + "短信息中心号码:" + this.CenterNumber;
btnConnect.Enabled = false;
btnSend.Enabled = true;
}
else
{
btnConnect.Enabled = true;
btnSend.Enabled = false;
}
}
ss_port.Close();
}
/// <summary>
/// 发送短信
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, System.EventArgs e)
{
this.smsContent.ReadOnly=true;
this.btnSend.Enabled=false;
this.button1.Enabled=true;
th=new Thread(new ThreadStart(this.SMSSend));
th.Start();
}
private void SMSSend()
{
if(this.smsContent.Text.Trim().Length>70)
{
MessageBox.Show("群法内容不得超过70字,请重新填写!");
}
else if(this.smsContent.Text.Trim().Length==0)
{
MessageBox.Show("群法内容不得为空,请重新填写!");
}
else
{
int i=0;
string [] numbers=this.targetNumber.Text.Split(new char[]{'*'});
for(i=0;i<numbers.Length;i++)
{
bool opened = InitCom(ConnectPort.SelectedItem.ToString(),Convert.ToInt32(ConnectBaudRate.SelectedItem.ToString()));
if(opened)
{
try
{
if(numbers[i].Trim()!="")
{
string decodedSMS = sms.smsDecodedsms(CenterNumber,numbers[i],smsContent.Text);
byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r",sms.nLength));
ss_port.Write(buf);
string response = Encoding.ASCII.GetString(ss_port.Read(128));
string SendState = "";
if( response.Length > 0 && response.EndsWith("> "))
{
ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a",decodedSMS)));
response= Encoding.ASCII.GetString(ss_port.Read(128));
SendState = "发送成功!";
}
else
{
SendState = "发送失败";
}
string Result = String.Format("{2}:{0}->{1}。\n\r",numbers[i],SendState,i+1);
if (smsState.Text.Length>10000)
{
smsState.Text="";
}
smsState.AppendText(Result + "\n\r");
System.Threading.Thread.Sleep(6500);
buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGD=1,4\r"));
ss_port.Write(buf);
System.Threading.Thread.Sleep(500);
response = Encoding.ASCII.GetString(ss_port.Read(128));
System.IO.StreamWriter sw=new StreamWriter("log.txt",true);
sw.WriteLine(Result);
sw.Close();
}
}
catch (Exception ex)
{
smsState.Text +=ex.ToString();
}
finally
{
this.btnSend.Enabled=true;
this.button1.Enabled=false;
this.smsContent.ReadOnly=false;
ss_port.Close();
}
}
}
}
}
/// <summary>
/// 关闭串口,退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, System.EventArgs e)
{
try
{
th.Abort();
}
catch
{
}
ss_port.Close();
Application.Exit();
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
this.openFile.ShowDialog();
if(this.openFile.FileName!="")
{
System.IO.StreamReader st =new StreamReader(this.openFile.OpenFile());
while(st.Peek()!=-1)
{
this.targetNumber.Text +=st.ReadLine().Trim() + "*";
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.smsContent.ReadOnly=false;
th.Abort();
MessageBox.Show("群发已停止!","友情提示");
this.btnSend.Enabled = true;
}
catch
{
}
}
private void ConnectBaudRate_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -