当前位置:首页 >> 电力/水利 >>

c#发短信一


using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.For

ms;

namespace SPAT { delegate void del_ShowMsg(string msg); public partial class Form1 : Form { public Form1() { InitializeComponent(); button1.Enabled = false; button3.Enabled = false; button4.Enabled = false; button5.Enabled = false; } private void Form1_Load(object sender, EventArgs e) { } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (serialPort1.IsOpen) Invoke(new del_ShowMsg(ShowMsg), serialPort1.ReadLine()); else return; } /// <summary> /// 将电话号码奇偶位对换进行编码 /// </summary> /// <param name="str"></param> /// <returns></returns> private string EncodeTelnum(string str) { //源电话号码 string strSource = "86" + str;

if (strSource.Length % 2 == 1)//如果为奇数,补 F strSource += "F"; //变换号码 char[] strchar = strSource.ToCharArray(); char temp; if (strchar.Length % 2 == 1) MessageBox.Show("error", "Error"); else { for (int i = 0; i < strchar.Length; i = i + 2) { temp = strchar[i]; strchar[i] = strchar[i + 1]; strchar[i + 1] = temp; } } string strDestination = new string(strchar); return strDestination; } /// <summary> /// 混合编码 /// </summary> /// <param name="str"></param> /// <returns></returns> private string EncodeUnicode(string str) { byte[] tmpSmsText = Encoding.Unicode.GetBytes(str); string strDestination = Convert.ToString(tmpSmsText.Length, 16); if (strDestination.Length == 1) strDestination = "0" + strDestination; else if (strDestination.Length == 0) strDestination = "00"; for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调 { string strTemp = Convert.ToString(tmpSmsText[i + 1], 16); if (strTemp.Length == 1) strTemp = "0" + strTemp; else if (strTemp.Length == 0) strTemp = "00"; strDestination += strTemp; strTemp = Convert.ToString(tmpSmsText[i], 16);

if (strTemp.Length == 1) strTemp = "0" + strTemp; else if (strTemp.Length == 0) strTemp = "00"; strDestination += strTemp; } return strDestination; } /// <summary> /// 汉字 PDU 编码 /// </summary> /// <param name="str"></param> /// <returns></returns> private string EncodeMessage(string str) { string strDestination; if (str.Length * 2 < 16) strDestination = "0" + Convert.ToString(str.Length * 2, 16); else if (str.Length * 2 < 256) strDestination = Convert.ToString(str.Length * 2, 16); else { MessageBox.Show("所发信息数据量太大", "Error"); return "Error"; } foreach (char item in str) { int i = item; strDestination += Convert.ToString(i, 16); } return strDestination; } /// <summary> /// 英文编码 /// </summary> /// <param name="str"></param> /// <returns></returns> private string EncodeEnglish(string str) { string strDestination = Convert.ToString(str.Length, 16); if (str.Length < 16) strDestination = "0" + strDestination;

for (int i = 0; i < str.Length; i++) { if (i < (str.Length - 1)) { if (i % 8 != 7) strDestination += EncodeLetter(str[i], str[i + 1], i % 8); else continue; } else strDestination += EncodeLetter(str[i], '\0', i % 8); } return strDestination; } /// <summary> /// 单个字符编码 /// </summary> /// <param name="a1"></param> /// <param name="a2"></param> /// <param name="i"></param> /// <returns></returns> private string EncodeLetter(char a1, char a2, int i) { string stra1 = Convert.ToString(a1, 2); char[] strChara1 = new char[8]; int j = 0, k = 0; //赋值 for (j = stra1.Length - 1, k = 0; k < 8; j--) { if (j >= 0) strChara1[k++] = stra1[j]; else strChara1[k++] = '0'; } //右移 i 位 for (j = 0, k = i; j < 8; ) { if (k < 8) strChara1[j++] = strChara1[k++]; else

strChara1[j++] = '0';//高位用 0 补足 } char[] strChara2 = new char[8]; //赋值 if (a2 == '\0') { for (int m = 0; m < 8; m++) strChara2[m] = '0'; } else { string stra2 = Convert.ToString(a2, 2); for (j = stra2.Length - 1, k = 0; k < 8; j--) { if (j >= 0) strChara2[k++] = stra2[j]; else strChara2[k++] = '0'; } } for (int s = 0; s < i + 1; s++) { strChara1[7 - i + s] = strChara2[s]; } strChara1 = Inversion(strChara1); stra1 = new string(strChara1); int inta1 = Convert.ToInt32(stra1, 2); string strDestion = Convert.ToString(inta1, 16); if (strDestion.Length == 1) strDestion = "0" + strDestion; else if (strDestion.Length == 0) strDestion = "00"; return strDestion; } /// <summary> /// 逆置字符数组 /// </summary> /// <param name="a1"></param> /// <returns></returns> private char[] Inversion(char[] a1) { char[] a2 = new char[a1.Length];

for (int i = 0, j = a1.Length - 1; i < a1.Length; i++, j--) { a2[j] = a1[i]; } return a2; } private void ShowMsg(string msg) { if (msg != "") { if (textBox1.Text.Trim().Equals("")) textBox1.Text = msg; else textBox1.Text = textBox1.Text + Environment.NewLine + " " + msg; button1.Enabled = true; } } //关闭串口 private void button1_Click(object sender, EventArgs e) { textBox1.Text = EncodeTelnum(textBox2.Text.Trim().ToString()); serialPort1.Close(); button1.Enabled = false; button2.Enabled = true; button3.Enabled = false; button4.Enabled = false; label1.Text = serialPort1.IsOpen.ToString(); } //打开串口 private void button2_Click(object sender, EventArgs e) { //serialPort1.NewLine = Environment.NewLine; serialPort1.BaudRate = 115200; serialPort1.PortName = "com1"; serialPort1.Open(); textBox1.Text = ""; textBox1.Text = EncodeMessage(textBox3.Text); //serialPort1.WriteLine("at+cmgf=1");//Text 模式发送英文 serialPort1.Write("at+cmgf=0\x0a");//PDU 模式发送 label1.Text = serialPort1.IsOpen.ToString(); button2.Enabled = false; button1.Enabled = true; button3.Enabled = true; }

