項目需求:由於上週機房溫度過高造成機房失火,要求實現溫度預警程式。
設備:感溫器(串口通訊,每四秒將當前溫度發送到串口)
GRPS modem (usb模擬串口通訊,通過at指令實現短信發送)
SERVER一臺
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
Call COMM1()
AxMSComm1.Output = "at+cpms=MT" & Chr(13) ' 絋﹚
Call COMM1()
AxMSComm1.Output = "at+cscs=ASCII" & Chr(13) ' 絋﹚
Call COMM1()
AxMSComm1.Output = "at+cmgw=" + Chr(34) + PhoneNUM + Chr(34) & Chr(13) ' 絋﹚
AxMSComm1.Output = SendMSG & Chr(26) ' 絋﹚
Call COMM1()
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
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
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
{
/// <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;
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <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>
[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);
}
{
strIn = strIn + com.Input;
if (strIn.IndexOf("OK") != 0)
{
blnSucc = true;
break;
}
if (strIn.IndexOf("ERR") != 0)
break;
}
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)
{
}
{
try
{
com.CommPort =Convert.ToInt16(t_port.Text.Trim());
if (com.PortOpen) com.PortOpen = false;
com.Settings = "115200,N,8,1";
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);
}
}
{
string myphone=textBox2.Text.Trim();
string mycontext=textBox3.Text.Trim();
sms_send_msg(myphone,mycontext);
}
{
string strcmd;
// 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;
strcmd = "at+cscs=/"ASCII/""+"/r";
if (send_at_command(strcmd, null) != true)
return false;
strcmd = "at+cmgw=" ;
strcmd += "/"";
strcmd += phone_no;
strcmd += "/"";
strcmd += "/r";
if (send_at_command(strcmd, msg) != true)
return false;
{
strcmd = "at+cmss="+strIndex+"/r";
if (send_at_command(strcmd, null) != true)
return false;
strcmd = "at+cmgd="+strIndex+"/r";
if (send_at_command(strcmd, null) != true)
return false;
else
return false;
return true;
}
}
--------------------------------------------------------------------------------------------------------------------------------