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

📄 form1.cs

📁 使用pdudecoding,pduencoding 和串口
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JustinIO;
using SMS;
using System.IO;
using smsForCharp;

namespace WindowsApplication1
{
    
    public  partial  class Form1 : System.Windows.Forms.Form  
    {
        public Form1()
        {
            InitializeComponent();
           
           
        }
        /// <summary>
        /// 初始化串口
        /// </summary>
        public bool InitCom(string m_port, int m_baudrate)
        {
            ss_port.PortNum = m_port;//串口号
            ss_port.BaudRate = m_baudrate;//波特率
            ss_port.ByteSize = 8;//数据位
            ss_port.Parity = 0;//
            ss_port.StopBits = 1;//停止位
            ss_port.ReadTimeout = 1000;//读超时
            try
            {
                if (ss_port.Opened)
                {
                    ss_port.Close();
                    ss_port.Open();
                }
                else
                {
                    ss_port.Open();//打开串口
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("错误:" + e.Message);
                return false;
            }
        }  
         
       
        private void butconnect_Click(object sender, EventArgs e)
        {
/// <summary>
/// 初始化代码,并获取手机相关信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
 
 bool opened = InitCom(combocom.SelectedItem.ToString(),Convert.ToInt32(combobot.SelectedItem.ToString()));//打开并初始化串口
 bool Connected = false;
 if (opened)
 {
     //ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //获取手机品牌
     string response = Encoding.ASCII.GetString(ss_port.Read(128));
     //if (response.Length > 0)
     //{
     //    txtconnect.Text = response.Substring(0, 7);
     //    Connected = true;
     //}
     //else
     //{
     //    txtconnect.Text = "与手机连接不成功";
     //    Connected = false;
     //}
     //ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMM\r"));//获取手机型号
     //response = Encoding.ASCII.GetString(ss_port.Read(128));
     //if (response.Length > 0)
     //{
     //    txtconnect.Text = txtconnect.Text + " " + response.Substring(0, 5) + " 连接中......";
     //    Connected = true;
     //}
     //else
     //{
     //    txtconnect.Text = "与手机连接不成功";
     //    Connected = false;
     //}
  //ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
     
  //response = Encoding.ASCII.GetString(ss_port.Read(128));

  //if(response.Length > 0)
  //{
  //   //txtserverno.Text = response.Substring(2,13);
  //    txtserverno.Text = response.Substring(11, 13);
  // Connected = true;
  //}
  //else
  //{
  // Connected = false;
  //}
  //if (Connected == true)
  //{
  // butconnect.Enabled = false;
  // butsend.Enabled = true;
  //}
  //else
  //{
  // butconnect.Enabled = true;
  // butsend.Enabled = false;
  //}
 }
 
        }

       
        /// <summary>
/// 发送短信
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butsend_Click(object sender, System.EventArgs e)
{
 string decodedSMS = sms.smsDecodedsms(txtserverno.Text,txttono.Text,txtsms.Text);
 byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r",sms.nLength));
 ss_port.Write(buf);
 string response = Encoding.ASCII.GetString(ss_port.Read(128));
 string SendState = "";
 if( response.Length >0 && response.EndsWith("> "))
 {
  ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a",decodedSMS)));
  SendState = "发送成功!";
 }
 else
 {
  SendState = "发送失败";
 }

 string Result = String.Format("{0},{1},{2},\n\r",txttono.Text,txtsms.Text,SendState);
 txtback.Text += Result;
}

        
        /// <summary>
        /// 关闭串口,退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butexit_Click(object sender, System.EventArgs e)
        {
            ss_port.Close();
           //Application.Exit();
            butconnect.Enabled = true;
        }

        private void butread_Click(object sender, EventArgs e)
        {

           // string decodedSMS = sms.smsDecodedsms(txtserverno.Text, txttono.Text, txtsms.Text);
            byte[] buf = Encoding.ASCII.GetBytes(String.Format("AT+CMGL={0}\r", txttype.Text ));
            ss_port.Write(buf);
            string response = Encoding.ASCII.GetString(ss_port.Read(1280));
            txtread.Text = response;
         
            //Encoding.ASCII.GetString (ss_port.Read(
            string[] smss =sms2.GetEverySMS (response );
            textBox1.Text = smss[5];
           // textBox1.Text = sms2.GetContent(smss[5]);
            //char[] str = "\n".ToCharArray();
            //string[] temp = response.Split(str);
            //return temp;  

            int i=0;
            smss.GetUpperBound(i);
            
            
             label3.Text  = i.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // string decodedSMS = sms.smsDecodedsms(txtserverno.Text, txttono.Text, txtsms.Text);
            byte[] buf = Encoding.ASCII.GetBytes(String.Format("AT+CMGL={0}\r", txttype.Text));
            ss_port.Write(buf);
            string response = Encoding.ASCII.GetString(ss_port.Read(1280));
            txtread.Text = response;

            //Encoding.ASCII.GetString (ss_port.Read(
            string[] smss = sms2.GetEverySMS(response);
            textBox1.Text = smss[5];
            textBox1.Text = sms2.GetContent(smss[5]);
            //char[] str = "\n".ToCharArray();
            //string[] temp = response.Split(str);
            //return temp;  

            int i = 0;
            smss.GetUpperBound(i);


            label3.Text = i.ToString();
        }
    }
}

⌨️ 快捷键说明

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