如今手機(jī)已成為大眾的交流工具。有關(guān)手機(jī)的程序開(kāi)發(fā)越來(lái)越廣泛,本節(jié)通過(guò)幾個(gè)典型案例介紹怎么利用短信貓發(fā)送、接收短信、遠(yuǎn)程控制計(jì)算機(jī)、業(yè)務(wù)員銷售數(shù)據(jù)采集與短信息娛樂(lè)互動(dòng)平臺(tái)。
案例431 利用短信貓收發(fā)短信息
案例詳解
短信貓是利用SIM卡發(fā)送短信的硬件設(shè)備,通過(guò)串口或USB接口(根據(jù)設(shè)備型號(hào)而定)與計(jì)算機(jī)相連。在程序中可以利用短信貓發(fā)送或接收短信。本例編寫(xiě)了利用短信貓收發(fā)短信息的功能。案例運(yùn)行結(jié)果如圖13.15所示。
技術(shù)要點(diǎn)
本例使用的是金笛的串口短信貓。在購(gòu)買短信貓時(shí)會(huì)附帶包括SDK的開(kāi)發(fā)包,其中提供了操作短信貓的函數(shù)(封裝在dllforvc.dll動(dòng)態(tài)庫(kù)中)。下面介紹操作短信貓的主要函數(shù)。
(1)GSMModemGetSnInfoNew函數(shù)
該函數(shù)獲取短信貓注冊(cè)需要的信息,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetSnInfoNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetSnInfoNew(string device, string baudrate);
參數(shù)詳解如下。
l device:通信端口,為null時(shí)系統(tǒng)會(huì)自動(dòng)檢查。
l baudrate:通訊波特率,為null時(shí)系統(tǒng)會(huì)自動(dòng)檢查。
(2)GSMModemGetDevice函數(shù)
該函數(shù)獲取當(dāng)前的通訊端口,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetDevice",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetDevice();
(3)GSMModemGetBaudrate函數(shù)
該函數(shù)獲取當(dāng)前的通訊波特率,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetBaudrate",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetBaudrate();
(4)GSMModemInitNew函數(shù)
該函數(shù)用于初始化短信貓。語(yǔ)法如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemInitNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemInitNew(
string device,
string baudrate,
string initstring,
string charset,
bool swHandshake,
string sn);
參數(shù)詳解如下。
l device:標(biāo)識(shí)通信端口,如果為NULL,系統(tǒng)會(huì)自動(dòng)檢查。
l baudrate:標(biāo)識(shí)通訊波特率,如果為NULL,系統(tǒng)會(huì)自動(dòng)檢查。
l initstring:標(biāo)識(shí)初始化命令,為NULL即可。
l charset:標(biāo)識(shí)通訊字符集,為NULL即可。
l swHandshake:標(biāo)識(shí)是否進(jìn)行軟件握手,為False即可。
l sn:標(biāo)識(shí)短信貓的授權(quán)號(hào),需要根據(jù)實(shí)際情況填寫(xiě)。
(5)GSMModemSMSsend函數(shù)
該函數(shù)用于發(fā)送手機(jī)短信。語(yǔ)法如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSsend",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemSMSsend(
string serviceCenterAddress,
int encodeval,
string text,
int textlen,
string phonenumber,
bool requestStatusReport);
參數(shù)詳解如下。
l serviceCenterAddress:標(biāo)識(shí)短信中心號(hào)碼,為NULL即可。
l encodeval:標(biāo)識(shí)短信息編碼格式,如果為8,表示中文短信編碼。
l text:標(biāo)識(shí)短信內(nèi)容。
l textlen:標(biāo)識(shí)短信內(nèi)容的長(zhǎng)度。
l phonenumber:標(biāo)識(shí)接收短信的電話號(hào)碼。
l requestStatusReport:標(biāo)識(shí)狀態(tài)報(bào)告。
(6)GSMModemSMSReadAll函數(shù)
該函數(shù)取得所有短信息,包括SIM卡與手機(jī)中的短信息。返回的短信內(nèi)容格式為電話號(hào)碼1|短信內(nèi)容1||電話號(hào)碼2|短信內(nèi)容2||:
//接收短信息返回字符串格式為:手機(jī)號(hào)碼|短信內(nèi)容||手機(jī)號(hào)碼|短信內(nèi)容||
//RD_opt為1表示接收短信息后不做任何處理,為0表示接收后刪除信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSReadAll",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemSMSReadAll(int RD_opt);
參數(shù)詳解如下。
l RD_opt:對(duì)讀取后的短信息進(jìn)行處理,0表示刪除,1表示不做處理。
編寫(xiě)過(guò)程
(1)新建一個(gè)項(xiàng)目,命名為Ex13_14,默認(rèn)窗體為Form1。
(2)在Form1窗體中,主要添加TextBox控件與Label控件,控件的數(shù)量及用途如圖13.15所示,添加兩個(gè)Button控件,分別用于發(fā)送短信息與接收短信息。
(3)主要程序代碼。
將所使用的函數(shù)封裝在GMS類中。代碼如下:
class GMS
{
//初始化gsm modem,并連接gsm modem
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemInitNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemInitNew(
string device,
string baudrate,
string initstring,
string charset,
bool swHandshake,
string sn);
//獲取短信貓新的標(biāo)識(shí)號(hào)碼
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetSnInfoNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetSnInfoNew(string device, string baudrate);
//獲取當(dāng)前通訊端口
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetDevice",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetDevice();
//獲取當(dāng)前通訊波特率
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetBaudrate",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetBaudrate();
//斷開(kāi)連接并釋放內(nèi)存空間
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemRelease",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern void GSMModemRelease();
//取得錯(cuò)誤信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetErrorMsg",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetErrorMsg();
//發(fā)送短信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSsend",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemSMSsend(
string serviceCenterAddress,
int encodeval,
string text,
int textlen,
string phonenumber,
bool requestStatusReport);
//接收短信息返回字符串格式為:手機(jī)號(hào)碼|短信內(nèi)容||手機(jī)號(hào)碼|短信內(nèi)容||
//RD_opt為1接收短信息后不做任何處理,0為接收后刪除信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSReadAll",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemSMSReadAll(int RD_opt);
}
在裝載Form1窗體時(shí),獲取設(shè)備信息。代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
//機(jī)器號(hào)碼,當(dāng)參數(shù)為空時(shí),函數(shù)自動(dòng)獲取設(shè)備信息
txtJQHM.Text = GMS.GSMModemGetSnInfoNew(txtCOM.Text, txtBTL.Text);
txtCOM.Text = GMS.GSMModemGetDevice(); //COM1
txtBTL.Text= GMS.GSMModemGetBaudrate(); //波特率
}
發(fā)送短信息。代碼如下:
private void btnSend_Click(object sender, EventArgs e)
{
if(txtSJHM.Text == “”)
{
MessageBox.Show(“手機(jī)號(hào)碼無(wú)法為空!”,”提示”, MessageBoxButtons.OK);
txtSJHM.Focus();
return;
}
if(txtContent.Text==””)
{
MessageBox.Show(“短信內(nèi)容無(wú)法為空!”, “提示”, MessageBoxButtons.OK);
txtContent.Focus();
return;
}
//連接設(shè)備
if(GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text)==false)
{
MessageBox.Show(“設(shè)備連接失敗!” + GMS.GSMModemGetErrorMsg(),”提示”, MessageBoxButtons.OK);
return;
}
// 發(fā)送短信
if (GMS.GSMModemSMSsend(null, 8, txtContent.Text, Encoding.Default.GetByteCount(txtContent.Text),txtSJHM.Text, false) == true)
MessageBox.Show(“短信發(fā)送成功!”, “提示”, MessageBoxButtons.OK);
else
MessageBox.Show(“短信發(fā)送失敗!” + GMS.GSMModemGetErrorMsg(), “提示”, MessageBoxButtons.OK);
}
接收短信息。代碼如下:
private void btnResvice_Click(object sender, EventArgs e)
{
//1)連接設(shè)備
if (GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text) == false)
{
MessageBox.Show(“連接失敗!” + GMS.GSMModemGetErrorMsg(), “提示”, MessageBoxButtons.OK);
return;
}
//2)接收短信
txtContent.Text = GMS.GSMModemSMSReadAll(1);
txtSJHM.Text = txtContent.Text.Substring(0, 13);
txtContent.Text = txtContent.Text.Substring(13, txtContent.Text.Length-13);
}
案例431 利用短信貓收發(fā)短信息
案例詳解
短信貓是利用SIM卡發(fā)送短信的硬件設(shè)備,通過(guò)串口或USB接口(根據(jù)設(shè)備型號(hào)而定)與計(jì)算機(jī)相連。在程序中可以利用短信貓發(fā)送或接收短信。本例編寫(xiě)了利用短信貓收發(fā)短信息的功能。案例運(yùn)行結(jié)果如圖13.15所示。
技術(shù)要點(diǎn)
本例使用的是金笛的串口短信貓。在購(gòu)買短信貓時(shí)會(huì)附帶包括SDK的開(kāi)發(fā)包,其中提供了操作短信貓的函數(shù)(封裝在dllforvc.dll動(dòng)態(tài)庫(kù)中)。下面介紹操作短信貓的主要函數(shù)。
(1)GSMModemGetSnInfoNew函數(shù)
該函數(shù)獲取短信貓注冊(cè)需要的信息,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetSnInfoNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetSnInfoNew(string device, string baudrate);
參數(shù)詳解如下。
l device:通信端口,為null時(shí)系統(tǒng)會(huì)自動(dòng)檢查。
l baudrate:通訊波特率,為null時(shí)系統(tǒng)會(huì)自動(dòng)檢查。
(2)GSMModemGetDevice函數(shù)
該函數(shù)獲取當(dāng)前的通訊端口,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetDevice",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetDevice();
(3)GSMModemGetBaudrate函數(shù)
該函數(shù)獲取當(dāng)前的通訊波特率,代碼如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetBaudrate",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetBaudrate();
(4)GSMModemInitNew函數(shù)
該函數(shù)用于初始化短信貓。語(yǔ)法如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemInitNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemInitNew(
string device,
string baudrate,
string initstring,
string charset,
bool swHandshake,
string sn);
參數(shù)詳解如下。
l device:標(biāo)識(shí)通信端口,如果為NULL,系統(tǒng)會(huì)自動(dòng)檢查。
l baudrate:標(biāo)識(shí)通訊波特率,如果為NULL,系統(tǒng)會(huì)自動(dòng)檢查。
l initstring:標(biāo)識(shí)初始化命令,為NULL即可。
l charset:標(biāo)識(shí)通訊字符集,為NULL即可。
l swHandshake:標(biāo)識(shí)是否進(jìn)行軟件握手,為False即可。
l sn:標(biāo)識(shí)短信貓的授權(quán)號(hào),需要根據(jù)實(shí)際情況填寫(xiě)。
(5)GSMModemSMSsend函數(shù)
該函數(shù)用于發(fā)送手機(jī)短信。語(yǔ)法如下:
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSsend",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemSMSsend(
string serviceCenterAddress,
int encodeval,
string text,
int textlen,
string phonenumber,
bool requestStatusReport);
參數(shù)詳解如下。
l serviceCenterAddress:標(biāo)識(shí)短信中心號(hào)碼,為NULL即可。
l encodeval:標(biāo)識(shí)短信息編碼格式,如果為8,表示中文短信編碼。
l text:標(biāo)識(shí)短信內(nèi)容。
l textlen:標(biāo)識(shí)短信內(nèi)容的長(zhǎng)度。
l phonenumber:標(biāo)識(shí)接收短信的電話號(hào)碼。
l requestStatusReport:標(biāo)識(shí)狀態(tài)報(bào)告。
(6)GSMModemSMSReadAll函數(shù)
該函數(shù)取得所有短信息,包括SIM卡與手機(jī)中的短信息。返回的短信內(nèi)容格式為電話號(hào)碼1|短信內(nèi)容1||電話號(hào)碼2|短信內(nèi)容2||:
//接收短信息返回字符串格式為:手機(jī)號(hào)碼|短信內(nèi)容||手機(jī)號(hào)碼|短信內(nèi)容||
//RD_opt為1表示接收短信息后不做任何處理,為0表示接收后刪除信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSReadAll",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemSMSReadAll(int RD_opt);
參數(shù)詳解如下。
l RD_opt:對(duì)讀取后的短信息進(jìn)行處理,0表示刪除,1表示不做處理。
編寫(xiě)過(guò)程
(1)新建一個(gè)項(xiàng)目,命名為Ex13_14,默認(rèn)窗體為Form1。
(2)在Form1窗體中,主要添加TextBox控件與Label控件,控件的數(shù)量及用途如圖13.15所示,添加兩個(gè)Button控件,分別用于發(fā)送短信息與接收短信息。
(3)主要程序代碼。
將所使用的函數(shù)封裝在GMS類中。代碼如下:
class GMS
{
//初始化gsm modem,并連接gsm modem
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemInitNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemInitNew(
string device,
string baudrate,
string initstring,
string charset,
bool swHandshake,
string sn);
//獲取短信貓新的標(biāo)識(shí)號(hào)碼
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetSnInfoNew",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetSnInfoNew(string device, string baudrate);
//獲取當(dāng)前通訊端口
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetDevice",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetDevice();
//獲取當(dāng)前通訊波特率
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetBaudrate",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetBaudrate();
//斷開(kāi)連接并釋放內(nèi)存空間
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemRelease",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern void GSMModemRelease();
//取得錯(cuò)誤信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemGetErrorMsg",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemGetErrorMsg();
//發(fā)送短信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSsend",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern bool GSMModemSMSsend(
string serviceCenterAddress,
int encodeval,
string text,
int textlen,
string phonenumber,
bool requestStatusReport);
//接收短信息返回字符串格式為:手機(jī)號(hào)碼|短信內(nèi)容||手機(jī)號(hào)碼|短信內(nèi)容||
//RD_opt為1接收短信息后不做任何處理,0為接收后刪除信息
[DllImport("dllforvc.dll",
EntryPoint = "GSMModemSMSReadAll",
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern string GSMModemSMSReadAll(int RD_opt);
}
在裝載Form1窗體時(shí),獲取設(shè)備信息。代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
//機(jī)器號(hào)碼,當(dāng)參數(shù)為空時(shí),函數(shù)自動(dòng)獲取設(shè)備信息
txtJQHM.Text = GMS.GSMModemGetSnInfoNew(txtCOM.Text, txtBTL.Text);
txtCOM.Text = GMS.GSMModemGetDevice(); //COM1
txtBTL.Text= GMS.GSMModemGetBaudrate(); //波特率
}
發(fā)送短信息。代碼如下:
private void btnSend_Click(object sender, EventArgs e)
{
if(txtSJHM.Text == “”)
{
MessageBox.Show(“手機(jī)號(hào)碼無(wú)法為空!”,”提示”, MessageBoxButtons.OK);
txtSJHM.Focus();
return;
}
if(txtContent.Text==””)
{
MessageBox.Show(“短信內(nèi)容無(wú)法為空!”, “提示”, MessageBoxButtons.OK);
txtContent.Focus();
return;
}
//連接設(shè)備
if(GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text)==false)
{
MessageBox.Show(“設(shè)備連接失敗!” + GMS.GSMModemGetErrorMsg(),”提示”, MessageBoxButtons.OK);
return;
}
// 發(fā)送短信
if (GMS.GSMModemSMSsend(null, 8, txtContent.Text, Encoding.Default.GetByteCount(txtContent.Text),txtSJHM.Text, false) == true)
MessageBox.Show(“短信發(fā)送成功!”, “提示”, MessageBoxButtons.OK);
else
MessageBox.Show(“短信發(fā)送失敗!” + GMS.GSMModemGetErrorMsg(), “提示”, MessageBoxButtons.OK);
}
接收短信息。代碼如下:
private void btnResvice_Click(object sender, EventArgs e)
{
//1)連接設(shè)備
if (GMS.GSMModemInitNew(txtCOM.Text, txtBTL.Text, null, null, false, txtPower.Text) == false)
{
MessageBox.Show(“連接失敗!” + GMS.GSMModemGetErrorMsg(), “提示”, MessageBoxButtons.OK);
return;
}
//2)接收短信
txtContent.Text = GMS.GSMModemSMSReadAll(1);
txtSJHM.Text = txtContent.Text.Substring(0, 13);
txtContent.Text = txtContent.Text.Substring(13, txtContent.Text.Length-13);
}
__________________
北京玉笛信息技術(shù)有限責(zé)任公司
地址:北京海淀區(qū)知春路23號(hào)量子銀座903(863軟件園)
地址:北京海淀區(qū)知春路23號(hào)量子銀座903(863軟件園)