使用方法很簡單,只要執行個體化類調用其中對應的方法就行,根據傳回值再處理!
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)
如有不足之處請指正!歡迎大家多提意見!謝謝!