短信报警

来源:互联网 发布:nginx apache ssl 编辑:程序博客网 时间:2024/04/29 19:56

具体代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SendMessage;

namespace WinInteSec
{
 /// <summary>
 /// SmsAdmin 的摘要说明。
 /// </summary>
 public class SmsAdmin : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TabControl tabControl1;
  private System.Windows.Forms.TabPage tabPage1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.ComboBox cbxport;
  private System.Windows.Forms.ComboBox cbxrate;
  private System.Windows.Forms.CheckBox ckbswitch;
  private System.Windows.Forms.Button button2;
  private AxMSCommLib.AxMSComm axMSComm1;
  private System.Windows.Forms.Label lblstyle;
  private System.Windows.Forms.Label lblmsg;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox txtphone;
  private System.Windows.Forms.RichTextBox rtbsms;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public SmsAdmin()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SmsAdmin));
   this.button1 = new System.Windows.Forms.Button();
   this.tabControl1 = new System.Windows.Forms.TabControl();
   this.tabPage1 = new System.Windows.Forms.TabPage();
   this.txtphone = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.rtbsms = new System.Windows.Forms.RichTextBox();
   this.lblmsg = new System.Windows.Forms.Label();
   this.lblstyle = new System.Windows.Forms.Label();
   this.axMSComm1 = new AxMSCommLib.AxMSComm();
   this.button2 = new System.Windows.Forms.Button();
   this.ckbswitch = new System.Windows.Forms.CheckBox();
   this.cbxrate = new System.Windows.Forms.ComboBox();
   this.label2 = new System.Windows.Forms.Label();
   this.cbxport = new System.Windows.Forms.ComboBox();
   this.label1 = new System.Windows.Forms.Label();
   this.tabControl1.SuspendLayout();
   this.tabPage1.SuspendLayout();
   ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button1.Location = new System.Drawing.Point(485, 287);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(56, 24);
   this.button1.TabIndex = 1;
   this.button1.Text = "确定";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // tabControl1
   //
   this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.tabControl1.Controls.Add(this.tabPage1);
   this.tabControl1.Location = new System.Drawing.Point(8, 8);
   this.tabControl1.Name = "tabControl1";
   this.tabControl1.SelectedIndex = 0;
   this.tabControl1.Size = new System.Drawing.Size(536, 272);
   this.tabControl1.TabIndex = 2;
   //
   // tabPage1
   //
   this.tabPage1.Controls.Add(this.txtphone);
   this.tabPage1.Controls.Add(this.label3);
   this.tabPage1.Controls.Add(this.rtbsms);
   this.tabPage1.Controls.Add(this.lblmsg);
   this.tabPage1.Controls.Add(this.lblstyle);
   this.tabPage1.Controls.Add(this.axMSComm1);
   this.tabPage1.Controls.Add(this.button2);
   this.tabPage1.Controls.Add(this.ckbswitch);
   this.tabPage1.Controls.Add(this.cbxrate);
   this.tabPage1.Controls.Add(this.label2);
   this.tabPage1.Controls.Add(this.cbxport);
   this.tabPage1.Controls.Add(this.label1);
   this.tabPage1.Location = new System.Drawing.Point(4, 21);
   this.tabPage1.Name = "tabPage1";
   this.tabPage1.Size = new System.Drawing.Size(528, 247);
   this.tabPage1.TabIndex = 0;
   this.tabPage1.Text = "功能设置";
   //
   // txtphone
   //
   this.txtphone.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.txtphone.Location = new System.Drawing.Point(264, 152);
   this.txtphone.Name = "txtphone";
   this.txtphone.Size = new System.Drawing.Size(256, 23);
   this.txtphone.TabIndex = 13;
   this.txtphone.Text = "";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(200, 160);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(56, 16);
   this.label3.TabIndex = 12;
   this.label3.Text = "电话号码";
   //
   // rtbsms
   //
   this.rtbsms.Location = new System.Drawing.Point(8, 0);
   this.rtbsms.Name = "rtbsms";
   this.rtbsms.Size = new System.Drawing.Size(528, 144);
   this.rtbsms.TabIndex = 11;
   this.rtbsms.Text = "";
   //
   // lblmsg
   //
   this.lblmsg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.lblmsg.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.lblmsg.Location = new System.Drawing.Point(188, 213);
   this.lblmsg.Name = "lblmsg";
   this.lblmsg.Size = new System.Drawing.Size(276, 23);
   this.lblmsg.TabIndex = 10;
   //
   // lblstyle
   //
   this.lblstyle.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.lblstyle.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.lblstyle.Location = new System.Drawing.Point(8, 213);
   this.lblstyle.Name = "lblstyle";
   this.lblstyle.Size = new System.Drawing.Size(176, 23);
   this.lblstyle.TabIndex = 9;
   this.lblstyle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // axMSComm1
   //
   this.axMSComm1.ContainingControl = this;
   this.axMSComm1.Enabled = true;
   this.axMSComm1.Location = new System.Drawing.Point(240, 64);
   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 = 7;
   this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(472, 208);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(56, 32);
   this.button2.TabIndex = 6;
   this.button2.Text = "测试";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // ckbswitch
   //
   this.ckbswitch.Checked = true;
   this.ckbswitch.CheckState = System.Windows.Forms.CheckState.Checked;
   this.ckbswitch.Cursor = System.Windows.Forms.Cursors.Hand;
   this.ckbswitch.Location = new System.Drawing.Point(200, 184);
   this.ckbswitch.Name = "ckbswitch";
   this.ckbswitch.Size = new System.Drawing.Size(86, 24);
   this.ckbswitch.TabIndex = 5;
   this.ckbswitch.Text = "短消息功能";
   //
   // cbxrate
   //
   this.cbxrate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
   this.cbxrate.Items.AddRange(new object[] {
               "300",
               "600",
               "1200",
               "2400",
               "4800",
               "9600",
               "14400",
               "19200",
               "38400",
               "56000",
               "57600",
               "115200",
               "256000"});
   this.cbxrate.Location = new System.Drawing.Point(64, 184);
   this.cbxrate.Name = "cbxrate";
   this.cbxrate.Size = new System.Drawing.Size(120, 20);
   this.cbxrate.TabIndex = 3;
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(8, 184);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(48, 16);
   this.label2.TabIndex = 2;
   this.label2.Text = "波特率";
   //
   // cbxport
   //
   this.cbxport.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
   this.cbxport.Items.AddRange(new object[] {
               "COM1",
               "COM2",
               "COM3",
               "COM4",
               "COM5",
               "COM6",
               "COM7",
               "COM8",
               "COM9",
               "COM10",
               "COM11",
               "COM12",
               "COM13",
               "COM14",
               "COM15",
               "COM16",
               "COM17",
               "COM18",
               "COM19",
               "COM20"});
   this.cbxport.Location = new System.Drawing.Point(64, 152);
   this.cbxport.Name = "cbxport";
   this.cbxport.Size = new System.Drawing.Size(120, 20);
   this.cbxport.TabIndex = 1;
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 160);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "串口";
   //
   // SmsAdmin
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(546, 314);
   this.Controls.Add(this.tabControl1);
   this.Controls.Add(this.button1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
   this.Name = "SmsAdmin";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "GSM/Modem设置";
   this.Load += new System.EventHandler(this.SmsAdmin_Load);
   this.tabControl1.ResumeLayout(false);
   this.tabPage1.ResumeLayout(false);
   ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  private void button1_Click(object sender, System.EventArgs e)
  {
   
   this.Close();
  }

  private void SmsAdmin_Load(object sender, System.EventArgs e)
  {
   cbxport.SelectedIndex = 0;
   cbxrate.SelectedIndex = 1; 
      this.lblstyle.Text = cbxport.Text +":"+ cbxrate.Text;
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   if(this.button2.Text == "测试")
   {
    if(txtphone.Text.Trim().Length == 0)
    {
     MessageBox.Show("请输入电话号码","操作提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
     txtphone.Focus();
     return;
    }
    if(rtbsms.Text.Trim().Length == 0 &&this.ckbswitch.Checked)
    {
     MessageBox.Show("请输入短消息内容","操作提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
     rtbsms.Focus();
     return;
    }   
    try
    {
    
     this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
     if( axMSComm1.PortOpen)
     {
      axMSComm1.PortOpen = false;
      axMSComm1.DTREnable = false;//添加部分
     }
     axMSComm1.CommPort   = (short)(cbxport.SelectedIndex+1);
     axMSComm1.Settings = this.cbxrate.Text+",N,8,1";    
     axMSComm1.RThreshold = 1;//Rthreshold 为 1,接收缓冲区收到每一个字符都会使 MSComm 控件产生 OnComm 事件
     axMSComm1.SThreshold = 0;
    
     axMSComm1.InputLen = 0;
     axMSComm1.PortOpen = true; //前面初始化串口端口设备
  
     axMSComm1.DTREnable = true;//20067-01-11日添加(数据终端准备好)

     axMSComm1.Output = System.Text.Encoding.ASCII.GetBytes("ATV1/r/n");  
    
     System.Threading.Thread.Sleep(100);
     axMSComm1.InputLen = 0;
     if(this.ckbswitch.Checked)
     {
      axMSComm1.Output = System.Text.Encoding.ASCII.GetBytes("AT+CMGF=0/r");
      System.Threading.Thread.Sleep(100);
      axMSComm1.InputLen = 0;
      axMSComm1.Output = System.Text.Encoding.ASCII.GetBytes("AT+CMGS="+(15+(rtbsms.Text.Length) * 2).ToString()+"/r");
      System.Threading.Thread.Sleep(100);
      axMSComm1.InputLen = 0;
      SendSMS(txtphone.Text.Trim(),txtphone.Text.Trim(),rtbsms.Text.Trim());          
      lblmsg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      lblmsg.Text = "正在发送短信.......";     
     }
     else
     { 
      System.Threading.Thread.Sleep(100);
      axMSComm1.InputLen = 0;
      axMSComm1.Output = "AT+CLIP=1/r";
      axMSComm1.Output = System.Text.Encoding.ASCII.GetBytes("ATD"+txtphone.Text.Trim()+";/r");
      lblmsg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      lblmsg.Text = "正在拨打电话.......";
      this.button2.Text = "挂断";
     }
    
    }
    catch
    {
     this.lblmsg .TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
     this.lblmsg .Text = "串口打开失败!请确认是否占用或已打开?";
    } 
    finally
    {     
     this.Cursor = System.Windows.Forms.Cursors.Default;
    }
   }
   else
   {
    axMSComm1.Output = System.Text.Encoding.ASCII.GetBytes("ATH/r");
    this.lblmsg .Text = "电话已挂断!";
    this.button2.Text = "测试";
   }
  }

  private void axMSComm1_OnComm(object sender, System.EventArgs e)
  {
   

   byte[] bytIn;
   object objIn;

   
   axMSComm1.InputMode=MSCommLib.InputModeConstants.comInputModeBinary;
   axMSComm1.InputLen = 0;
   objIn=axMSComm1.Input ; //这里注意axMSComm1.Input返回的是一个object的
   bytIn =(byte[])objIn;   //类型,所以必须使用显式的类型转换,这点和VB不同

   this.lblstyle.Text = cbxport.Text.Trim()+":"+cbxrate.Text.Trim()+" OK";
   
   for(int i =0 ;i<bytIn.Length;i++)
   {
    if(System.Convert.ToChar(bytIn[i]) == '>')
    {    
     System.Threading.Thread.Sleep(100);
     axMSComm1.InputLen = 0;
     this.SendSMS(txtphone.Text.Trim(),txtphone.Text.Trim(),rtbsms.Text.Trim());     
     break;
    }
   }
  }

  


  //手机号码转换为pdu模式
  private string telc(string MobileNum)
  {
   int tl;
   string ltem, rtem, ttem;
   int ti;

   ttem = "";
   tl = MobileNum.Trim().Length;
   if (tl != 11 && tl != 13)
   {
    MessageBox.Show("wrong number:" + MobileNum);
    return "";
   }

   if (tl == 11)  // 11位转换为13位
   {
    tl += 2;
    MobileNum = "86" + MobileNum;
   }

   for (ti = 0; ti < tl; ti += 2)
   {
    ltem = MobileNum.Substring(ti, 1);
    if (ti == tl - 1)
    {
     rtem = "F";
    }
    else
    {
     rtem = MobileNum.Substring(ti + 1, 1);
    }

    ttem += rtem + ltem;  //每两位颠倒
   }

   return ttem;
  }

  //Unicode解码函数
  private string ascg(string smsg)
  {
   string res="";
   string ls;
   string rs;

   byte[] resByte = System.Text.UnicodeEncoding.Unicode.GetBytes(smsg);
   for (int i = 0; i < resByte.Length ; i+=2)
   {
    ls = resByte[i].ToString("X2");
    rs = resByte[i + 1].ToString("X2");
    res = res + rs + ls; //注意这里高低位颠倒
   }
   return res.Trim();
  }

  /// <summary>
  /// 发送短信编码内容
  /// </summary>
  /// <param name="csca">预留短信中心号码</param>
  /// <param name="num">接送端手机号码</param>
  /// <param name="msg">发送的短信内容</param>
  private void SendSMS(string csca,string num, string msg)
  {
   string midx = "0011000D91";
   string sufx = "000800";

   string pdu,  pnum, pmsg;
   string leng;
   int length;
  
   length = (msg.Length) * 2;
   leng = length.ToString("X");

   if (length < 16)
   {
    leng = "0" + leng;
   }

//   psmsc = telc(csca).Trim();
   pnum = telc(num).Trim();
   
   pmsg = ascg(msg.Trim());
   pdu =  midx + pnum + sufx + leng + pmsg;//注意编码组合方式   
   byte[] end = { 0x1a };
   axMSComm1.Output = pdu + System.Text.ASCIIEncoding.ASCII.GetString(end);
  }
 点击查看原始尺寸

原创粉丝点击