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

📄 form1.cs

📁 利用手机或短信发射机发送短信的源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
            //测试用户发送量加1
            iTestUserCurrentSendCount++;
        }
        //////////////////////////////////////////////////////////////////////////
        //设置短信格式为PDU模式(发送AT+CMGF=0.)
        public byte[] Command_SetPDUMode()
        {
            string temps;
            temps = myPDUdecoding.smsPDUEncoded("AT+CMGF=0");
            if (temps.Length % 2 != 0)
                temps = "0" + temps;
            temps = temps.Substring(2, temps.Length - 2);
            temps = delStr00(temps);
            temps += "0D0A";//PDU串完成

            byte[] tempb = new byte[80];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
        //////////////////////////////////////////////////////////////////////////
        //发送短信长度(发送AT+CMGS=032.)
        public byte[] Command_SendContentLength()
        {
            string temps;
            int intCount = ((myPDUdecoding.smsDecodedsms(strCenterNumber.ToString(), strReceiverNumber, strSmsContent)).Length - 18) / 2;
            temps = myPDUdecoding.smsPDUEncoded("AT+CMGS=" + intCount.ToString());
            if (temps.Length % 2 != 0)
                temps = "0" + temps;
            temps = temps.Substring(2, temps.Length - 2);
            temps = delStr00(temps);
            temps += "0D";//PDU串完成

            byte[] tempb = new byte[80];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
        //////////////////////////////////////////////////////////////////////////
        //发送任意数据命令
        public byte[] Command_Send(String command)
        {
            string temps;
            temps = myPDUdecoding.smsPDUEncoded(command);
            if (temps.Length % 2 != 0)
                temps = "0" + temps;
            temps = temps.Substring(2, temps.Length - 2);
            temps = delStr00(temps);
            temps += "0D0A";//PDU串完成

            byte[] tempb = new byte[80];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
        //////////////////////////////////////////////////////////////////////////
        //直接发送PDU码
        public byte[] PDUCommand_Send(String PDUCommand)
        {
            string temps;
            temps = PDUCommand;//PDU串完成

            byte[] tempb = new byte[80];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
        //////////////////////////////////////////////////////////////////////////
        //主发送短信函数
        public byte[] SendSms()
        {
            string pdu = String.Empty; //开始合成 PDU 串
            //短信正文转为Unicode       
            byte[] tmpSmsText = Encoding.Unicode.GetBytes(myPDUdecoding.smsDecodedsms(strCenterNumber.ToString(), strReceiverNumber, strSmsContent));

            pdu += tmpSmsText.Length.ToString("X2"); //正文内容长度

            for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调 
            {
                pdu += tmpSmsText[i + 1].ToString("X2");//("X2")转为16进制
                pdu += tmpSmsText[i].ToString("X2");
            }
            if (pdu.Substring(0,1)!="B")
            {
                pdu = pdu.Substring(1, pdu.Length - 1);
            } 
            else
            {

            }
            
            pdu = pdu.Substring(2, pdu.Length - 2);
            pdu = delStr00(pdu);
            pdu += "1A0D0A";//PDU串完成

            //////////////////////////////////////////////////////////////////////////


            Console.WriteLine(pdu);
            int tmpLength = (pdu.Length - 18) / 2;//除去SMSC段长度

            string temps = delspace(pdu);
            if (temps.Length % 2 != 0)
                temps = "0" + temps;

            byte[] tempb = new byte[512];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
        //////////////////////////////////////////////////////////////////////////
        //提取数据包
        public byte[] Command_ExtractionDataPack()
        {
            string temps = delspace("1A");
            if (temps.Length % 2 != 0)
                temps = "0" + temps;

            byte[] tempb = new byte[80];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;

        }


        private void txbSmsContent_GotFocus(object sender, EventArgs e)
        {
            if (this.txbSmsContent.Text == "请填写短信内容...")
            {
                this.txbSmsContent.Text = String.Empty;
            }
        }
        //////////////////////////////////////////////////////////////////////////
        //开串口
        public bool OpenCom()
        {
            //////////////////////////////////////////////////////////////////////////
            //新加入初始化COM过程,运用
            port.PortName = "COM"+strPort.ToString();
            try
            {
                if (port.IsOpen) 
                    port.Close();
                else
                {
                    port.Open();
                    port.Close();
                }
            }
            catch (Exception e)
            {
                this.SetText("串口:" + port.PortName + "打开失败,错误代码:" +e+ "\r\n");
            }

            //////////////////////////////////////////////////////////////////////////
            //原来的过程,似乎不能设置一些参数
            try
            {
                if (mycom1.Opened)
                {
                    mycom1.Close();
                    this.SetText("串口关闭......" + "\r\n");
                    mycom1.Open(); //打开串口
                    this.SetText("串口打开......" + "\r\n");
                }
                else
                {
                    mycom1.Open();//打开串口
                    this.SetText("串口打开......" + "\r\n");
                }
                return true;
            }
            catch (Exception e)
            {
                this.SetText("错误:" + e.Message + "\r\n");
                return false;
            }
        }

        //////////////////////////////////////////////////////////////////////////
        //去掉发送数组中的空格
        public string delspace(string putin)
        {
            string putout = "";
            for (int i = 0; i < putin.Length; i++)
            {
                if (putin[i] != ' ')
                    putout += putin[i];
            }
            return putout;
        }
        //////////////////////////////////////////////////////////////////////////
        //去除多余的"00"
        private string delStr00(string strPDU)
        {
            int intCount;
            intCount = strPDU.Length;
            string tempPDU = strPDU;
            intCount = tempPDU.Length;
            for (int i = 1; i <= intCount / 4; i++)
            {
                tempPDU = tempPDU.Substring(0, 4 * (intCount / 4 - i)) + tempPDU.Substring(4 * (intCount / 4 - i) + 2, i * 2);
            }
            return tempPDU;
        }
        //////////////////////////////////////////////////////////////////////////
        //程序关闭,结束串口
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (iAllowClose==true)
            {
                mycom1.Close(); 
            }
            else
            {
                e.Cancel = true;
                this.Hide();
            }
        }
        //////////////////////////////////////////////////////////////////////////
        //显示包信息
        public string dis_package(byte[] reb)
        {
            string temp = "";
            foreach (byte b in reb)
                temp += b.ToString("X2") + " ";
            return temp;
        }

        private void btnClearContent_Click(object sender, EventArgs e)
        {
            txbSmsContent.Text = String.Empty;
            txbReceiverNumber.Text = String.Empty;
        }

        //////////////////////////////////////////////////////////////////////////
        //右键菜单
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            iAllowClose = true;
            this.Close();
        }

        private void MinSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void MaxSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();            
            this.WindowState = FormWindowState.Normal;
            this.Width = 710;
            this.Height = 582; 


        }

        private void txbSmsContent_LostFocus(object sender, EventArgs e)
        {
            if (this.txbSmsContent.Text == String.Empty)
            {
                this.txbSmsContent.Text = "请填写短信内容...";
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Width = 710;
            this.Height = 582; 
        }

    }
}

⌨️ 快捷键说明

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