📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Text;
namespace GPSLocate
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Thread MainThread;
static ComIO.CommOpr s_port = new ComIO.CommOpr();
private System.Windows.Forms.TextBox textBox1;
public string nLength;
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()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(696, 280);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 328);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "start";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(328, 328);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 2;
this.button2.Text = "stop";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(400, 328);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "Send";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 296);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(696, 21);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(696, 357);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
s_port.PortNum = "COM1";
s_port.BaudRate = 9600;
s_port.ByteSize = 8;
s_port.Parity = 0;
s_port.StopBits = 1;
s_port.ReadTimeout = 500;
try
{
if (s_port.Opened)
{
s_port.Close();
s_port.Open();
}
else
{
s_port.Open();//打开串口
}
}
catch
{
listBox1.Items.Add("端口打开失败!");
return ;
}
listBox1.Items.Add("端口打开成功!");
s_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r")); //获取手机短信中心号
string response = Encoding.ASCII.GetString(s_port.Read(128));
if(response.Length > 15)
{
listBox1.Items.Add("与短信模块连接成功!");
}
else
{
listBox1.Items.Add("与短信模块连接失败!");
}
Thread.Sleep(1000);
s_port.Write(Encoding.ASCII.GetBytes("AT+CNMI=2,2,0,0,0\r"));
response = Encoding.ASCII.GetString(s_port.Read(128));
// Thread.Sleep(1000);
// string con="170400A87E7E00007E7E030002170891683108100005F011010D91683125322027F400F50B0E";
// con=smsDecodedsms("8613800100500","8613439234253",con);
//
// byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r",nLength));
// s_port.Write(buf);
// response = Encoding.ASCII.GetString(s_port.Read(128));
// if( response.Length ==4 && response.EndsWith("> "))
// {
// s_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a",con)));
// listBox1.Items.Add("设置成功!");
// }
// else
// {
// listBox1.Items.Add("设置失败!");
// }
MainThread=new Thread(new ThreadStart(MainProcedure));
MainThread.Start();
}
private void MainProcedure()
{
while(true)
{
byte[] ans=s_port.Read(1024);
if(ans.Length>0)
{
string ss=System.Text.Encoding.ASCII.GetString(ans);
listBox1.Items.Add(System.Text.Encoding.ASCII.GetString(ans));
}
else
{
Thread.Sleep(10);
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
//683134294352F3 8613439234253
//683125322027F4 8613522302724
//683118500607F8 8613810560708
//683108100005F0
//车台 170400A8 A8000417
string con="170400A87E7E00007E7E010501";
//con="ABC";
//con="C8329BFD0E01";
con=smsDecodedsms("8613800100500","8613439234253",con);
Thread.Sleep(1000);
byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r",nLength));
s_port.Write(buf);
string response = Encoding.ASCII.GetString(s_port.Read(128));
if( response.Length ==4 && response.EndsWith("> "))
{
s_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a",con)));
listBox1.Items.Add("指令发送成功!");
listBox1.Items.Add(String.Format("{0}\x01a",con));
}
else
{
listBox1.Items.Add("指令发送失败!");
}
}
public string smsDecodedsms(string strCenterNumber, string strNumber, string strSMScontent)
{
string s = String.Format("{0}11000D91{1}00F500{2}",smsDecodedCenterNumber(strCenterNumber),smsDecodedNumber(strNumber),specialen(strSMScontent));
nLength =String.Format("{0:D2}", (s.Length - smsDecodedCenterNumber(strCenterNumber).Length) / 2 ); //获取短信内容加上手机号码长度
return s;
}
public string smsDecode(string strCenterNumber, string strNumber, string strSMScontent)
{
string s = String.Format("{0}11000D91{1}00F500{2}",smsDecodedCenterNumber(strCenterNumber),smsDecodedNumber(strNumber),smsPDUEncoded(strSMScontent));
nLength =String.Format("{0:D2}", (s.Length - smsDecodedCenterNumber(strCenterNumber).Length) / 2 ); //获取短信内容加上手机号码长度
return s;
}
public string smsDecodedNumber(string srvNumber)
{
string s = null;
if (!(srvNumber.Substring(0,2) == "86"))
{
srvNumber = String.Format("86{0}",srvNumber); //检查当前接收手机号是否按标准格式书写,不是,就补上“86”
}
int nLength = srvNumber.Length;
for(int i = 1 ; i < nLength ; i += 2) //将奇数位和偶数位交换
{
s += srvNumber[i];
s += srvNumber[i-1];
}
if(!(nLength % 2 == 0)) //是否为偶数,不是就加上F,并对最后一位与加上的F位互换
{
s += 'F';
s += srvNumber[nLength - 1];
}
return s;
}
public string smsDecodedCenterNumber(string srvCenterNumber)
{
string s = null;
int nLength = srvCenterNumber.Length;
for(int i = 1 ; i < nLength;i += 2) //奇偶互换
{
s += srvCenterNumber[i];
s += srvCenterNumber[i-1];
}
if(!(nLength % 2 == 0)) //是否为偶数,不是就加上F,并对最后一位与加上的F位互换
{
s += 'F';
s += srvCenterNumber[nLength - 1];
}
s = String.Format("91{0}",s); //加上91,代表短信中心类型为国际化
s = String.Format("{0:X2}{1}",s.Length / 2,s); //编码后短信中心号长度,并格式化成二位十六制
return s;
}
//短信内容编码
public string smsPDUEncoded(string srvContent)
{
Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode;
string sPDUContext= null;
byte [] encodedBytes = encodingUTF.GetBytes(srvContent);
for (int i =0;i < encodedBytes.Length;i++)
{
sPDUContext += BitConverter.ToString(encodedBytes,i,1);
}
sPDUContext = String.Format("{0:X2}{1}",sPDUContext.Length / 2,sPDUContext);
return sPDUContext;
}
//格式化成二位十六制
public string specialen(string srvContent)
{
srvContent = String.Format("{0:X2}{1}",srvContent.Length / 2,srvContent);
return srvContent;
}
private void button2_Click(object sender, System.EventArgs e)
{
MainThread.Abort();
s_port.Close();
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
textBox1.Text=listBox1.SelectedItems[0].ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -