C# at命令发送&&接收短信测试程序

此文转载,出处不明。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
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;
            button6.Enabled = false;
            button7.Enabled = false;
            textBox7.Enabled = true;
            textBox7.Text = "0";
            textBox8.Text = "0";
            textBox7.Enabled = false;
            textBox10.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            button10.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;
        }
        private void ShowMsg(string msg)
        {
            textBox9.Text += msg;
            int index = msg.IndexOf("+CSCA:");
            if (index != -1)
            {
                textBox10.Enabled = true;
                textBox10.Text = msg.Substring(index + 11, 11);
                textBox10.Enabled = false;
                return;
            }
            if (msg.IndexOf("\"SM\",") != -1)
            {
                int number = int.Parse(msg.Substring(msg.IndexOf("\"SM\",") + 5));
                int number7 = int.Parse(textBox7.Text);
                MessageBox.Show("有新信息,位置为:" + msg.Substring(msg.IndexOf("\"SM\",") + 5));
                textBox8.Text = msg.Substring(msg.IndexOf("\"SM\",") + 5);
                if (number7 < number)
                {
                    textBox7.Enabled = true;
                    textBox7.Text = number.ToString();
                    textBox7.Enabled = false;
                }
                return;
            }
            if (msg.Length > 33)
            {
                string telnum = "+" + DecodeTelnum(msg);
                string msgDestination = DecodeMessage(msg);
                string time = DecodeTime(msg);
                textBox1.Text = telnum;
                textBox4.Text = msgDestination;
                textBox5.Text = time;
            }
            else
            {
                if (msg.Equals("Error"))
                {
                    textBox1.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                }
                int index1 = msg.IndexOf("+CMGL:");
                int index2 = msg.IndexOf(",");
                if (index1 != -1)
                {
                    string strTemp = msg.Substring(index1 + 6, index2 - index1 - 6);
                    if (textBox7.Text.Trim().CompareTo(strTemp) > 0)
                    {
                        textBox7.Enabled = true;
                        textBox7.Text = strTemp.Trim();
                        textBox7.Enabled = false;
                    }
                }
                textBox6.Text = msg;
            }
            //if (msg != "")
            //{
            //    //string telnum = DecodeTelnum(msg);
            //    //string msgDestination = DecodeMessage(msg);
            //    //textBox1.Text = msgDestination;
            //    //if (textBox1.Text.Trim().Equals(""))
            //    //    textBox1.Text = msg;
            //    //else
            //    //    textBox1.Text = textBox1.Text + Environment.NewLine + " " + msg;
            //    //button1.Enabled = true;
            //}
            //if (msg.Length > 5)
            //    if (msg.Substring(0, 5).Equals("+CMTI"))
            //    {
            //        numberRead = msg.Substring(msg.LastIndexOf(',') + 1);
            //    }
        }
        /// <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);//转换成UTF-16编码
            string strDestination = Convert.ToString(tmpSmsText.Length, 16);//将长度表示为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 button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = EncodeTelnum(textBox2.Text.Trim().ToString());
            serialPort1.Close();
            textBox1.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = false;
            button6.Enabled = false;
            button5.Enabled = false;
            button4.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            button10.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";
            try
            {
                serialPort1.Open();
            }
            catch
            {
                MessageBox.Show("端口已经被占用!");
                return;
            }
            textBox1.Text = "";
            textBox1.Text = EncodeMessage(textBox3.Text);
            //serialPort1.WriteLine("at+cmgf=1");//Text模式发送英文
            serialPort1.Write("at+cmgf=0\x0a");//PDU模式发送
            //serialPort1.WriteLine("at+cnmi=2,1");//
            //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;
            button6.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;
            button9.Enabled = true;
            button10.Enabled = true;
        }


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值