📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Threading;
using System.IO;
using asitelcom;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button2;
private AxMSCommLib.AxMSComm axMSComm1;
private System.Windows.Forms.Timer receive;
private System.ComponentModel.IContainer components;
private string buii="";
private sendmsg fs=new sendmsg();
private recmsg rec=new recmsg();
private string tem;
private string lastph;
private string lastmsg;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Timer ListSim;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private string pdu;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.listBox1 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.axMSComm1 = new AxMSCommLib.AxMSComm();
this.receive = new System.Windows.Forms.Timer(this.components);
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.ListSim = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.listBox1.ItemHeight = 12;
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(432, 88);
this.listBox1.TabIndex = 0;
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 200);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 2;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// axMSComm1
//
this.axMSComm1.Enabled = true;
this.axMSComm1.Location = new System.Drawing.Point(112, 48);
this.axMSComm1.Name = "axMSComm1";
this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState")));
this.axMSComm1.Size = new System.Drawing.Size(38, 38);
this.axMSComm1.TabIndex = 6;
//
// receive
//
this.receive.Tick += new System.EventHandler(this.receive_Tick);
//
// button3
//
this.button3.Location = new System.Drawing.Point(24, 200);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 24);
this.button3.TabIndex = 12;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 112);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 13;
this.textBox1.Text = "+8613010851500";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 144);
this.textBox2.MaxLength = 11;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 21);
this.textBox2.TabIndex = 14;
this.textBox2.Text = "";
//
// ListSim
//
this.ListSim.Tick += new System.EventHandler(this.ListSim_Tick);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 24);
this.label1.TabIndex = 15;
this.label1.Text = "短信息中心号码";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 152);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 16;
this.label2.Text = "目的手机号码";
//
// label3
//
this.label3.Location = new System.Drawing.Point(296, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 16);
this.label3.TabIndex = 17;
this.label3.Text = "短信内容";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(232, 136);
this.textBox3.MaxLength = 70;
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(192, 96);
this.textBox3.TabIndex = 18;
this.textBox3.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 245);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox3,
this.label3,
this.label2,
this.label1,
this.textBox2,
this.textBox1,
this.button3,
this.axMSComm1,
this.button2,
this.listBox1});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "收发短信程序";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void receive_Tick(object sender, System.EventArgs e)//接收短信定时器处理函数每秒执行一次
{
if(this.axMSComm1.InBufferCount>0)//串口控件axMSComm1输入缓冲区存在数据
{ buii=(string)this.axMSComm1.Input; }//将输入缓冲区数据读入buii字符串
if(buii.IndexOf("0891",0)>=0 && buii.IndexOf("+CMGL",0)>=0)//判断是否为CMGL命令返回的信息
{
int start=buii.IndexOf("+CMGL:",0);int end=buii.IndexOf("OK",0);//确定返回信息中CMGL和OK的位置
if(start!=-1 && end!=-1) //如果存在CMGL和OK
{
buii=buii.Substring(start);//截取以CMGL开头的buii串
while(buii.IndexOf("0891")!=-1)//存在0891,即PDU串的开始位置
{
int start1=buii.IndexOf("0891",0);//确定PDU串的开始位置
buii=buii.Substring(start1); //取以0891开头的buii串
int start2=buii.IndexOf("+CMGL:");//下一个CMGL的位置
if(start2!=-1)//若存在下一个CMGL 即从输入缓冲区读取不止一条短信
{
string tem=buii.Substring(0,start2-2);//得到一个完整的PDU串tem,start2-2表示去掉下一个CMGL前的<CR>和<LF>
rec.read_sim(tem);//对PDU串进行解析rec.Msg为解析得到的用户信息,rec.phone为解析得到的手机号码
if(lastmsg==rec.Msg&&lastph==rec.phone)
{buii="";continue;}//若为已收到信息则跳出循环
lastmsg=rec.Msg;lastph=rec.phone;
buii=buii.Substring(start2); //将buii串截取成以下一个CMGL开头的串
}
else//从输入缓冲区只读取到一条短信
{
int ste=buii.IndexOf("OK",0);//确定系统返回的OK标志的位置
tem=buii.Substring(0,ste-4);//得到完整的PDU串,ste-4表示去掉4个字节的OK标志位,从后面对PDU串进行定位(不同于多条短信的情况)
buii="";//清空buii串
rec.read_sim(tem);//对PDU串进行解析rec.Msg为解析得到的用户信息,rec.phone为解析得到的手机号码
if(lastmsg==rec.Msg&&lastph==rec.phone)
{buii="";continue;}//若为已收到信息则跳出循环
lastmsg=rec.Msg;lastph=rec.phone;
}//else
this.listBox1.Items.Insert(0,"收到新的信息**********"+rec.Msg); //将收到的信息输出到屏幕
}//while
}//if
}//if
}//reasd
private void button3_Click(object sender, System.EventArgs e)//发送短信
{
this.ListSim.Enabled=false;//发送时关闭列出SIM卡信息的定时器
this.receive.Enabled=false;//发送时关闭处理CMGL返回信息的定时器
fs.setup_pdu(textBox1.Text,textBox2.Text,textBox3.Text);//根据用户输入合成PDU串
pdu=fs.msg_pdu;
this.axMSComm1.Output="AT+CMGC="+fs.lnth+(char)13;//先发送AT+CMGC命令,fs.lnth为CMGC中的<length>
Thread.Sleep(100); //延迟以便设备处理
this.axMSComm1.Output=pdu ;//再输入PDU串
this.axMSComm1.Output=((char)26).ToString();//CTRL+C
this.ListSim.Enabled=true;//打开列出SIM卡信息的定时器
this.receive.Enabled=true;//打开处理CMGL返回信息的定时器
}
private void Form1_Load(object sender, System.EventArgs e)//窗体打开时初始化串口控件
{
axMSComm1.CommPort=1;//采用串口1
axMSComm1.Settings="19200,n,8,1";//波特率19200,没有校验位,数据位为8位,1位停止位
axMSComm1.PortOpen=true; //打开串口
this.receive.Interval=1000;//每1秒处理一次CMGL返回信息
this.receive.Enabled=true;//打开定时器
this.ListSim.Interval=6000;//每6秒执行一次CMGL命令
this.ListSim.Enabled=true;//打开定时器
}
private void ListSim_Tick(object sender, System.EventArgs e)//每6秒执行一次CMGL命令
{
this.axMSComm1.Output="AT+CMGL=0"+(char)13;//执行CMGL命令,列出SIM卡中的信息
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -