C#通用类库--短信猫操作类2使用方法(原始AT命令)

使用方法很简单,只要实例化类调用其中对应的方法就行,根据返回值再处理!

 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)

如有不足之处请指正!欢迎大家多提意见!谢谢!

posted @ 2011-03-02 17:03  飞扬青云  阅读(2382)  评论(2编辑  收藏  举报