如何用gprs段信猫发短信

    技术2022-05-11  169

    项目需求:由于上周机房温度过高造成机房失火,要求实现温度预警程式。

    设备:感温器(串口通讯,每四秒将当前温度发送到串口)

              GRPS modem (usb模拟串口通讯,通过at指令实现短信发送)

              SERVER一台

                at command 流程大致如下 :                      1. atz                       (reset modem)                      2. at+cpms=”MT” (select message storage)                      3. at+cscs=”ASCII” (select TE character set                      4. at+cmgw=0939112781 <cr> Your SMS Message here (0939112781 is my phone number, write to storage and get an index, ie 5)                      5. at+cmss=5 (5 is the index you got at step 4, send message)                      6. at+cmgd=5 (5 is the index you got at step 4, delete message to prevent the storage full). 核心代码: -------------------------------------------vb.net-----------------------------------------------------------------------------------------   Dim Instring As String        Dim Buffer$        Dim ComPORT As Integer        Dim PhoneNUM As String        Dim SendMSG As String        ComPORT = Me.TextBox1.Text        PhoneNUM = TextBox2.Text        SendMSG = TextBox3.Text        AxMSComm1.CommPort = ComPORT         AxMSComm1.Settings = "115200,N,8,1"         AxMSComm1.InputLen = 0         AxMSComm1.PortOpen = True         List1.Items.Clear()         Out_STR = ""         AxMSComm1.Output = "atz" & Chr(13) ' 絋﹚        Call COMM1()         Application.DoEvents()        AxMSComm1.Output = "at+cpms=MT" & Chr(13) ' 絋﹚        Call COMM1()         Application.DoEvents()        AxMSComm1.Output = "at+cscs=ASCII" & Chr(13) ' 絋﹚        Call COMM1()         Application.DoEvents()        AxMSComm1.Output = "at+cmgw=" + Chr(34) + PhoneNUM + Chr(34) & Chr(13)    ' 絋﹚         Application.DoEvents()        AxMSComm1.Output = SendMSG & Chr(26)  ' 絋﹚        Call COMM1()         Application.DoEvents()        If Out_STR <> "" Then            AxMSComm1.Output = "at+cmss=" & Out_STR & Chr(13) ' 絋﹚            Call COMM1()            Application.DoEvents()            AxMSComm1.Output = "at+cmgd=" & Out_STR & Chr(13) ' 絋﹚            Call COMM1()        Else            MsgBox("Sorry,ERROR", vbOKOnly + vbCritical, "ERROR")        End If         AxMSComm1.PortOpen = False ----------------------------------------------------------------------------------------------------------------------------- Sub COMM1()        Dim Ctn As Integer        Dim Buffer$        Ctn = 0        Do            Application.DoEvents()            Buffer$ = Buffer$ & AxMSComm1.Input        Loop Until (InStr(Buffer$, "OK" & vbCrLf)) Or (InStr(Buffer$, "ERROR" & vbCrLf))        List1.Items.Add(Buffer$)        If InStr(Buffer$, "+CMGW:") Then            Ctn = InStr(Buffer$, "+CMGW:")            Out_STR = Trim(Mid(Buffer$, Ctn + 6, 4))        End If    End Sub ---------------------------------------------------------------------------------------------------------------------------- --------c#部分代码(据说测试未通过,因我也没有设备测试所以就没继续做下去了,有兴趣可以帮忙看下错误在哪里,流程和上面的一样)--------------------- using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data; namespace WindowsApplication5{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form {  private System.Windows.Forms.GroupBox groupBox1;  private System.Windows.Forms.TextBox t_port;  private System.Windows.Forms.Label label1;  private System.Windows.Forms.Label label3;  private System.Windows.Forms.TextBox t_rate;  private System.Windows.Forms.Label label4;  private System.Windows.Forms.TextBox t_bytesize;  private System.Windows.Forms.Label label5;  private System.Windows.Forms.TextBox t_stopbyte;  private AxMSCommLib.AxMSComm com;  public static string  strIndex="";  public static string  strIn="";  public string  strtest="";  private System.Windows.Forms.Label label9;  private System.Windows.Forms.Button button2;  private System.Windows.Forms.Button button1;  private System.Windows.Forms.TextBox textBox3;  private System.Windows.Forms.Label label8;  private System.Windows.Forms.TextBox textBox2;  private System.Windows.Forms.ListBox listBox1;  /// <summary>  /// 必需的设计器变量。  /// </summary>  private System.ComponentModel.Container components = null;   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 窗体设计器生成的代码  /// <summary>  /// 设计器支持所需的方法 - 不要使用代码编辑器修改  /// 此方法的内容。  /// </summary>  private void InitializeComponent()  {   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));   this.groupBox1 = new System.Windows.Forms.GroupBox();   this.button1 = new System.Windows.Forms.Button();   this.textBox3 = new System.Windows.Forms.TextBox();   this.label9 = new System.Windows.Forms.Label();   this.textBox2 = new System.Windows.Forms.TextBox();   this.label8 = new System.Windows.Forms.Label();   this.button2 = new System.Windows.Forms.Button();   this.t_port = new System.Windows.Forms.TextBox();   this.label1 = new System.Windows.Forms.Label();   this.label3 = new System.Windows.Forms.Label();   this.t_rate = new System.Windows.Forms.TextBox();   this.label4 = new System.Windows.Forms.Label();   this.t_bytesize = new System.Windows.Forms.TextBox();   this.label5 = new System.Windows.Forms.Label();   this.t_stopbyte = new System.Windows.Forms.TextBox();   this.com = new AxMSCommLib.AxMSComm();   this.listBox1 = new System.Windows.Forms.ListBox();   this.groupBox1.SuspendLayout();   ((System.ComponentModel.ISupportInitialize)(this.com)).BeginInit();   this.SuspendLayout();   //    // groupBox1   //    this.groupBox1.Controls.Add(this.button1);   this.groupBox1.Controls.Add(this.textBox3);   this.groupBox1.Controls.Add(this.label9);   this.groupBox1.Controls.Add(this.textBox2);   this.groupBox1.Controls.Add(this.label8);   this.groupBox1.Controls.Add(this.button2);   this.groupBox1.Controls.Add(this.t_port);   this.groupBox1.Controls.Add(this.label1);   this.groupBox1.Controls.Add(this.label3);   this.groupBox1.Controls.Add(this.t_rate);   this.groupBox1.Controls.Add(this.label4);   this.groupBox1.Controls.Add(this.t_bytesize);   this.groupBox1.Controls.Add(this.label5);   this.groupBox1.Controls.Add(this.t_stopbyte);   this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));   this.groupBox1.Location = new System.Drawing.Point(8, 15);   this.groupBox1.Name = "groupBox1";   this.groupBox1.Size = new System.Drawing.Size(216, 329);   this.groupBox1.TabIndex = 7;   this.groupBox1.TabStop = false;   this.groupBox1.Text = "参数设置";   //    // button1   //    this.button1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));   this.button1.Location = new System.Drawing.Point(112, 256);   this.button1.Name = "button1";   this.button1.TabIndex = 14;   this.button1.Text = "发送简讯";   this.button1.Click += new System.EventHandler(this.button1_Click);   //    // textBox3   //    this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.textBox3.Location = new System.Drawing.Point(96, 176);   this.textBox3.Name = "textBox3";   this.textBox3.Size = new System.Drawing.Size(104, 21);   this.textBox3.TabIndex = 13;   this.textBox3.Text = "";   //    // label9   //    this.label9.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));   this.label9.Location = new System.Drawing.Point(8, 184);   this.label9.Name = "label9";   this.label9.Size = new System.Drawing.Size(88, 16);   this.label9.TabIndex = 12;   this.label9.Text = "发送内容:";   //    // textBox2   //    this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.textBox2.Location = new System.Drawing.Point(96, 144);   this.textBox2.Name = "textBox2";   this.textBox2.Size = new System.Drawing.Size(104, 21);   this.textBox2.TabIndex = 11;   this.textBox2.Text = "";   //    // label8   //    this.label8.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));   this.label8.Location = new System.Drawing.Point(8, 152);   this.label8.Name = "label8";   this.label8.Size = new System.Drawing.Size(88, 16);   this.label8.TabIndex = 10;   this.label8.Text = "电话号码:";   //    // button2   //    this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;   this.button2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));   this.button2.Location = new System.Drawing.Point(16, 256);   this.button2.Name = "button2";   this.button2.TabIndex = 3;   this.button2.Text = "应用设置";   this.button2.Click += new System.EventHandler(this.button2_Click);   //    // t_port   //    this.t_port.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.t_port.Location = new System.Drawing.Point(96, 16);   this.t_port.Name = "t_port";   this.t_port.Size = new System.Drawing.Size(104, 21);   this.t_port.TabIndex = 2;   this.t_port.Text = "1";   //    // label1   //    this.label1.Location = new System.Drawing.Point(8, 24);   this.label1.Name = "label1";   this.label1.Size = new System.Drawing.Size(72, 16);   this.label1.TabIndex = 1;   this.label1.Text = "串口号:";   //    // label3   //    this.label3.Location = new System.Drawing.Point(8, 56);   this.label3.Name = "label3";   this.label3.Size = new System.Drawing.Size(72, 16);   this.label3.TabIndex = 1;   this.label3.Text = "波特率:";   //    // t_rate   //    this.t_rate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.t_rate.Location = new System.Drawing.Point(96, 48);   this.t_rate.Name = "t_rate";   this.t_rate.Size = new System.Drawing.Size(104, 21);   this.t_rate.TabIndex = 2;   this.t_rate.Text = "115200";   //    // label4   //    this.label4.Location = new System.Drawing.Point(8, 88);   this.label4.Name = "label4";   this.label4.Size = new System.Drawing.Size(72, 16);   this.label4.TabIndex = 1;   this.label4.Text = "数据位:";   //    // t_bytesize   //    this.t_bytesize.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.t_bytesize.Location = new System.Drawing.Point(96, 80);   this.t_bytesize.Name = "t_bytesize";   this.t_bytesize.Size = new System.Drawing.Size(104, 21);   this.t_bytesize.TabIndex = 2;   this.t_bytesize.Text = "8";   //    // label5   //    this.label5.Location = new System.Drawing.Point(8, 120);   this.label5.Name = "label5";   this.label5.Size = new System.Drawing.Size(72, 16);   this.label5.TabIndex = 1;   this.label5.Text = "停止位:";   //    // t_stopbyte   //    this.t_stopbyte.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;   this.t_stopbyte.Location = new System.Drawing.Point(96, 112);   this.t_stopbyte.Name = "t_stopbyte";   this.t_stopbyte.Size = new System.Drawing.Size(104, 21);   this.t_stopbyte.TabIndex = 2;   this.t_stopbyte.Text = "1";   //    // com   //    this.com.Enabled = true;   this.com.Location = new System.Drawing.Point(240, 336);   this.com.Name = "com";   this.com.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("com.OcxState")));   this.com.Size = new System.Drawing.Size(38, 38);   this.com.TabIndex = 9;   //    // listBox1   //    this.listBox1.ItemHeight = 12;   this.listBox1.Location = new System.Drawing.Point(232, 24);   this.listBox1.Name = "listBox1";   this.listBox1.Size = new System.Drawing.Size(216, 304);   this.listBox1.TabIndex = 10;   //    // Form1   //    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);   this.ClientSize = new System.Drawing.Size(456, 390);   this.Controls.Add(this.listBox1);   this.Controls.Add(this.com);   this.Controls.Add(this.groupBox1);   this.Name = "Form1";   this.Text = "Form1";   this.Load += new System.EventHandler(this.Form1_Load);   this.groupBox1.ResumeLayout(false);   ((System.ComponentModel.ISupportInitialize)(this.com)).EndInit();   this.ResumeLayout(false);   }  #endregion   /// <summary>  /// 应用程序的主入口点。  /// </summary>  [STAThread]  static void Main()   {   Application.Run(new Form1());  }  public bool send_at_command(string strCmd, string strData)  {   int i, j;   string strSUB = "/x1A";   bool blnSucc = false;   com.Output=strCmd.Trim();   strtest=strtest + com.Input;   listBox1.Items.Add(strtest);   if (strData != null)    {    com.Output = strData+strSUB;    strtest=strtest + com.Input;    listBox1.Items.Add(strtest);   }    for (i = 0; i < 200000; i++)    {    strIn = strIn + com.Input;    if (strIn.IndexOf("OK") != 0)     {     blnSucc = true;     break;    }    if (strIn.IndexOf("ERR") != 0)     break;   }    // get index   if ((strData != null) && (blnSucc == true))    {    i = strIn.IndexOf("+CMGW:");    listBox1.Items.Add(i);    j = strIn.Length;    listBox1.Items.Add(j);    if (i!= 0)     {     listBox1.Items.Add(strIn);//     strIn = strIn.Substring(i+6,3);//     listBox1.Items.Add(strIn);//     j = strIn.IndexOf(strLF);//     strIndex = strIn.Substring(1, j-1);     strIndex=strIn.Trim();    }   }   return blnSucc;   }  private void Form1_Load(object sender, System.EventArgs e)  {    }   private void button2_Click(object sender, System.EventArgs e)  {   try   {    com.CommPort =Convert.ToInt16(t_port.Text.Trim());          if (com.PortOpen) com.PortOpen = false;     com.Settings = "115200,N,8,1";     com.InputLen = 0;        com.PortOpen = true;       MessageBox.Show("Comm Port Open Success!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);   }   catch   {    MessageBox.Show("Comm Port Not Open!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);      }   }   private void button1_Click(object sender, System.EventArgs e)  {   string myphone=textBox2.Text.Trim();   string mycontext=textBox3.Text.Trim();   sms_send_msg(myphone,mycontext);    }   public bool sms_send_msg(string phone_no, string msg)  {   string strcmd;    strIndex = "";   // reset   strcmd = "atz"+"/r";   if (send_at_command(strcmd, null) != true)    return false;    // select message stroage    strcmd =  "at+cpms=/"MT/""+"/r";    if (send_at_command(strcmd, null) != true)     return false;    // select TE character set    strcmd =  "at+cscs=/"ASCII/""+"/r";    if (send_at_command(strcmd, null) != true)     return false;    // write to stroage    strcmd =  "at+cmgw=" ;    strcmd += "/"";    strcmd +=  phone_no;    strcmd += "/"";    strcmd += "/r";    if (send_at_command(strcmd, msg) != true)     return false;    if (strIndex != "")    {     // send message     strcmd =  "at+cmss="+strIndex+"/r";     if (send_at_command(strcmd, null) != true)      return false;     // delete message     strcmd =  "at+cmgd="+strIndex+"/r";     if (send_at_command(strcmd, null) != true)      return false;    }    else     return false;    return true;   }  } } --------------------------------------------------------------------------------------------------------------------------------  

    最新回复(0)