⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pduencod.cs

📁 SendSMS 发送短信的源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
using System.Threading;
using System.IO.Ports;  
 

namespace PDUEncoder
{
    class PDUEncod
    {
        public string Resp = "";//save response
        string PDU = "";//save encoded PDU
        string data = "";//data on the port
        
        public PDUEncod()//constructor
        { 
        
        }
        public string SendSMS(string Phone, string data)//SMS send
        {
            PDU=SetPhInHex(Phone);
            data=MakeHex(data);
            /*
             Note that i am using 001101 as default string and also 00001D,you can change it accordingly
             */
            PDU = "001101" + PDU + "00001D"+data;//complete PDU for SMS
            return PDU;
        }
        private string SetPhInHex(string phone)//change phone # according to PDU format
        {
            string Type="";
            int Len = phone.Length;
            string PhLength= Microsoft.VisualBasic.Strings.Format((object)Len, "X2");
            if (phone.Contains("+"))
            {
                Type = "91";//international
            }
            else
            {
                Type = "81";//national
            }
            if (Len % 2 == 1)
            {
                phone += "F"; //if length is odd then add 'F's
            }
            phone = InvertPhone(phone);//invert the phone no....
            phone = PhLength + Type + phone;    //changed phone #
            return phone;
        }
        private string InvertPhone(string ph)//invert the phone #
        {
            char[] phone = ph.ToCharArray();
            char[] tmp=new char[phone.Length];
            string res = "";
            for (int i = 0; i < ph.Length; i = i + 2)//3243->4332
            {
                tmp[i] = phone[i + 1];
                tmp[i + 1] = phone[i];  
            }
            for (int i = 0; i < tmp.Length; i++)//save inverted phone in res
            {
                res += tmp[i].ToString();  
            }
            return res;
        }
        private string MakeHex(string data)//convert data to hex 
        {
            int DataLen = data.Length;
            string res = Microsoft.VisualBasic.Strings.Format(DataLen, "X2");
            char[] ch = data.ToCharArray();
            string tmp = "";

            foreach (char c in ch)
            {
                tmp = CharToBits(c) + tmp;//char to bits like 'a' -> 1010101
            }

            int i = 0;
            if ((tmp.Length % 8) != 0)//if bits are not multiple of 8 then add some '0'
            {
                int tp=(8 - (tmp.Length % 8));
                for (i = 1; i <=tp ; i++)
                 tmp = "0" + tmp;
            }

            string result = "";
            for (i = tmp.Length - 8; i >= 0; i = i - 8)//bits to hex
            {
                result = result + BitsToHex(Microsoft.VisualBasic.Strings.Mid(tmp, i + 1, 8));
            }
            result = res + result; 
            return result;
        }
        private string CharToBits(char c)//char to bits
        {
            
            if (c == '@') return "0000000";//default
            string Result = "";
            int i = 0; 
            for (i = 0; i <= 6; i++)//7-bit technique used here
            {
                int val = Microsoft.VisualBasic.Strings.Asc(c);
                int val1=(int)Math.Pow(2,i);
                if ( (val & val1) > 0)
                    Result = "1" + Result;
                else
                    Result = "0" + Result;
            }
            return Result;

        }
        private string BitsToHex(string Bits)//bits to hex
        {
                int i=0, v=0;
                for( i = 0; i<=Bits.Length - 1;i++)//make hex
                {
                    int val = (int)Microsoft.VisualBasic.Conversion.Val(Microsoft.VisualBasic.Strings.Mid(Bits, (int)(i + 1), 1));
                    int val1 = (int)Math.Pow(2, (double)(7 - i));  
                   v = v +  val* val1;      
                }
                string result="";
                result = Microsoft.VisualBasic.Strings.Format(v, "X2");//get result in hex
                return result;  
        }
        public bool Response(string res,SerialPort pt)//get response from serialport
        {
            bool fg = true;
            int count = 0;
            while ((!(data.Contains(res))))//wait until data come on port
            {
                if (data.Contains("Error")) { fg = false; break; }
                Thread.Sleep(100);
                count++;
                if (count >= 4) { fg = false; break; }
                data = pt.ReadExisting();//read existing data on the port
            }
            Resp = data;//save the data
            data = "";
            return fg;
        }
        public void SendCmd(string cmd, string res,SerialPort pt)//send commnad on port
        {
        Start:
            pt.Write(cmd + "\r\n");//wirte on port
            if (!Response(res,pt))//wait until response come on the port
            {
                goto Start;
            }

        }
        public int GetMsgIndex(string res)//get index of the SMS
        {
            string[] sp = new string[] { "\r", "\n" };
            string[] tmp = res.Split(sp, StringSplitOptions.None);
            string[] final = new string[tmp.Length];
            int indx = 0;
            //get index from the responsed string , split it and get SMS #
            try
            {
                for (int i = 0; i < tmp.Length; i++)
                {
                    if (tmp[i] != "") { final[indx] = tmp[i]; indx++; }
                }
                for (int i = 0; i < final.Length; i++)
                {
                    if (final[i].Contains("+")) { res = final[i]; break; }
                }
                tmp = res.Split(':');
                indx = Convert.ToInt32(tmp[1].Trim());
                tmp = null; final = null; res = null;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            return indx;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -