《短信收发程序c.doc》由会员分享,可在线阅读,更多相关《短信收发程序c.doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、,using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading;using System.Text;using JustinIO;using SMS;using System.IO;using System.Timers;public struct message/定时发送相关信息 public string number; public strin
2、g center; public string mess; ; message message1 = new message(); message message2 = new message(); message message3 = new message(); string state = ;/存储发送情况 int x = 0, y = 0;/发送计数 / / 应用程序的主入口点。 / STAThread static void Main() Application.Run(new Form1(); / 初始化串口 public bool InitCom(string m_port, i
3、nt 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(错误:
4、+ e.Message); return false; /初始化代码,并获取相关信息 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) ss_port.Write(Encoding.ASCII.GetBytes(AT
5、+CGMIr); /获取品牌 string response = Encoding.ASCII.GetString(ss_port.Read(128); if (response.Length 0) ConnectState.Text = response.Substring(2, 16); Connected = true; else ConnectState.Text = 连接不成功; Connected = false; ss_port.Write(Encoding.ASCII.GetBytes(AT+CGMMr);/获取型号 response = Encoding.ASCII.GetS
6、tring(ss_port.Read(128); if (response.Length 0) ConnectState.Text = ConnectState.Text + + response.Substring(2, 6) + 已连接; Connected = true; else ConnectState.Text = 连接不成功; Connected = false; ss_port.Write(Encoding.ASCII.GetBytes(AT+CSCA?r);/获取短信中心号 response = Encoding.ASCII.GetString(ss_port.Read(12
7、8); if (response.Length 0) CenterNumber.Text = response.Substring(11, 13); Connected = true; else Connected = false; if (Connected = true) btnConnect.Enabled = false; btnSend.Enabled = true; receive.Enabled = true; unread.Enabled = true; sendstate.Enabled = true; clearsend.Enabled = true; else btnCo
8、nnect.Enabled = true; btnSend.Enabled = false; receive.Enabled = false; unread.Enabled = false; sendstate.Enabled = false; clearsend.Enabled = false; timer2.Start();/连接成功后,每1秒自动读取一次未读信息,若有未读则显示 /发送短信 private void btnSend_Click(object sender, System.EventArgs e) /输入对方手机号码不足或短信为空时报错并重启 string num = ta
9、rgetNumber.Text; num = num.Substring(0, 2); bool result = string.Equals(num, 13); if (!result) result = string.Equals(num, 14); if (!result) result = string.Equals(num, 15); if (!result) result = string.Equals(num, 18); if (!result) | (targetNumber.TextLength != 11) | (smsContent.TextLength = 0) Mes
10、sageBox.Show(请检查对方手机号且短信内容不得为空!, 警告); return; /选择定时则启动定时器 if (radioButton1.Enabled & hour.Enabled)/定时1 message1.number = targetNumber.Text; message1.center = CenterNumber.Text; message1.mess = smsContent.Text; int second = (int.Parse(hour.Text) * 60 + int.Parse(min.Text) * 60+1;/若定时为0,防止定时出错,默认为1秒 t
11、imer3.Interval = 1000 * second; smsState.Text = 信息将在 + hour.Text + 小时 + min.Text + 分钟之后发出。rn; timer3.Start(); radioButton1.Enabled = false; return; else if (radioButton2.Enabled & hour2.Enabled)/定时2 message2.number = targetNumber.Text; message2.center = CenterNumber.Text; message2.mess = smsContent.
12、Text; int second = (int.Parse(hour2.Text) * 60 + int.Parse(min2.Text) * 60+1; timer4.Interval = 1000 * second; smsState.Text = 信息将在 + hour2.Text + 小时 + min2.Text + 分钟之后发出。rn; timer4.Start(); radioButton2.Enabled = false; return; else if (radioButton4.Enabled & hour3.Enabled)/定时3 message3.number = ta
13、rgetNumber.Text; message3.center = CenterNumber.Text; message3.mess = smsContent.Text; int second = (int.Parse(hour3.Text) * 60 + int.Parse(min3.Text) * 60 + 1; timer5.Interval = 1000 * second; smsState.Text = 信息将在 + hour3.Text + 小时 + min3.Text + 分钟之后发出。rn; timer5.Start(); radioButton4.Enabled = fal
14、se; return; Thread.Sleep(2000);/当前线程延迟2秒,提高发送成功率 string decodedSMS = sms.smsDecodedsms(CenterNumber.Text, targetNumber.Text, smsContent.Text); byte buf = Encoding.ASCII.GetBytes(String.Format(AT+CMGS=0r, sms.nLength); ss_port.Write(buf); string response = Encoding.ASCII.GetString(ss_port.Read(128);
15、string SendState = ; /ConnectState.Text = UCS2: + decodedSMS;/短信编码 if (response.Length 0 & response.EndsWith( ) ss_port.Write(Encoding.ASCII.GetBytes(String.Format(0x01a, decodedSMS); SendState = 发送成功!r; x += 1; else SendState = 发送失败。r; y += 1; string Result = String.Format(0 内容:1 2rn, targetNumber.
16、Text, smsContent.Text, SendState); smsState.Text += Result; state += Result; / 关闭串口,退出程序 private void btnExit_Click(object sender, System.EventArgs e) ss_port.Close(); Application.Exit(); /断开 private void btnDisconnect_Click(object sender, EventArgs e) ss_port.Close(); state = ; btnConnect.Enabled =
17、 true; btnSend.Enabled = false; receive.Enabled = false; unread.Enabled = false; sendstate.Enabled = false; clearsend.Enabled = false; CenterNumber.Text = ; ConnectState.Text = 未连接任何设备; private void Form1_Load(object sender, EventArgs e) /清空状态 private void clear_Click(object sender, EventArgs e) sms
18、State.Clear(); /收件箱 private void receive_Click(object sender, EventArgs e) ss_port.Write(Encoding.ASCII.GetBytes(AT+CMGL=4r); /列出SIM卡中所有短信 string response = Encoding.ASCII.GetString(ss_port.Read(5620); if (response.IndexOf(+CMGL:, 0) -1) response = sms.smsunPDU(response); smsState.Text = response; /
19、清空收件箱 private void unread_Click(object sender, EventArgs e) for (int i = 0; i -1) response = response.Substring(response.IndexOf(rn+CMGL:, 0); response = sms.smsunPDU(response); smsState.Text = 收到新的短消息rn + response; /发送情况 private void sendstate_Click(object sender, EventArgs e) smsState.Text = state
20、 + rn + 已发送成功 + x.ToString() + 条,失败 + y.ToString() + 条。; private void clearsend_Click(object sender, EventArgs e) state = ; /定时发送1 private void timer3_Tick(object sender, EventArgs e) string decodedSMS = sms.smsDecodedsms(message1.center, message1.number, message1.mess); byte buf = Encoding.ASCII.Ge
21、tBytes(String.Format(AT+CMGS=0r, 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(0x01a, decodedSMS); SendState = 发送成功!r; x += 1; else
22、SendState = 发送失败。r; y += 1; string Result = String.Format(0 内容:1 2rn, message1.number, message1.mess, SendState); smsState.Text += Result; state += Result; timer3.Stop(); radioButton1.Enabled = true ; /定时发送2 private void timer4_Tick(object sender, EventArgs e) string decodedSMS = sms.smsDecodedsms(m
23、essage2.center, message2.number, message2.mess); byte buf = Encoding.ASCII.GetBytes(String.Format(AT+CMGS=0r, 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
24、.GetBytes(String.Format(0x01a, decodedSMS); SendState = 发送成功!r; x += 1; else SendState = 发送失败。r; y += 1; string Result = String.Format(0 内容:1 2rn, message2.number, message2.mess, SendState); smsState.Text += Result; state += Result; timer4.Stop(); radioButton2.Enabled = true ; /定时发送3 private void ti
25、mer5_Tick(object sender, EventArgs e) string decodedSMS = sms.smsDecodedsms(message3.center, message3.number, message3.mess); byte buf = Encoding.ASCII.GetBytes(String.Format(AT+CMGS=0r, sms.nLength); ss_port.Write(buf); string response = Encoding.ASCII.GetString(ss_port.Read(128); string SendState
26、= ; if (response.Length 0 & response.EndsWith( ) ss_port.Write(Encoding.ASCII.GetBytes(String.Format(0x01a, decodedSMS); SendState = 发送成功!r; x += 1; else SendState = 发送失败。r; y += 1; string Result = String.Format(0 内容:1 2rn, message3.number, message3.mess, SendState); smsState.Text += Result; state +
27、= Result; timer5.Stop(); radioButton4.Enabled = true ; /定时读取区域1 private void radioButton1_CheckedChanged(object sender, EventArgs e) hour.Enabled = !hour.Enabled; min.Enabled = !min.Enabled; /定时读取2 private void radioButton2_CheckedChanged(object sender, EventArgs e) hour2.Enabled = !hour2.Enabled; min2.Enabled = !min2.Enabled; / 定时读取3 private void radioButton4_CheckedChanged(object sender, EventArgs e) hour3.Enabled = !hour3.Enabled; min3.Enabled = !min3.Enabled;