C#通用類庫–簡訊貓操作類2使用方法(原始AT命令)

來源:互聯網
上載者:User

使用方法很簡單,只要執行個體化類調用其中對應的方法就行,根據傳回值再處理!

 myGSM gsm = new myGSM("COM3", 9600);

 打電話:gsm.Call("15000450819");

 發簡訊:listBox1.Items.Add(gsm.SendMsg("15000450819", "我就再發一條簡訊CSabcd!,呵呵!").ToString());

 擷取手機機器碼:listBox1.Items.Add("機器碼:" + gsm.GetMachineNo()); 

 擷取短訊息中心號碼:listBox1.Items.Add("短訊息中心號碼:" + gsm.GetMsgCenterNo());

 擷取未讀簡訊:

string[] ss = gsm.GetUnReadMsg();
            foreach (string s in ss)
            {
                if (s != string.Empty && s.Length != 0)
                {
                    listBox1.Items.Add(s);
                }
            }    

擷取所有簡訊: try
            {
                string[] ss = gsm.GetAllMsg();
                foreach (string s in ss)
                {
                    if (s != string.Empty && s.Length != 0)
                    {
                        listBox1.Items.Add(s);
                    }
                }
            }
            catch { }

 讀取指定序號簡訊:textBox1.Text = gsm.ReadMsgByIndex(3);

 開啟裝置:

try
            {
                gsm.OpenComm();
                this.cs(true);
                button8.Enabled = false;
            }
            catch { MessageBox.Show("開啟裝置出錯!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }

關閉裝置:try
            {
                gsm.CloseComm();
                this.cs(false);
                button8.Enabled = true;
            }
            catch { MessageBox.Show("關閉裝置出錯!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }

下面列出該測試介面所有代碼:View Code using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using myClassLibrary;

namespace myClasssLibraryTest
{
    public partial class frmGSM : Form
    {
        public frmGSM()
        {
            InitializeComponent();
        }
        myGSM gsm = new myGSM("COM3", 9600);
        delegate void UpdataDelegate();
        UpdataDelegate UpdateHandle = null; 

        private void frmGSM_Load(object sender, EventArgs e)
        {
            gsm.GetNewMsg += new myGSM.OnRecievedHandler(my_OnRecieved);
            UpdateHandle = new UpdataDelegate(Updata1);
            this.cs(false);
            button8.Enabled = true;
        }

        void my_OnRecieved(object sender, EventArgs e)
        {
            Invoke(UpdateHandle, null);
        }

        void Updata1()
        {
            textBox1.Text = "收到新訊息";
            listBox1.Items.Add(gsm.ReadNewMsg());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gsm.Call("15000450819"); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(gsm.SendMsg("15000450819", "我就再發一條簡訊CSabcd!,呵呵!").ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("機器碼:" + gsm.GetMachineNo());  
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("短訊息中心號碼:" + gsm.GetMsgCenterNo()); 
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string[] ss = gsm.GetUnReadMsg();
            foreach (string s in ss)
            {
                if (s != string.Empty && s.Length != 0)
                {
                    listBox1.Items.Add(s);
                }
            }       
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                string[] ss = gsm.GetAllMsg();
                foreach (string s in ss)
                {
                    if (s != string.Empty && s.Length != 0)
                    {
                        listBox1.Items.Add(s);
                    }
                }
            }
            catch { } 
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = gsm.ReadMsgByIndex(3);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                gsm.OpenComm();
                this.cs(true);
                button8.Enabled = false;
            }
            catch { MessageBox.Show("開啟裝置出錯!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                gsm.CloseComm();
                this.cs(false);
                button8.Enabled = true;
            }
            catch { MessageBox.Show("關閉裝置出錯!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void cs(bool b)
        {
            button1.Enabled = b;
            button2.Enabled = b;
            button3.Enabled = b;
            button4.Enabled = b;
            button5.Enabled = b;
            button6.Enabled = b;
            button7.Enabled = b;
            button8.Enabled = b;
            button9.Enabled = b;
        }
    }
}

 一個C#資源分享平台,專業分享學習高品質代碼,每周期布置學習任務,激發學習C#興趣!(QQ群:128874886)

如有不足之處請指正!歡迎大家多提意見!謝謝!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.