//发送短信 private void button3_Click(object sender, EventArgs e) { textBox1.Text = ""; //serialPort1.WriteLine("at+cmgs=" + textBox2.Text);//Text 模式发送英 文 //int count = 14 + textBox3.Text.Length * 2 + 1;//PDU 模式发送中文 //int count = 14 + EncodeEnglish(textBox3.Text).Length / 2;//PDU 模式 发送英文 int count = 14 + EncodeUnicode(textBox3.Text).Length / 2;//PDU 模式混 发 serialPort1.WriteLine("at+cmgs=" + count);//PDU 模式发送 button1.Enabled = false; button3.Enabled = false; button5.Enabled = true; button4.Enabled = true; } //发送确认 private void button4_Click(object sender, EventArgs e) { //serialPort1.WriteLine(textBox3.Text + "\x01a");//Text 模式发送英文 //string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000800" + EncodeMessage(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU 模式发送 中文 string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000800" + EncodeUnicode(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU 模式混发 //string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000000" + EncodeEnglish(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU 模式发送 英文 serialPort1.WriteLine(message);//PDU 模式发送 button4.Enabled = false; button5.Enabled = false; button3.Enabled = true; } //发送取消 private void button5_Click(object sender, EventArgs e) { serialPort1.WriteLine("\x01b"); button4.Enabled = false; button5.Enabled = false; button3.Enabled = true; button1.Enabled = true; }

} //////////////////////////// //打开串口 //发送(可以实现一键发送将下面的代码替换上面 button2_click 的代码可以实现点击 button2 直接发送) private void button2_Click(object sender, EventArgs e) { //serialPort1.NewLine = Environment.NewLine; serialPort1.BaudRate = 115200; serialPort1.PortName = "com1"; serialPort1.Open(); textBox1.Text = ""; textBox1.Text = EncodeMessage(textBox3.Text); //serialPort1.WriteLine("at+cmgf=1");//Text 模式发送英文 serialPort1.Write("at+cmgf=0\x0a");//PDU 模式发送 int count = 14 + EncodeUnicode(textBox3.Text).Length / 2;//PDU 模式混 发 serialPort1.WriteLine("at+cmgs=" + count);//PDU 模式发送 string message = "0011000D91" + EncodeTelnum(textBox2.Text.Trim()) + "000800" + EncodeUnicode(textBox3.Text.Trim()).ToUpper() + "\x01a";//PDU 模式混发 serialPort1.WriteLine(message);//PDU 模式发送 label1.Text = serialPort1.IsOpen.ToString(); button2.Enabled = false; button1.Enabled = true; button3.Enabled = true; }


相关文章:
c#发短信一
c#发短信一c#发短信一隐藏>> using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System....
C#利用短信猫收发短信息的方法
C#利用短信猫收发短信息的方法_计算机软件及应用_IT...实现过程 (1)新建一个项目,命名为 Ex13_14,默认...} // 发送短信 if (GMS.GSMModemSMSsend(null, ...
基于C#平台的短信发送软件设计
基于以上原因,设计了一种基于 C#利用 GSM MODEM 实现短信 发送的方案,该方案...AT+CMGS:发送短信。这是短信发送中的核心指令。短信内容结果编码后 会得到一串...
用C#实现短信收发2
C#实现短信收发2_信息与通信_工程科技_专业资料。使用C#通过串口方式实现短信的...下列循环将已读短信逐一从SIM卡内存中删除,否则若SIM卡内存不足将再无发收发!...
C#开发终端式短信的原理和方法
C#开发终端式短信的原理和方法简介 没发过短信的年轻人肯定是属于那种受保护的稀有...西门子的手机大多只支持 PDU 模式, PDU 模式是发送或接收手机 SMS 信息的一...
某公司C#短信接口
某公司C#短信接口_IT/计算机_专业资料。使用的C#短信...发 else if(result==110) MessageBox.Show("短信...<returns>返回一个整型</returns> [DllImport("...
如果发短信给一个人
如果发短信给一个人_人力资源管理_经管营销_专业资料。如果发短信给一个人,他...c#发短信一 5页 免费 发短信 5页 免费 怎么样给刚认识的女孩发... 5页 ...
C#短信猫操作源码开发经验_图文
我曾经开发的某项目需要使用短信猫模块,要求使用 C#编程操作。能独立 使用的动态链接库之一就是网上流传的 sms.dll,但它的功能较少,不能满足需 求, 鉴于一时...
短信发送原理
短信的原理和实现方法,重点说明了短信的编码方式、AT 指令以及用 C#实现串口通讯...tutorial_id =320 ,一个国外网友的写 的教程,作者很热心,我曾有发邮件给他,...
基于C#.NET的GSM MoDEM短信猫应用设计开发
(2)开发独立的短信后台服务,实现发送短信的过程是从短信发送队列中读取短信,调用...2.3 串口通信的实现 C#(C sharp)是微软公司发布的一种面向对象的、 运行于....
更多相关标签:
c 发短信 | c pc给手机发短信 | c 发短信 安卓 | 女神发短信口a田c甲d | c at 发短信 安卓 | sim800c 发短信 | c 程序给手机发短信 | c 如何给手机发短信 |